Recursive command to include all sub-folders


macrumors 65816
Original poster
Oct 21, 2005
What is the correct UNIX terminal command in order to list (ls), delete (rm), copy (cp) or whatever all files of a certain type (e.g. all ".jpg" images) inside a specific directory but including all sub-directories at any level?
For instance, I have a folder named "Photos" with a bunch of .jpg files spread all around in its root, sub-directories and sub-directories of the sub-directories etc. and I want to delete all of them (but leave the folders and any other files alone):

               [B]Uncle_Bob.jpg [/B]
I've been experimenting with the list command (ls) as it's not quite as destructive as "rm" (remove) ;) .... and believe I need to use the "-R" (recursive) option, but I'm not sure if I'm getting it right.

I've tried this: ls -lR Photos/* *.jpg
But it seems to also include other files and folders.

And yes, I'm aware that using the "rm" command this way can be quite destructive if you don't know what you're doing (but also quite effective and time-saving). Backing up the folder structure first is probably a smart move.
Last edited:


Staff member
May 3, 2009
I think you need ls -lR Photos/*.jpg

The first * is causing the command to find all files.


macrumors 65816
Original poster
Oct 21, 2005
It doesn't work here. Your suggestion only displays the .jpg files in the root of the /Photos/ folder and if there's no .jpg file there it tells me "no such file or directory". Apparently it doesn't do any recursive searching.


macrumors 65816
Jan 23, 2010
San Diego, CA USA
The command you're looking for is one of my favorite shell commands: find. Use 'man find' to see all the stuff it can do. It takes some getting used to, so I'll describe it.

The 'find' command will return a set of files below a given directory with the requested set of characteristics and then perform an action on those files. The characteristics I'm referring to include name, file type (in the Unix sense: directory, FIFO, file, pipe, special, etc.), date, size, etc. The action performed can be any shell command or there is a special command just for delete.

1.  Find all *.jpg files under a directory and list them.

find /my/directory/path -name '*.jpg' -type f -print

2.  Find all *.jpg files under the current directory and delete them.

find . -name '*.jpg' -type f -delete

3.  Find all *.txt files bigger than 20 megabytes and search them for the 
string "foo".  Note: the '{}' is replaced by the file name.

find . -name '*.txt' -size +20M -exec grep foo {} \;
The manage for find has more examples.


macrumors 6502a
Jul 27, 2009
I like to use -iname instead of -name. Just in case. [pun intended]
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.