user441470
user441470

Reputation: 11

bash scripting - selectively handle file names with spaces

I have a directory with the files names as "a b c.jpg", "d e f 0.jpg", "g h i.jpg"

I need a script to have all the files ending with "0.jpg" to become "_0.jpg"

So, in the above example the second file should become "d e f_0.jpg"

Upvotes: 0

Views: 655

Answers (3)

camh
camh

Reputation: 42538

I think your question should read that you want files ending with " 0.jpg" to become "_0.jpg" (note the space in the first quotes). That makes sense with your example.

for i in *\ 0.jpg ; do
    mv -- "$i" "${i/ 0.jpg/_0.jpg}"
done

That is, for every file matching the pattern "* 0.jpg", rename it replacing " 0.jpg" with "_0.jpg"

Edit: For added safety, consider using -n (no-clobber) or -i (interactive) as an option to mv(1).

Upvotes: 5

tdammers
tdammers

Reputation: 20706

The rename tool might be what you need.

Upvotes: 1

Teodor Pripoae
Teodor Pripoae

Reputation: 1394

ls -1 | grep .jpg | awk -F "." '{print $1 "_0." $2}'

Upvotes: -3

Related Questions