tags:

views:

654

answers:

9

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?

+1  A: 
sed -e 's:^0*::'

Complete loop:

for f in `ls`; do
   mv $f $(echo $f | sed -e 's:^0*::')
done
eduffy
How do I pass the directory contents into this command?
George
updated the post
eduffy
This is probably good enough for George's situation, but in a different environment one might wish to handle the special case where the filename is all zeroes.
Jim Lewis
There is absolutely no reason to use `for f in \`ls\`` instead of the much safer and more efficient `for f in *`.
ephemient
A: 

Maybe not the most elegant but it will work.

for i in 0*
do
mv "${i}" "`expr "${i}" : '0*\(.*\)'`"
done
jml3310
Your formatting needs work. Use 4 spaces before 'for' to make the whole line a coded section.
bradlis7
A: 

I dont know sed at all but you can get a listing by using find:

find -type f -name *.jpg

so with the other answer it might look like

find . -type f -name *.jpg | sed -e 's:^0*::'

but i dont know if that sed command holds up or not.

prodigitalson
A: 
for FILE in `ls`; do mv $FILE `echo $FILE | sed -e 's:^0*::'`; done
cyborg
You don't need to (and shouldn't) use `ls` like this. Do it this way: `for FILE in *`
Dennis Williamson
this is a bad example. first using ls, and then never quote your variables
ghostdog74
I don't see any problem with using ls. You can substitute it for the find command suggested below or any other command.Since it works I don't really see what your justifications are other than stylistic, which when it comes to one line shell scripts seems a little pointless.
cyborg
* will do the right thing, `ls` sometimes will not (for example if a file has a space in its name, this script will not work).
Justin Smith
My experience has not shown that sort of behaviour. If specifying the list myself the items will need to be separated by a line break.
cyborg
+1  A: 

sed by itself is the wrong tool for this: you need to use some shell scripting as well.

See http://www.go2linux.org/rename-bulk-files-with-linux-console-command for some ideas. One of the ideas suggested is to use the rename perl script:

rename 's/^0*//' *.jpg

(N.B. untested)

Simon Nickerson
I love that command, but unfortunately it's not installed on every system :(.
Kaleb Pederson
A: 

Here's one that doesn't require sed:

for x in *.jpg ; do let num="10#${x%%.jpg}"; mv $x ${num}.jpg ;  done

Note that this ONLY works when the filenames are all numbers. You could also remove the leading zeros using the shell:

for a in *.jpg ; do dest=${a/*(0)/} ; mv $a $dest ; done
Kaleb Pederson
+2  A: 

In Bash, which is likely to be your default login shell, no external commands are necessary.

shopt -s extglob
for i in 0*[^0]; do mv "$i" "${i##*(0)}"; done
ephemient
A: 

@OP, in bash

shopt -s nullglob
for file in 0*.jpg
do
   echo mv "$file" "${file##*0}"
done
ghostdog74
A: 

Hi all, I need the exactly opposite. I've filenames like 1.jpg , 101.jpg, 2.jpg, 20.jpg, 1001.jpg I would like to add the leading zeros to order alfabetically the files. I nees such a basj script that read the filename, count the number of digits, adn add the remaining zeros to reach the decided number of total digits (ex 101.jpg->0101.jpg, 2.jpg-> 0002.jpg) Could you kindly help me? Thks Fabio

Fabio
eg use `printf "%04d" "1"` . also you should start a new question
ghostdog74