Some days ago, after reading, “Creating and deleting files using the Mac Terminal”, a faithful reader wrote to

I have a Mac folder at work, which currently contains more than 10,000 images. Every month we add a few thousands images to that folder, and must remove some thousands. I do always have a complete list of the images that should be removed from the folder, but I need some sort of script for Mac that will do the job for me.

This week’s post is a general, but direct answer to that call for help. I will first summarize what scripts are and how they work, then present a simple script to do the requested job on Linux, and finally mention some resources to reuse “Linux scripts” on both Mac and Windows systems.

Disclaimer: The script and resources below were prepared specifically because a reader asked for it, but please understand that, at the moment, I do not have any Mac or Windows computer available to actually test them myself. Back up everything (which is something you should regularly do anyway) before trying them! For the same reason, readers with such systems are very welcome to point out errors, omissions, or better resources for reusing Linux tips and scripts on other platforms!

What is a script, or why a “Linux script” may not run elsewhere

Before even getting started, it is useful to review and clarify a term or two, because it will save you lots of frustration when some “Linux script” doesn’t work on other platforms. Strictly speaking, there is no such thing as a “Linux script”.

Computer programs are executable files, that is, blobs of instructions in machine-readable format. Launching a program means telling the processor in your computer to go execute that sequence of machine-level codes. Those sequences are created by special programs, called compilers, by reading the source code of that program. That code describes how the program works in a language that is much easier for humans to read and write.

Scripts are different. In the software world, that word usually defines a sequence of commands in some language, saved in a plain text file, that must not be compiled. When you “launch” a script, the operating system first detects the language in which it is written. Immediately after, the whole script file is passed to the special executable program that acts as interpreter for that language. The interpreter reads and executes all the commands directly, more or less one at a time. A Perl script is nothing else but a plain text file, written in a language that the executable program perl understands.

Gnu/Linux is not a language, but an operating system that can run all sorts of programs. That’s why there cannot be a “Linux script”, only scripts that run on Linux, because their interpreter is available, and usually preinstalled, on Linux. Shell, Perl, Python, Ruby… there are tens of scripting languages, but none of them will work if the corresponding interpreter is not installed.

To make things more complicated, er… stimulating, one single script in one language may call scripts in other languages and/or executable programs that are installed by default only on some operating system. So, don’t be surprised if the first run of a “Linux script” fails because some piece is missing. It’s not a big deal, and it is often very easy to fix.

How to remove all the files mentioned in a list

Back to the original question now! Let’s assume that you have a plain text file called thelist.txt, with the names of all and only the files you want to delete, one per line:


On Linux, you may remove all those files by opening a terminal window in the folder that contains them and typing the following command:

/complete/path/to/ thelist.txt

where is the script I’ll show you in a moment, and “/complete/path/to/” is the actual folder in which it was saved. The command above assumes that thelist.txt is in the same folders of the files to be removed. If this is not the case, you should prepend the complete path also to thelist.txt. besides, must have executable permissions. Here is its content:

  1 #! /bin/bash
  3 LIST=$1
  4 while read LINE; do
  5    echo "Now deleting: $LINE;"
  6    #rm "$LINE"
  7 done < $LIST

Line 1, often called the “shebang line” is the one that tells the operating system what interpreter should be used for the current script: the file above is written for the Bash shell interpreter /bin/bash. Line 3 assigns the first argument passed to the script to the $LIST variable. The while loop in lines 4 to 7 scans the $LIST file one line at a time. Line 5 prints to the terminal the content of the current $LINE. Line 6 would remove the corresponding file… if it were not commented with the hash character #.

The way to run in a Mac terminal that or any other shell script is explained here. I suggest that you run the script as is and then, only when you are sure that it is reading the file names correctly, remove the # character from line 6 and rerun it. For a general introduction on how to reuse “Linux scripts” on Mac or Windows, check the tutorials and programs below.

Mac OS


The first step to run on Windows scripts developed for Linux is to install the Cygwin. To learn how to use it, look at: