Programming and Scripting :: Need help with bash



for i in [1-9].jpg; do mv $i 0$i; done

No sissy pipes need apply :p

Very nice!  Small, simple, efficient, and it does what you want.

I'll try to spend less time playing with my pipes  :;):

mikshaw,

I thought you would write it as:
for i in [1-9].jpg; do mv {,0}$i; done

Like your tip in the post: Bash, Tips for commandline and shell scripts

But great syntax...... :)

NotTheMama, thanks for that link.  I was looking for it before but couldn't find it.

So what's the best way to reverse the process?  Remove the leading zero?  I had a complicated mess until I looked at the link from NotTheMama and the Advanced Bash-Scripting Guide and tossed in some parameter substitution.  But I'd bet there's a simpler way:

for i in 0[1-9].jpg; do mv $i ${i#0}; done

I wish I knew all this before when I was renaming files by stripping away the first part (always the same) and it took forever using the rename button in emelfm.

NotTheMama: I would have if the filenames were longer, but "mv {,0}$i" doesn't save any bytes or keystrokes from "mv $i 0$i" when you're working with single-character filenames. It's actually one additional stroke, plus an extra shift, and it's more difficult to read =o)
I'm glad people appreciate my input....i certainly appreciate all the knowledge I get from others here.

Clacker:  I can never remember how the string substitution works.  I recently bugged RobertS to help me out, but i still have to refer to the ABS guide to figure out when to use # or ## or %, etc

Next Page...
original here.