4

The title says it all and yes I know there are many questions similar, but the closest I could find was removing " and not '. What I've tried so far is:

find -name "*'*" -type f | rename 's/\'//g'

or rename 's/[']//g' and some other variations. I find all the files, but when I'm piping it to rename, I'm just getting >for prompt and have to Ctrl+C to return. I've recently changed a lot of files with this, and it has worked like a charm on any special character, except the single quotes.

2 Answers 2

8

You can use double quotes:

rename -n "s/'//g"

remove -n after testing to rename the files for real

Use -exec though...

find -name "*'*" -type f -exec rename -n "s/'//g" '{}' \;

If the list is not too long, make it faster...

find -name "*'*" -type f -exec rename -n "s/'//g" '{}' +

Or, as suggested in comments, use recursive shell globbing:

shopt -s globstar
rename -n "s/'//g" **/*"'"*

You can use shopt -u globstar to turn recursive globbing with ** off, but it will be off anyway when you start a new shell.

13
  • The rename command has its own file globbing syntax, so there's likely no need for the find command here. Sep 5, 2020 at 1:13
  • @TomRussell oh really? I don't know about that - I use the shell to glob with rename (there is globstar which would presumably work here). Would you care to post an answer to demonstrate?
    – Zanna
    Sep 5, 2020 at 2:04
  • The final parameter is the standard shell globbing: Usage: rename [options] <expression> <replacement> <file>... Sep 6, 2020 at 9:51
  • Oops. I neglected to consider the recursive aspect of the OP's question. Sep 8, 2020 at 5:18
  • @TomRussell that output looks like it comes from the other rename (rename.ul) command to me. I am not familiar with that command, but I did not find anything about rename having its own globbing syntax (the way find does). Bash has globstar which makes ** recursive, but one has to quote the single quote again... shopt -s globstar; rename -n "s/'//g" **/*"'"* is working for me in Bash 5.0.17
    – Zanna
    Sep 8, 2020 at 5:33
0

This works with bash on my system (OpenSUSE Leap):

$ rename "'" '' **

The 3 args to rename are: "'", '' and ** (globstar), telling it to replace the quote with the empty string and do so recursively.

Note that ** (globstar) is turned on by default on my system. I'm not aware of a way to turn it off.

You must log in to answer this question.

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