Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash command to remove leading zeros from all file names

I have a directory with a bunch of files with names like:

001234.jpg
001235.jpg
004729342.jpg

I want to remove the leading zeros from all file names, so I'd be left with:

1234.jpg
1235.jpg
4729342.jpg

I've been trying different configurations of sed, but I can't find the proper syntax. Is there an easy way to list all files in the directory, pipe it through sed, and either move or copy them to the new file name without the leading zeros?

like image 201
George Avatar asked Jan 15 '10 20:01

George


1 Answers

for FILE in `ls`; do mv $FILE `echo $FILE | sed -e 's:^0*::'`; done
like image 56
cyborg Avatar answered Oct 31 '22 00:10

cyborg