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

Thread: Command to copy files in subfolders to one folder

  1. #1
    Join Date
    Mar 2009
    Location
    New Zealand
    Beans
    687
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Command to copy files in subfolders to one folder

    Okay so I have a bunch of images set up like this:

    - Images
    -- Folder 1
    -- Folder 2
    -- Folder 3
    ... etc

    In each folder there are a tonne of images. I want to be able to copy all the images in each folder into a folder called "all" so that I can activate a slideshow using gThumb with ALL my images, instead of just one folder.

    I could open each folder manually and copy everything, then paste into the "all" folder, then repeat. But this is time consuming, and there must be a command line way
    Writer for OMG! Ubuntu!, Editor-in-Chief Ubuntu Gamer. Co-founder of media and software company Ohso.

  2. #2
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: Command to copy files in subfolders to one folder

    Code:
    cd Images
    That is get into the parent directory, the one you want all your images in.

    Code:
    find ./ -name '*.jpg' -exec cp '{}' ./ \;
    This assumes all the images end with the .jpg file extention.

    If there are only images in all the sudirectories, but they have different file extensions .png .JPG .jpg .jpeg etc then

    Do not do this in your home directory or you will have a right mess on your hands


    |
    |
    |
    V

    Code:
    find ./ -type f -exec cp '{}' ./ \;
    will move any file in all the subdirectories to the one you`re in.

    You can then rename the parent folder "all"
    Alternatively use feh to create the slideshow.

    Code:
    sudo apt-get install feh
    Then
    Code:
    feh -Fzr -D 5 ~/Images
    This will create a slideshow. F is fullscreen, z is random, r tells feh to go through the directories recursively and D specifies the time each picture is displayed before changing to the next one. The time is in seconds so my example would delay the pictures for 5 seconds.

    I am known for making typos, test the code first.
    Last edited by nothingspecial; January 20th, 2010 at 03:12 PM.

  3. #3
    Join Date
    Mar 2009
    Location
    Florida
    Beans
    97
    Distro
    Ubuntu Jaunty Jackalope (testing)

    Re: Command to copy files in subfolders to one folder

    Quote Originally Posted by humphreybc View Post
    Okay so I have a bunch of images set up like this:

    - Images
    -- Folder 1
    -- Folder 2
    -- Folder 3
    ... etc

    In each folder there are a tonne of images. I want to be able to copy all the images in each folder into a folder called "all" so that I can activate a slideshow using gThumb with ALL my images, instead of just one folder.

    I could open each folder manually and copy everything, then paste into the "all" folder, then repeat. But this is time consuming, and there must be a command line way
    I've managed to figure out how to move folders with a command to another drive.
    My first recommendation is to open the help menu and search "command line"
    It helps guide for other commands you may need at any given time.

    If your folder name has a space, you have to substitute it with * . Meaning DON'T use spaces when dealing with folders.

    Also note, you have to use -r or maybe -R prior to your folder name being moved. such as.... sudo mv -r your*folder*name /yournewdirectory.

    Hope this helps.

    a2z
    www.webstore.com/~a2zHere
    If you didn't learn something one day, you didn't do anything that day. Those that don't do anything one day, don't want to know anything.

  4. #4
    Join Date
    Feb 2009
    Beans
    6

    Re: Command to copy files in subfolders to one folder

    Code:
    cp /images/{folder1,folder2,etc}/* /images/all/

  5. #5
    Join Date
    Dec 2008
    Location
    UK
    Beans
    589
    Distro
    Ubuntu Mate 22.04 Jammy Jellyfish

    Re: Command to copy files in subfolders to one folder

    This post
    http://ubuntuforums.org/showpost.php...68&postcount=4
    shows a command to merge all subfolders into the current folder.
    If you use 'cp' instead of 'mv' and change the '.' to the location of your 'all' folder this should work. One issue is if you place your 'all' folder under 'images' this could cause problems.


    The folowing command should work if you are in the Images folder and the 'all' folder is *not* under the Images folder:
    Code:
    find . -type f -exec cp '{}' /path/to/all/folder \;
    I think the following might work if the 'all' folder *is* in the images folder by excluding the 'all' folder from the find:
    Code:
    find . -type f -wholename './all' -prune -exec cp '{}' ./all \;
    Again your current directory (folder) should be set to Images

    Warning: I'm typing this with access to the 'find' command manual but without a Linux system to test on, so I suggest you make a copy of your images file and test this on the copy.
    Also note you can test the 'find' part of the command by replacing the '-exec blah blah' with '-print', as mentioned in the post referenced above - this will just give you a list of files selected by find.

  6. #6
    Join Date
    Mar 2006
    Beans
    1,441
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Command to copy files in subfolders to one folder

    Quote Originally Posted by nothingspecial View Post

    Code:
    find ./ -name '*.jpg' -exec mv '{}' ./ \;
    T.
    I think the OP wanted to copy the files, and not move them. So you should replace the mv to cp in the above.
    _______________
    - anaconda

  7. #7
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: Command to copy files in subfolders to one folder

    Quote Originally Posted by anaconda View Post
    I think the OP wanted to copy the files, and not move them. So you should replace the mv to cp in the above.
    oops - corrected

  8. #8
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Command to copy files in subfolders to one folder

    Quote Originally Posted by a2z View Post
    If your folder name has a space, you have to substitute it with * . Meaning DON'T use spaces when dealing with folders.
    Sorry I think you are mistaken.
    Fear is the mindkiller....
    The little death that obliterates...

  9. #9
    Join Date
    Mar 2009
    Location
    Florida
    Beans
    97
    Distro
    Ubuntu Jaunty Jackalope (testing)

    Re: Command to copy files in subfolders to one folder

    Quote Originally Posted by airtonix View Post
    Sorry I think you are mistaken.
    I've moved all my folders from cd's to my 500g hd. This is the only way it worked. So I guess believe what you wish. I know what worked for me hehehe
    www.webstore.com/~a2zHere
    If you didn't learn something one day, you didn't do anything that day. Those that don't do anything one day, don't want to know anything.

  10. #10
    Join Date
    Dec 2007
    Location
    The last place I look
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Command to copy files in subfolders to one folder

    looks like you already have some good answers to the query, but figured I'd chime in as well. here is my python script for almost the same thing. it walks all subdirs in a dir, and causes all the files within them to erupt into the base dir.

    Code:
    #!/usr/bin/env python
    #usage: volcano.py <basedirpath>
    
    import os, sys, shutil
    
    
    def main():
        for a in sys.argv:
            arg = os.path.normpath(a)
            for root, dirs, files in os.walk(arg, False):
                for f in files:
                    src = os.path.join(root, f)
                    fname =  os.path.split(src)[1]
                    try:
                        shutil.move(src, os.path.join(arg, fname))
                        print "moved %s to %s" %(src, os.path.join(arg, fname))
                    except Exception, e:
                        print "Errpr moving file %s to %s" %(src, os.path.join(arg, fname))
                        print "Exception: ", e 
    
    if __name__ == "__main__":
        main()
    Things are rarely just crazy enough to work, but they're frequently just crazy enough to fail hilariously.

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
  •