Other Renaming files with whitespaces using bash and sed

I have files with names like this that I am trying to rename.
Code:
10 - 88JMtZq.jpg
17 - wkyjt7A.jpg
24 - sFDt4Hr.jpg
What I have tried is using a script that looks like this
Code:
#!/usr/local/bin/bash
for i in `ls *.jpg`; do
    mv $i $(echo $i | sed 's/[0-9][0-9] \- //')
done
I am certain that the problems with the script are due to whitespaces based on the output given and a bit of sense. I would love to learn the correct way to solve the problem using a script with sed.
 
The problem description is not very clear. so I am guessing what you want to do.

Install sysutils/rename and if all you want to do is strip e.g. 10 - of the filenames, this might work:
rename -t -s'/[0-9]+ - //e' *.jpg. Remove the -t (test mode) switch to actually rename the files after you have verified that the output is ok.
 
The problem description is not very clear. so I am guessing what you want to do.

Install sysutils/rename and if all you want to do is strip e.g. 10 - of the filenames, this might work:
rename -t -s'/[0-9]+ - //e' *.jpg. Remove the -t (test mode) switch to actually rename the files after you have verified that the output is ok.
Yes, sorry. This does work. And I was trying something like this before
rename '-s/[0-9][0-9] \- //' *.jpg, and it wasn't working for me.

However I would also like to know how to do this in a script with bash and sed.
 
Yes, sorry. This does work. And I was trying something like this before
rename '-s/[0-9][0-9] \- //' *.jpg, and it wasn't working for me.

However I would also like to know how to do this in a script with bash and sed.
With sed it's pretty much the same. You just need to escape the spaces. Here's an example:
Code:
echo "10 - fasdf.jpg" | sed -e 's/^[0-9][0-9]\ -\ //'
or
Code:
echo "10 - fasdf.jpg" | sed -Ee 's/^[0-9]+\ -\ //'
 
You need to quote the variables:
Code:
#!/usr/local/bin/bash
for i in `ls *.jpg`; do
    mv "$i" $(echo "$i" | sed 's/[0-9][0-9] \- //')
done
 
Yeah, without the quotes in "$i" the shell performs white space splitting according to $IFS and that breaks the argument $i into many smaller arguments if it contains any $IFS characters.

sh(1)
 
Code:
for i in `ls *.jpg`; do
can be simplified to
Code:
for i in *.jpg; do

Quoting and regular expressions in the shell are a mess. If allowed, one of the scripting languages like Perl or Python can make this much easier and less error-prone.
 
  • Thanks
Reactions: a6h
I have been having a similar problem.

I have been trying to run the same scripts that I created on an iMac, on my FreeBSD box.

The iMac has no problems with white spaces in file names, FreeBSD has a big problem with that.

IMO: this is a significant problem with FreeBSD. FreeBSD seems to be the only modern *NIX system that cannot handle white spaces.
 
From the command line, I cannot seem to mv, cp, rm, or do much of anything else with a file that has white spaces in the name. For example, let's suppose I have a file named one two three and I try to copy it to a directory:

# cp one two three newdir/

FreeBSD will be looking for three seperate files name one two and three

Using an asterix does not seem to work either.

# cp one* newdir/

BTW: I have never liked the idea of white spaces in file names. But sometimes I have to work with files that other people have named.
 
What about using single quotes? (or if you use bash or tcsh, it may autocomplete) From command line, with either tcsh or bash
cp one (then hit the tab key, or you might need to do one\ <space key once>. Hrrm sh might also autocomplete these days, but generally, as a user I use bash.

Otherwise cp 'one two three' might work or cp one\ two\ three.

And yes, as most Windows and Mac users put spaces in file names, it can be difficult. I remember a job many years ago, where, perhaps due to hitting the space bar by habit, we had files with names like 123.jpg and 123.jpg<whitespace>. Eventually, I think I used something list ls *|cat -e which puts a $ at the end, so I could see one file 123.jpg$ and 123.jpg $. I suspect all admins, including Windows admins, dislike spaces in file names.
 
Back
Top