Results 1 to 4 of 4

Thread: move or copy bulk files with spaces from many dir. to one dir.

  1. #1
    Join Date
    Oct 2005
    Location
    Wabasha MN
    Beans
    2,355
    Distro
    Ubuntu

    move or copy bulk files with spaces from many dir. to one dir.

    OK, I have a directory with 439 directory and all have spaces in the names. Now I move all to a directory name temp in my home directory. All have files with extensions (epub, pdf, jpg, opf) Now all I want is the .epub files from all the directories and I want to move or copy them to a directory named temp2 in my home directory.
    Now I wrote a small script file to do this, but I get errors that none of the directories or files can be found and that is because of the spaces in the names. Here is my script file. What do I need to change to make it work.
    Code:
    #!/bin/bash
    
    main_search_dir="/home/irv/temp/";
    save_dir="/home/irv/temp2/";
    extention="epub";
    
    cp `find $main_search_dir -name "*.$extention"` $save_dir
    My hometown on the Mississippi River.
    Using a Asus 3632QM laptop with 8gig RAM, 750gig Hybrid.
    Machine Registered 366271, 366273, 366275.
    Registered Ubuntu user number 18630. Registered Linux user number 458093.

  2. #2
    Join Date
    Apr 2012
    Beans
    6,697

    Re: move or copy bulk files with spaces from many dir. to one dir.

    This is the way I've learned to do this kind of thing (from the wiser folks on here):

    Code:
    while read -r -d $'\0' file; do echo cp "$file" "$save_dir"; done < <(find "$main_search_dir" -name "*.$extention" -print0)
    Take out the 'echo' once you've checked it's doing the right thing

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: move or copy bulk files with spaces from many dir. to one dir.

    Please check out BashFAQ 020 (link in my signature).

    I'd try something like:
    Code:
    find "$m_s_d" -name "*.$e" -exec cp -t "$s_d" {} +

  4. #4
    Join Date
    Oct 2005
    Location
    Wabasha MN
    Beans
    2,355
    Distro
    Ubuntu

    Re: move or copy bulk files with spaces from many dir. to one dir.

    OK I am going to mark this solved. Here is what I did.
    I created two directories, "temp" and "temp2". Then I created a script file cpepubfiles.sh and put it in /home/irv/temp/
    Code:
    #!/bin/bash
    
    main_search_dir="/home/irv/temp/";
    save_dir="/home/irv/temp2/";
    extention="epub";
    
    find "$main_search_dir" -name "*.$extention" -exec cp -t "$save_dir" {} +
    Then I copied all the directories with the .epub files in them to the temp directory.
    Then I open a terminal and changed to the temp directory and ran
    Code:
    . cpepubfiles.sh
    It copied all the epub files from all the directories to /home/irv/temp2.
    Worked great.
    Thanks for all the help.
    My hometown on the Mississippi River.
    Using a Asus 3632QM laptop with 8gig RAM, 750gig Hybrid.
    Machine Registered 366271, 366273, 366275.
    Registered Ubuntu user number 18630. Registered Linux user number 458093.

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
  •