+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
Scripts Thread, bash script doesnt like folders with spaces in Coding and Web Development; Ive written a little bash script that sorts photos out of the sub folders and puts them all into the ...
  1. #1

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199

    bash script doesnt like folders with spaces

    Ive written a little bash script that sorts photos out of the sub folders and puts them all into the root folder, but my script doesnt seem to like folders with spaces. I think its the for loop, thinking the space means its a new item and it treats "Summer 2009" as Summer and 2009.

    Anyone point me in the right direction to sort this?


    Code:
    cd /Photos/new
    
    #Move JPG files
    
    for directory in $(find . -type d)
    do (cd $directory
    for file in *.jpg
    
    do mv $file /Photos/new
    echo "Moved $file "
    done)
    
    done
    
    #Remove empty directorys
    find . -type d -exec rmdir {} \;

  2. #2

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,675
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    you need to escape them like so
    Code:
    mkdir new\ folder
    This creates a directory called "new folder"
    Note the \ is right after word "new"

    so to cd into "Summer 2009"

    Code:
    cd Summer\ 2009

  3. #3
    Arcath's Avatar
    Join Date
    Feb 2009
    Location
    Lancashire
    Posts
    972
    Thank Post
    102
    Thanked 116 Times in 101 Posts
    Rep Power
    74
    try:

    chaning $directory on line 6 to
    Code:
    ${directory/ /\\ }
    that should subsitute all the spaces in $directory to "\ "

    you will have to do it for file name aswell because "random picture.jpg" will be treated as "random" and "picture.jpg"

  4. #4

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    Quote Originally Posted by Gatt View Post
    you need to escape them like so
    Code:
    mkdir new\ folder
    This creates a directory called "new folder"
    Note the \ is right after word "new"

    so to cd into "Summer 2009"

    Code:
    cd Summer\ 2009
    Thanks for the suggestion, but Im not making the folders, and also they arent being made via command line or on linux for that matter.

  5. #5

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    Quote Originally Posted by Arcath View Post
    try:

    chaning $directory on line 6 to
    Code:
    ${directory/ /\\ }
    that should subsitute all the spaces in $directory to "\ "

    you will have to do it for file name aswell because "random picture.jpg" will be treated as "random" and "picture.jpg"
    I tried that, it didnt seem to work. Same error as before

    Code:
     cd: 2009: No such file or directory
    mv: cannot stat `*.jpg': No such file or directory

  6. #6

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Just wrap your variable in quotes:

    Code:
    mv "$directory"
    Bash does greedy matching for variables AFAIK, but doesn't do regular expressions.

  7. #7

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    I tried that before as well powdarmonkey thinking it would do the trick, but it didnt work either.

  8. #8
    Arcath's Avatar
    Join Date
    Feb 2009
    Location
    Lancashire
    Posts
    972
    Thank Post
    102
    Thanked 116 Times in 101 Posts
    Rep Power
    74
    Quote Originally Posted by RabbieBurns View Post
    Code:
    mv: cannot stat `*.jpg': No such file or directory
    That seems to imply that its taking "*" as a literal character, which athough isnt your current problem, might come up later

  9. #9

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Ah, wait, I misread your question. Try this:

    Code:
    for directory in $(find . -type d)
    do (cd $directory
    for file in `ls *.jpg`
    
    do mv $file /Photos/new
    ....
    This shells out to ls to get a list of .jpg files and works off that, instead of using the string literal '*.jpg'.

  10. #10

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    Arcath the problem isnt with the *.jpg, as it works fine with non spaced directories

    The problem is the script cant change directory into a directory with a space, and therefore there are no jpg files for it to move, as the folder it tries to change into doesnt exist

    Edit: The folder name as is "Summer 2009" so the full error it gives is:


    cd: Summer: No such file or directory
    mv: cannot stat `*.jpg': No such file or directory
    cd: 2009: No such file or directory
    mv: cannot stat `*.jpg': No such file or directory

    (sorry should have posted the full error more clearly)
    Last edited by RabbieBurns; 12th February 2009 at 10:17 AM.

  11. #11

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    You might also find this easier (note: untested) as I think all you're trying to do is find .jpg files in a tree, and move them all into one place.

    Code:
    find . -name *.jpg -type f -print0 | xargs -0 mv '{}' /Photos/new
    One line to rule them all, one line to find them, one line to bring them all and in the darkness replace your whole script

    Your friends are 'man find' and 'man xargs'.

  12. Thanks to powdarrmonkey from:

    RabbieBurns (12th February 2009)

  13. #12

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    Quote Originally Posted by powdarrmonkey View Post
    You might also find this easier (note: untested) as I think all you're trying to do is find .jpg files in a tree, and move them all into one place.

    Code:
    find . -name *.jpg -type f -print0 | xargs -0 mv '{}' /Photos/new
    One line to rule them all, one line to find them, one line to bring them all and in the darkness replace your whole script

    Your friends are 'man find' and 'man xargs'.
    "I think all you're trying to do is find .jpg files in a tree, and move them all into one place" <-- Yep thats exactly what Im attempting.

    Im not quite sure what this error means?

    Code:
    roberts Photos # find . -name *.jpg -type f -print0 | xargs -0 mv '{}' /Photos/new
    mv: target `./new/Summer 2009/summer2.jpg' is not a directory

  14. #13

    Join Date
    Apr 2006
    Posts
    138
    Thank Post
    0
    Thanked 19 Times in 10 Posts
    Rep Power
    38
    I would do the obvious, which is to rename your directory to something like summer_2009.

  15. #14

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,527
    Thank Post
    1,339
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    199
    But if my script cant CD into a directory with a space, how will it recognize a directory with a space to change the space to _ ?

  16. #15


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    does this work?

    Code:
    find . -type f -name "*.jpg" -exec mv {} ./new/path/  \;

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Script to share multiple folders
    By originofsymmetry in forum Scripts
    Replies: 8
    Last Post: 25th August 2008, 11:11 PM
  2. Script to delete specific folders
    By Baasha_Bhai in forum Scripts
    Replies: 8
    Last Post: 2nd July 2008, 10:41 AM
  3. VB script to delete certain folders
    By PTD in forum Scripts
    Replies: 7
    Last Post: 4th June 2008, 12:30 PM
  4. Script to delete certain folders
    By Pumaedition in forum Scripts
    Replies: 4
    Last Post: 22nd April 2008, 10:08 PM
  5. VB Script to create folders
    By markesmith in forum Scripts
    Replies: 2
    Last Post: 26th April 2007, 03:03 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •