Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,052
175
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:
I think you need ls -lR Photos/*.jpg

The first * is causing the command to find all files.
 
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.
 
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.
 
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.