Recursive command to include all sub-folders

macstatic

macrumors 65816
Original poster
Oct 21, 2005
1,342
68
Norway
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):

Code:
/Photos/
[B]Phone.jpg[/B]
[B]Mac.jpg[/B]
  /People/
    [B]popstar.jpg[/B]
      /Family/
        [B]dad.jpg[/B]
        [B]mom.jpg[/B]
        [B]sister.jpg[/B]
            /Uncles/
               [B]Uncle_Jim.jpg[/B]
               [B]Uncle_Bob.jpg [/B]
  /Places/
     [B]Tokyo.jpg[/B]
     [B]London.jpg[/B]
     [B]Paris.jpg[/B]
     [B]New_York.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:

maflynn

Moderator
Staff member
May 3, 2009
66,710
33,600
Boston
I think you need ls -lR Photos/*.jpg

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

macstatic

macrumors 65816
Original poster
Oct 21, 2005
1,342
68
Norway
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.
 

mfram

macrumors 65816
Jan 23, 2010
1,064
142
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.

Examples...
Code:
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.
 

rrl

macrumors 6502a
Jul 27, 2009
511
56
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.