PDA

View Full Version : strip hyphens from all files in dir



ZenMasta
March 2nd, 2013, 12:20 AM
I have a folder with about 5700 files. The files are all named numerically but there are multiple dashes in each name

ie 978-1-234-5678-9.jpg

I want to completely remove the hyphen from all the file names.

I tried this but it doesn't appear to do anything.
find media/images/ -type f -exec sed -i 's/-//g' {} \;


Any ideas?

steeldriver
March 2nd, 2013, 12:27 AM
'sed' is for editing the contents of the found files - you can either use the 'mv' command within a 'find', or just use the 'rename' command e.g.


rename -n -v 's/-//g' *.jpg
978-1-234-5678-9.jpg renamed as 978123456789.jpg

(the '-n' means dry run - remove it once you are certain it's matching filenames correctly)

LHammonds
March 2nd, 2013, 12:31 AM
Not 100% sure if this would work but try this:



for file in *.jpg
do
echo "${file}" "`echo ${file} | sed 's/-//g'`"
done


If that looks correct, change "echo" to "mv" to perform the rename.

LHammonds

steeldriver
March 2nd, 2013, 12:41 AM
^^^ if you want to use mv in a loop instead of a one-shot rename, you could use a builtin bash substitution directly instead of sed though:-



for file in *.jpg
do
echo mv "${file}" "${file//-/}"
done

The Cog
March 2nd, 2013, 09:42 PM
I have a folder with about 5700 files. The files are all named numerically but there are multiple dashes in each name

ie 978-1-234-5678-9.jpg

I want to completely remove the hyphen from all the file names.

I tried this but it doesn't appear to do anything.
find media/images/ -type f -exec sed -i 's/-//g' {} \;


Any ideas?

Aargh!!! Noooooo!!!
That will corrupt all your images! It will remove the any '-' characters from the file contents. I hope you have a backup of all these images.