RabbieBurns (12th February 2009)

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 {} \;

you need to escape them like so
This creates a directory called "new folder"Code:mkdir new\ folder
Note the \ is right after word "new"
so to cd into "Summer 2009"
Code:cd Summer\ 2009
try:
chaning $directory on line 6 to
that should subsitute all the spaces in $directory to "\ "Code:${directory/ /\\ }
you will have to do it for file name aswell because "random picture.jpg" will be treated as "random" and "picture.jpg"



Just wrap your variable in quotes:
Bash does greedy matching for variables AFAIK, but doesn't do regular expressions.Code:mv "$directory"

I tried that before as well powdarmonkey thinking it would do the trick, but it didnt work either.

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

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 11:17 AM.

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.
One line to rule them all, one line to find them, one line to bring them all and in the darkness replace your whole scriptCode:find . -name *.jpg -type f -print0 | xargs -0 mv '{}' /Photos/new
Your friends are 'man find' and 'man xargs'.
RabbieBurns (12th February 2009)

"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
I would do the obvious, which is to rename your directory to something like summer_2009.

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 _ ?

does this work?
Code:find . -type f -name "*.jpg" -exec mv {} ./new/path/ \;
There are currently 1 users browsing this thread. (0 members and 1 guests)