Results 1 to 10 of 10

Thread: extract multiple files from multiple folders

  1. #1
    Join Date
    Apr 2007
    Beans
    284
    Distro
    Ubuntu 10.10 Maverick Meerkat

    extract multiple files from multiple folders

    What bash command would search through a folder with multiple folders and extract *.ttf to a single folder?
    MS skill level -- I press buttons and stuff happens
    Linux skill level -- I copy and paste into the terminal and stuff happens!
    Mac skill level -- Wear a black turtleneck and stuff happens

  2. #2
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    493
    Distro
    Ubuntu

    Re: extract multiple files from multiple folders

    from the current directory try the following but make a backup before you try it!

    copy
    Code:
    find . -name "*.ttf" -exec cp "{}" /dir \;
    move
    Code:
    find . -name "*.ttf" -exec mv "{}" /dir \;
    Last edited by sohlinux; April 15th, 2012 at 09:22 PM.

  3. #3
    Join Date
    Sep 2011
    Beans
    615

    Re: extract multiple files from multiple folders

    deleted by edit, because on rereading I realize I misunderstood the question. Please ignore this dumb post.
    Last edited by Dreamer Fithp Apprentice; April 16th, 2012 at 06:43 AM.

  4. #4
    Join Date
    Apr 2007
    Beans
    284
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: extract multiple files from multiple folders

    Thanks. I remember reading that the copy command also pastes? Just need to find where they ended up....
    MS skill level -- I press buttons and stuff happens
    Linux skill level -- I copy and paste into the terminal and stuff happens!
    Mac skill level -- Wear a black turtleneck and stuff happens

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: extract multiple files from multiple folders

    .bash_history file stores commands you entered

    Code:
    grep 'find.*-exec' .bash_history

  6. #6
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: extract multiple files from multiple folders

    +1 for sohlinux's solution.

    However, I would add a little cavet: since you'll be copying or moving from several directories, it is possible that two different files have the same file name. For instance:
    Code:
    $ ls -R
    file.ttf
    adir/file.ttf
    Then when moved or copied to dir/, they will be overwritten (just the last one will survive). As a safe measure, you can use the -b option (either on cp or mv). That will create a copy of the file before overwritten it.

    For example:
    Code:
    find . -name '*.ttf' -exec mv -b '{}' dir/ \;
    Cavet's cavet: sadly this will only save the last two copies. If there's a 3rd, or more, file with the same filename, just the last two will survive.

    Just s few thoughts.
    Regards.

  7. #7
    Join Date
    Apr 2007
    Beans
    284
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: extract multiple files from multiple folders

    I did a backup so duplicates won't be an issue. No duplicate files turned up. The grep command gave a no file found.

    Code:
    find . -name '*.ttf' -exec mv -b '{}' dir/ \;
    returned (the last example)
    Code:
    mv: cannot move `./lora/Lora-BoldItalic.ttf' to `dir/': Not a directory
    Which is odd because it worked the first time.
    When I went to nautilus and put in /dir no dir was found. Should mkdr be used to create an output folder?
    MS skill level -- I press buttons and stuff happens
    Linux skill level -- I copy and paste into the terminal and stuff happens!
    Mac skill level -- Wear a black turtleneck and stuff happens

  8. #8
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: extract multiple files from multiple folders

    The target directory has to exist.

    In sohlinux's example it was /dir Thats a directory under the root directory (/).

    Sorry if I created confusion, but in my example I used a local directory just under the command is executed. To created that directory run:
    Code:
    mkdir dir
    I hope that clarify things a little bit.
    Regards.

  9. #9
    Join Date
    Apr 2007
    Beans
    284
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: extract multiple files from multiple folders

    Thanks that worked.
    Code:
    find . -name '*.ttf' -exec mv -b '{}' /home/username/Documents/newfonts \;
    Could someone explain all the parts of that command.
    Last edited by tadcan; April 16th, 2012 at 10:08 PM.
    MS skill level -- I press buttons and stuff happens
    Linux skill level -- I copy and paste into the terminal and stuff happens!
    Mac skill level -- Wear a black turtleneck and stuff happens

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: extract multiple files from multiple folders

    find <dir> -name <filenames-pattern>
    -exec = for each found object perform the following:
    mv -b {} destination_dir
    {} is a placeholder for the name of the found object
    -exec is ended with escaped \; or ';' so it knows where the inner command ends

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
  •