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

Thread: Creat folders and copy file inside them

  1. #1
    Join Date
    Apr 2006
    Beans
    127

    Creat folders and copy file inside them

    Hello

    I have many photos inside many folders for example:

    2008-Home
    --1.jpg
    --2.jpg
    2008-Work
    --2.jpg
    --2.jpg
    --2.jpg
    2011-Party
    --1.jpg
    --2.jpg
    --3.jpg

    etc.

    Each folder has jpg photos. I want to batch these folder and go inisde them then create a folder called jpg then copy all photos inside the jpg folder to be like
    2008-Home
    --jpg
    ----1.jpg
    ----2.jpg
    2008-Work
    --jpg
    ----1.jpg
    ----2.jpg
    ----3.jpg
    2011-Party
    --jpg
    ----1.jpg
    ----2.jpg
    ----3.jpg

    How can I do that?
    Last edited by OOzypal; August 14th, 2019 at 05:35 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    UK
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: Creat folders and copy file inside them

    Support, not chat.

    Thread moved to General Help.
    Ubuntu 20.04 Desktop Guide - Ubuntu 22.04 Desktop Guide - Forum Guide to BBCode - Using BBCode code tags

    Member: Not Canonical Team

    If you need help with your forum account, such as SSO login issues, username changes, etc, the correct place to contact an admin is here. Please do not PM me about these matters unless you have been asked to - unsolicited PMs concerning forum accounts will be ignored.

  3. #3
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Creat folders and copy file inside them

    filenames and directory names beginning with dashes (-) will cause issues with some programs. Best to avoid that.

  4. #4
    Join Date
    Apr 2006
    Beans
    127

    Re: Creat folders and copy file inside them

    Sure. But these not (-s). These to show the folder structure.

  5. #5
    &wP*!) is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2006
    Beans
    167

    Re: Creat folders and copy file inside them

    You can do that with a small Perl script very easily. Following Perl code does what you want, but only for 1 hierarchy:
    Code:
    #!/bin/env perl
    
    use Getopt::Long;
    
    $root_dir = ".";
    
    GetOptions (
            "d=s"   => \$root_dir,
    );
    
    print "$root_dir\n";
    
    foreach $dir (glob "$root_dir/*") {
            print "Processing $dir\n";
            if (glob "$dir/*.jpg" == "") { print "The directory $dir has no JPEG files. Skipping...\n"; }
            mkdir "jpg";
            system ("mv ".glob ("$dir/*.jpg")." $dir/jpg");
    }
    Save it as something like mv_jpeg.pl and make it executable by chmod 755 mv_jpeg.pl.
    Now you can call it in any root directory you want:
    Code:
    ./mv_jpeg.pl -d some_dir
    This creates in each subdirectory under "some_dir" a sub-sub-directory called "jpg" and move all files in the subdir with the extension "*.jpg" into this folder.

    You can modify the script in the way you want.
    With little effort it can be recursive.
    Last edited by &wP*!); August 14th, 2019 at 07:32 PM.

  6. #6
    Join Date
    Dec 2014
    Beans
    2,590

    Re: Creat folders and copy file inside them

    The simplest solution would be something like this
    Code:
    for i in list-of-folder-names ; do mkdir "$i"/jpg && mv -- "$i/*[jJ][pP][gG]" "$i"/jpg; done
    executed in a terminal. Replace "list-of-folder-names" with a space-separated list of directories (if any of the folder-names contains space(s) put the name in quotes). This will create a directory named "jpg" inside each of the named folders and move the files whose names end in 'jpg' (in any variation of capital and minor letters) from the original directory into the new directory. If the attempt to make the directory fails, moving isn't tried (that's what the '&&' means). The '--' in the 'mv' command tells 'mv' that there are no further options, so filenames starting with on or more dashes don't lead to problems.

  7. #7
    Join Date
    Apr 2006
    Beans
    127

    Re: Creat folders and copy file inside them

    hab@asus:~/Dropbox/Personal/Multimedia/Photo$ ./my_jpeg.pl -d some_dir

    /bin/env: ‘perl\r’: No such file or directory

  8. #8
    &wP*!) is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2006
    Beans
    167

    Re: Creat folders and copy file inside them

    Quote Originally Posted by OOzypal View Post
    hab@asus:~/Dropbox/Personal/Multimedia/Photo$ ./my_jpeg.pl -d some_dir

    /bin/env: ‘perl\r’: No such file or directory
    There must be no other character after the text perl. I do see "\r" in your text. Don't use weird whitespace characters. Just type enter after the 1st line (it should go to the next line in the editor).
    If it is still the same problem, change:
    Code:
    #!/bin/env perl
    with
    Code:
    #!/usr/bin/perl
    ... and run the script again. If this does not work either, try this (no matter how 1st line looks like) - change "some_dir" with the top directory name !! -:
    Code:
    perl mv_jpeg.pl -d some_dir
    If this doesn't work, check whether you have Perl:
    Code:
    which perl
    If this doesn't return anything, it means to me that you don't have Perl installed on your computer. Install it with the following command:
    Code:
    sudo apt-get install perl
    afterwards even /bin/env solution should work.
    Last edited by &wP*!); August 14th, 2019 at 10:11 PM.

  9. #9
    Join Date
    Apr 2006
    Beans
    127

    Re: Creat folders and copy file inside them

    I did remove the extra character and I am getting nothing except the some_dir is re-printed in the terminal.

  10. #10
    Join Date
    Apr 2006
    Beans
    127

    Re: Creat folders and copy file inside them

    Quote Originally Posted by Holger_Gehrke View Post
    The simplest solution would be something like this
    Code:
    for i in list-of-folder-names ; do mkdir "$i"/jpg && mv -- "$i/*[jJ][pP][gG]" "$i"/jpg; done
    executed in a terminal. Replace "list-of-folder-names" with a space-separated list of directories (if any of the folder-names contains space(s) put the name in quotes). This will create a directory named "jpg" inside each of the named folders and move the files whose names end in 'jpg' (in any variation of capital and minor letters) from the original directory into the new directory. If the attempt to make the directory fails, moving isn't tried (that's what the '&&' means). The '--' in the 'mv' command tells 'mv' that there are no further options, so filenames starting with on or more dashes don't lead to problems.
    True it is simple but it is not practical. I can't list so many directories in one line. In addition, it didn't work. It created a jpg folder in each directoy but nothing else with an error-see attached.

    Screenshot from 2019-08-15 11-18-09.png

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
  •