There are many files in the folder with the names in the form of numbers in parentheses:

(123).txt (757823).txt 

Tell me how to massively remove parentheses from file names? To become:

 123.txt 757823.txt 
  • one
    Decisions through GUI will suit you? I perform massive operations with file names using the multifunctional free program Bulk Rename Utility . In your case, for me it is the simplest thing - to do this . Thank. - Sasha Chernykh
  • If there are other files in the folder, and not just files with numbers in brackets, then so . If there are additional conditions, add them to the question. Thank. - Sasha Chernykh
  • one
    Thanks to this utility that helped me - Beginner

3 answers 3

So far it has worked out for a long time, but it seems safe from the point of view of special characters:

 find -name '(*).txt' -print | sed 's/$/\x0/;p;s/[()]//g' | tr -d '\n' | xargs -0 -n2 mv 

Attention : before you execute in combat mode, execute the command with replacing mv with echo to check which arguments will be passed to the mv command.

The following happens in the find : find finds all matching files and prints one per line. sed adds a 0-character before the end of the line and prints the line, then deletes all the brackets in the name and prints the line again (default action). After it, tr deletes the carriage returns. xargs executes the specified command, passing in pairs of 2 arguments from the input stream, separated by a 0-character. In this mode, xargs guarantees the correct transfer of parameters with any special characters (spaces and quotes)

  • replies mv: missing file operand - Beginner
  • but if replaced with echo, then nothing answers - Beginner
  • @ Novice This error occurs if there is not a single file in the directory to be renamed. Try any commands in parts, first you want to find find exactly the files you need, then feed the result to sed and check what each stage produces - Mike
 #!/bin/bash for f1 in `find /temp -name '*'`; do f2=${f1/(/} f2=${f2/)/} echo "$f1 -> $f2" mv $f1 $f2 done 
  • Answers: find: '/ temp': No such file or directory although the temp folder is there - Beginner

Team

 $ rename 's/[\(\)]//g' *.txt 

renames all .txt files by removing the parentheses.

  • hmm judging by the description of rename, it should do it. But on my system for some reason it does not rename. Do you work it out like this? - Mike
  • @Mike, yes, it works. - zombic
  • 3
    I figured it out. It turns out there are two kinds of rename. one of the util-linux package, which is one of my own and which cannot do this. And the other one comes with perl (in fact, it is generally a link to the perl executable file), so she understands this syntax - Mike
  • @Mike, it's still worse: in debian, in addition to util-linux, there is also the rename package. it generally makes sense to call not rename, but more specifically prename. then the chances of running into the wrong implementation are less. - aleksandr barakin
  • @Mike rename not from util-linux is a small perl script perlmonks.org/?node_id=303814 - Hellseher