Question

I have a bunch of sequentially named files in this format: imageXXX.jpg. So it would be like image001.jpg and onward. I just want to keep the number part of this, and get rid of the prepended 0's. So instead, that file would be named 1.jpg. How could I achieve this using Bash?

Was it helpful?

Solution

On Linux the venerable Perl utility rename is friendly:

$ rename 's/^image0+//' image*.jpg

You should be aware that stripping leading zeros will ruin the sort order, that is *.jpg orders like:

1.jpg
10.jpg
11.jpg
...
2.jpg
20.jpg

If you want to keep the order just use

$ rename 's/^image//' image*.jpg

instead.

added in response to system identification

You can likely script it in bash alone, but it would be non-trivial and the failure cases really need to be handled correctly. Yeah, hoisting Perl onto a system is non-trivial too, but it is easy and that wheel's already been invented

Fedora Core 8 Perl RPM: http://rpm.pbone.net/index.php3/stat/4/idpl/5152898/dir/fedora_8/com/perl-5.8.8-30.n0i.51.fc8.i386.rpm.html
CPAN rename: http://metacpan.org/pod/File::Rename

added in response to silent failure

rename like chmod will complain if you give it malformed arguments, but both are silent if what you request has no effect. For example

$ ls -l junk
-rw-r--r-- 1 msw msw 0 2010-09-24 01:59 junk
$ chmod 688 junk
chmod: invalid mode: '688'
$ chmod 644 junk    # was already 644 mode, nothing happened no error
$ rename 's/bob/alice/' ju*k    
# there was no 'bob' in 'junk' to substitute, no change, no error
$ ls -l junk
-rw-r--r-- 1 msw msw 0 2010-09-24 01:59 junk
$ rename 's/un/ac/' j*k  # but there is an 'un' in 'junk', change it
$ ls -l j*k
-rw-r--r-- 1 msw msw 0 2010-09-24 01:59 jack

You can make rename less silent:

$ rename --verbose 's/ac/er/' j*k
jack renamed as jerk
$ rename --verbose 's/ac/er/' j*k   # nothing to rename
$                  

OTHER TIPS

Pure Bash:

shopt -s extglob
for f in image*.jpg; do mv "$f" "${f/#image*(0)}"; done

Additional code could check for name collisions or handle other error conditions. You could use mv -i to prompt before files are overwritten.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top