Recursive command to include all sub-folders

Discussion in 'macOS' started by macstatic, Oct 8, 2014.

  1. macstatic, Oct 8, 2014
    Last edited: Oct 8, 2014

    macstatic macrumors 6502a

    Joined:
    Oct 21, 2005
    #1
    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.
     
  2. maflynn Moderator

    maflynn

    Staff Member

    Joined:
    May 3, 2009
    Location:
    Boston
    #2
    I think you need ls -lR Photos/*.jpg

    The first * is causing the command to find all files.
     
  3. macstatic thread starter macrumors 6502a

    Joined:
    Oct 21, 2005
    #3
    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.
     
  4. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #4
    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.
     
  5. macstatic thread starter macrumors 6502a

    Joined:
    Oct 21, 2005
  6. rrl macrumors 6502

    rrl

    Joined:
    Jul 27, 2009
    #6
    I like to use -iname instead of -name. Just in case. [pun intended]
     

Share This Page