8

I know how to replace a set of characters in filenames. E.g

for i in *example_text* ;
do
    mv "$i" "`echo $i | sed 's/example_text//'`"
done

How do I do so if a part of the filename is ";" (between quotes). I get an error whenever I try to run:

for i in *;* ;
do
    mv "$i" "`echo $i | sed 's/;//'`"
done

Any tips?

2 Answers 2

18

While using mv in a loop will work, I suggest to use perl rename instead (it might be installed as perl-rename or prename, depending on your operating system). It is a tool made exactly for your purpose and is very likely to be installed on your system already.

rename 's/;//g' ./* will do the trick,
you can review what would be done with a dry-run first: rename -v -n 's/;//g' ./*

1
  • Thanks for the edits Stéphane :-)
    – markgraf
    Commented Feb 26, 2022 at 11:19
13

Escape the semicolon, it's a special character for the shell.

for i in *\;*
# or
for i in *';'*

If you're using bash, parameter expansion is much faster than shelling out to sed.

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

Note that it can overwrite a file if it already exists. Use mv -n or check for existence with test -e.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .