Results 1 to 4 of 4

Thread: Moving folders containing certain file types

  1. #1
    Join Date
    Dec 2007
    Beans
    14
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Question Moving folders containing certain file types

    I've been searching the net for the last couple hours for the best way to consolidate my backed up pictures, music, and video into single directories. Right now they're scattered across my backup drive in a bunch of different folders. I was able to move all my .avi files into a single directory with this command:

    Code:
    find . -iname "*.avi" -exec mv '{}' /media/Backup/Movies/ \;
    This works fine for movies because there aren't a ton of them and they aren't really related to any other files, but if I do that for my .jpg's or .mp3's they won't be organized by date or by album anymore.

    I know I can just find directories by using find and specifying -type d, but I don't know how to tell it to:

    1. find directories containing files that end in a certain file type, and
    2. Move all those directories to a new location.

    Thanks for taking a look at this, any light you can shed on this is much appreciated.

  2. #2
    Join Date
    Feb 2008
    Location
    Townsville, Qld Australia
    Beans
    1,065

    Re: Moving folders containing certain file types

    This will only copy those files with the given extestion, you can however change the extension and run it again (if for example you have .mov files in with your pictures).
    Code:
    #find all jpgs copy them maintaining the directory structure
    find ./ -iname '*.jpg' -exec cp -b --parents {} ~/Desktop/ \;
    Registered Linux User #464572
    journal.ryanhaigh.net
    www.ryanhaigh.net

  3. #3
    Join Date
    Dec 2007
    Beans
    14
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Moving folders containing certain file types

    Thanks for the point in the right direction. One thing I discovered while testing the command you suggested is that if your destination directory is contained in the directory you are searching it will create an infinite loop. I'm sure you knew that but since I am new to bash commands I'm glad I tested it.

    I'm still searching for a way to move the directories instead of copy them but I have posted this question on several boards and this was the best answer so far.

  4. #4
    Join Date
    Dec 2007
    Beans
    222

    Re: Moving folders containing certain file types

    Quote Originally Posted by Udaho View Post
    ...if your destination directory is contained in the directory you are
    searching it will create an infinite loop.
    • Move the files to some parent or sibling directory of that you do the
      search in, i.e. if you're running search for /home move the files to /tmp.
    • Use "-prune" with "find" to exclude some sub-directories below.
      Note: There is a drawback though. If the name of directory, you have excluded,
      is common, "find" will disregard any occurrence of it in sub-directories as
      well.

      An example:

      Code:
      :~$ mkdir -p ~/{foo/{dir_1/{dir_2/{new,dir_3},new},new},foo_2}
      :~$ touch ./foo/new/file_1.txt ./foo/dir_1/new/file_2.txt \
      > ./foo/dir_1/dir_2/new/file_3.txt ./foo/file_4.txt \
      > ./foo/dir_1/file_5.txt ./foo/dir_1/dir_2/file_6.txt \
      > ./foo/dir_1/dir_2/dir_3/file_7.txt
      :~$ ls
      foo foo_2
      :~$ ls -FR ~/foo
      $HOME/foo:
      dir_1/  file_4.txt  new/
      
      $HOME/foo/dir_1:
      dir_2/  file_5.txt  new/
      
      $HOME/foo/dir_1/dir_2:
      dir_3/  file_6.txt  new/
      
      $HOME/foo/dir_1/dir_2/dir_3:
      file_7.txt
      
      $HOME/foo/dir_1/dir_2/new:
      file_3.txt
      
      $HOME/foo/dir_1/new:
      file_2.txt
      
      $HOME/foo/new:
      file_1.txt
      
      # Move all *.txt files from ~/foo and all its sub-directories to ~/foo_2:
      # omit from search any */new directory, prompt before executing (type either
      # y or n, Enter):
      
      :~$ find ~/foo -name new -prune -o -name "*.txt" -print0 \
      > | xargs -0p mv -t ~/foo_2
      mv -t $HOME/foo_2 $HOME/foo/file_4.txt $HOME/foo/dir_1/dir_2/dir_3/file_7.txt
      $HOME/foo/dir_1/dir_2/file_6.txt $HOME/foo/dir_1/file_5.txt ?...y
      
      # If you want to combine *.odt, *.txt, *.bak -> -name "*[.odt,.txt,.bak]"
      # !!! Before moving any essential data practice with some foo files.
      
      :~$ ls ~/foo_2
      file_4.txt  file_5.txt  file_6.txt  file_7.txt
      :~$ ls -FR ~/foo
      $HOME/foo:
      dir_1/  new/
      
      $HOME/foo/dir_1:
      dir_2/  new/
      
      $HOME/foo/dir_1/dir_2:
      dir_3/  new/
      
      $HOME/foo/dir_1/dir_2/dir_3:
      
      $HOME/foo/dir_1/dir_2/new:
      file_3.txt
      
      $HOME/foo/dir_1/new:
      file_2.txt
      
      $HOME/foo/new:
      file_1.txt
    • Use "-maxdepth n, -mindepth n" with "find". See "man find".


    By the way, "-exec...'{}' \;" will start a program for each file it finds, which
    is more resource and time consuming; better use "-exec...'{}' \+" or "xargs",
    it will queue all matches and execute them in order.
    Last edited by pauper; August 3rd, 2008 at 08:18 AM. Reason: Typo.
    Help yourself | [Hints] Disassemble your laptop

    "Si peccasse negamus, fallimur,
    Et nulla est in nobis veritas--"

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •