0

I would like to remove []-_" etc. from multiple file names that are situated in different subdirectories.

Found this

rename "s/\[|\]/_/g" * 

but it's working only in a specific subdirectory and not for other files that are elsewhere

Is there way to do this?

  • If you just want to rename all files in all direct subdirectories of the current directory, try the shell glob */* instead: rename "s/\[|\]/_/g" */* – Byte Commander Aug 9 '17 at 23:29
  • thank u you for your reply but could you tell me exactly what i need to type in the terminal? – Boris Cohaniuc Aug 9 '17 at 23:30
  • 1
    I think I just did that. What exactly is unclear? You said you already tried the command in your question, just replace * with */* there, as I did in my comment. Type that while you are in the parent directory of those inside which you want to rename the files. – Byte Commander Aug 9 '17 at 23:34
1

If you want to apply the renaming recursively to all files within the directory hierarchy under your current working directory, you can use Bash's globstar feature. You need to activate this first. Type the following:

shopt -s globstar
rename "s/\[|\]/_/g" **

In other words, you just to activate the globstar feature, then you can run your command with two stars (**) instead of only one (*). This will match all files in the directory hierarchy under the current working directory, i.e., regardless of how deeply they are nested under the current working directory.

If you only want to rename all files under the current working directory without the risk of renaming matching directory names too, you can use find instead:

find . -type f -execdir rename -- 's/\[|\]/_/g' {} +

This will match all files containing the characters [ or ] anywhere in the directory structure under your current working directory, then run your renaming routine on the subdirectories containing any matching files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.