Remove hyphens from filename with Bash
Asked Answered
C

4

5

I am trying to create a small Bash script to remove hyphens from a filename. For example, I want to rename:

CropDamageVO-041412.mpg

to

CropDamageVO041412.mpg

I'm new to Bash, so be gentle :] Thank you for any help

Charbonneau answered 15/4, 2012 at 0:56 Comment(0)
B
15

Try this:

for file in $(find dirWithDashedFiles -type f -iname '*-*'); do
  mv $file ${file//-/}
done

That's assuming that your directories don't have dashes in the name. That would break this.

The ${varname//regex/replacementText} syntax is explained here. Just search for substring replacement.

Also, this would break if your directories or filenames have spaces in them. If you have spaces in your filenames, you should use this:

for file in *-*; do
  mv $file "${file//-/}"
done

This has the disadvantage of having to be run in every directory that contains files you want to change, but, like I said, it's a little more robust.

Blooper answered 15/4, 2012 at 1:1 Comment(2)
If there might be spaces in the filename, you need to double-quote $file (i.e. mv "$file" "${file//-/}")Inviting
Don't parse the output of find, and certainly not with a for loop: as is, your statement breaks with spaces or glob characters in file names. If you want to stick with find use a more robust method, e.g., find dirWithDashedFiles -type f -name '*-*' -exec bash -c 'mv "$f" "${f//-}"' - {} \; (note that the trailing slash is optional when the substitution string is empty).Yangyangtze
C
8
FN=CropDamageVO-041412.mpg
mv $FN `echo $FN | sed -e 's/-//g'`

The backticks (``) tell bash to run the command inside them and use the output of that command in the expression. The sed part applies a regular expression to remove the hyphens from the filename.

Or to do this to all files in the current directory matching a certain pattern:

for i in *VO-*.mpg
do
    mv $i `echo $i | sed -e 's/-//g'`
done
Countdown answered 15/4, 2012 at 0:58 Comment(5)
This does have the advantage of general Posix-shell compatibility. I'm never sure if I should drink all of the bash kool-aid.Clawson
Indeed, that's how I got into the habit of avoiding the fancy bash-isms, to the extent that I don't really use them even now when I rarely use antique shells. Solaris and AIX have a lot to answer for...Countdown
Wow, that was a quick reply. I should have specified that I was trying to remove the hyphens in numerous files, in the format of: [NAME]VO-[DATE].mpgCharbonneau
No sooner said than done. If you want to learn more about bash, tldp.org/LDP/abs/html is a good guide, although rather dry.Countdown
So perfect! I appreciate it so much! You just made my job much easierCharbonneau
K
5

A general solution for removing hyphens from any string:

$ echo "remove-all-hyphens" | tr -d '-'
removeallhyphens
$
Kathiekathleen answered 29/11, 2020 at 9:55 Comment(0)
C
4
f=CropDamageVO-041412.mpg
echo "${f//-}"

or, of course,

mv "$f" "${f//-}"
Clawson answered 15/4, 2012 at 1:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.