Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to harvest files from several directories - CLI

  1. #1
    Join Date
    Nov 2009
    Beans
    6

    How to harvest files from several directories - CLI

    Hi everyone,
    I'm in the process oflearning Bash scripting. Right now I want to harvest all the .png files from several subfolders (all are in one master folder) and put them all in one target folder. I played a bit with ls -R and grep *.png, but this aparently gets me nowhere. I know there must be a simple solution, but I seem to lack the skills. Could anyone of you commandline ninjas show me towards the right direction?
    Thank you very much in advance,
    Christian

  2. #2
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: How to harvest files from several directories - CLI

    Code:
    for f in /path/to/folder1/*.png /path/to/folder2/*.png /path/to/folder3/*.png; do
    # cp $f
    # mv $f
    done
    replace the folders with the ones you want. if you want to copy, remove the # before cp; the # before mv for move

  3. #3
    Join Date
    Nov 2009
    Beans
    6

    Re: How to harvest files from several directories - CLI

    Quote Originally Posted by HiImTye View Post
    Code:
    for f in /path/to/folder1/*.png /path/to/folder2/*.png /path/to/folder3/*.png; do
    # cp $f
    # mv $f
    done
    replace the folders with the ones you want. if you want to copy, remove the # before cp; the # before mv for move
    Brilliant. Is there a way to look into these folders recursively? So I only have to specify the main folder, not each individual subfolder?

    Thanks for your efforts,
    Christian

  4. #4
    Join Date
    Oct 2005
    Location
    Al Ain
    Beans
    8,077

    Re: How to harvest files from several directories - CLI

    Howdy,

    Learn how to use the 'find' command, with the 'exec' option. Read the man page.

    The reason why Bash looping is so cripple, is because 'find' does it all.

  5. #5
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: How to harvest files from several directories - CLI

    Quote Originally Posted by HiImTye View Post
    Code:
    for f in /path/to/folder1/*.png /path/to/folder2/*.png /path/to/folder3/*.png; do
    # cp $f
    # mv $f
    done
    replace the folders with the ones you want. if you want to copy, remove the # before cp; the # before mv for move
    helps if I have an out folder, should be
    Code:
    out="/path/to/folder"
    for f in /path/to/folder1/*.png /path/to/folder2/*.png /path/to/folder3/*.png; do
    #cp "$f" "$out"
    #mv "$f" "$out"
    done
    for recursive, use something like
    Code:
    in="/path/to/folder1/
    /path/to/folder2/
    /path/to/folder3/"
    out="/path/to/folder"
    while read -r f; do
    #cp -R "$f" "$out"
    #mv -R "$f" "$out"
    done< <(echo -e "$test")
    if you're saving them as scripts then set out to "$1" and in to "$2" then execute them as
    Code:
    scriptName "/path/to/folder anywhere"
    #-- or --
    scriptName "/path/to/folder anywhere" "/path/to/folder1
    /path/to/folder2
    /path/to/folder3"
    lastly, if you're saving scripts, it's a good idea to make line 1
    Code:
    #!/bin/bash
    Last edited by HiImTye; May 29th, 2013 at 05:47 AM.

  6. #6
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: How to harvest files from several directories - CLI

    Quote Originally Posted by HermanAB View Post
    Howdy,

    Learn how to use the 'find' command, with the 'exec' option. Read the man page.

    The reason why Bash looping is so cripple, is because 'find' does it all.
    I prefer locate, it's faster, and bash is anything but crippled if you know how to use it. Linux commands work the Unix way, "do one thing, really well" because you can redirect their output

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

    Re: How to harvest files from several directories - CLI

    find is the tool for file search and manipulation of the results, there is nothing better.

    Code:
    mkdir /dest/dir
    find /src/dir -iname '*.png' -exec mv '{}' /dest/dir \;
    Last edited by Vaphell; May 29th, 2013 at 06:55 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: How to harvest files from several directories - CLI

    I prefer the speed of
    Code:
    mkdir /out/dir/
    locate --regex '/path/to/wherever/.*.png' '/path/to/wherever else/.*.png' | while read f; do mv "$f" /out/dir/; done
    which takes about a half second, compared to a half minute with find

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

    Re: How to harvest files from several directories - CLI

    and what if the database that locate uses is not up to date? find doesn't care about it.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Mar 2007
    Location
    Portsmouth, UK
    Beans
    Hidden!
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: How to harvest files from several directories - CLI

    Indeed, when it comes to handling files across directories, find is the tool I use the most - by far.

Page 1 of 2 12 LastLast

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
  •