Using the Perl rename
utility, if available,
find . -type f -name '* (*)*' -exec rename 's/ \([^)]*\)//g' {} +
This would find all regular files in or below the current directory that have at least one matched set of parentheses in their names. For batches of such files, rename
is used to rename the files by removing the parentheses that we know are there,
including the leading space before the left parenthesis.
If you don't have rename
, then...
Assuming you'd want to remove all parentheses from all names of regular files in the current directory or below, the following find
command would do that:
find . -type f -name '* (*)*' -exec bash -O extglob -c '
for pathname do
filename=${pathname##*/}
mv -i "$pathname" "${pathname%/*}/${filename// (!(*\)*))}"
done' bash {} +
This would locate all regular files whose filename has parentheses in it. For batches of these files, a short bash
script would be called.
The bash
script would iterate over the current batch of found pathnames and would, for each, first extract the filename portion of the pathname, and then rename the file by removing the parentheses and their content.
There are three parameter substitutions used in the code. They are
${pathname##*/}
: This strips off the directory path from the pathname and leaves only the filename. It is, in this instance, the same as $(basename "$pathaname")
.
${pathname%/*}
: This removes the filename from the pathname and leaves the directory path. This is, in this instance, the same as $(dirname "$pathname")
.
${filename// (!(*\)*))}"
: This is a bash
-specific substitution that removes any space+parentheses from the value of $filename
. The sub-pattern !(*\)*)
is an extended globbing pattern that matches any string that does not contain a )
. We use that in removing individual parentheses so that a filename such as 0 (1) 2 (3)
is turned into 0 2
and not just 0
.
If there are multiple parentheses, all of these would be removed
(as illustrated above).
If you are certain that you don't have directories with parentheses like this, then you may use the shorter command
find . -type f -name '* (*)*' -exec bash -O extglob -c '
for pathname do
mv -i "$pathname" "${pathname// (!(*\)*))}"
done' bash {} +
Testing the first command:
$ ls -l
total 0
-rw-r--r-- 1 kk wheel 0 Mar 6 18:21 Super Mario Bros (J).nes
-rw-r--r-- 1 kk wheel 0 Mar 6 18:21 Super Random game (J,U,E).nes
$ find . -type f -name '* (*)*' -exec prename 's/ \([^)]*\)//g' {} +
$ ls -l
total 0
-rw-r--r-- 1 kk wheel 0 Mar 6 18:21 Super Mario Bros.nes
-rw-r--r-- 1 kk wheel 0 Mar 6 18:21 Super Random game.nes
The other commands give the same results.
rename
utility? – Kusalananda Mar 05 '19 at 21:06rename
might exist on your system as/usr/bin/file-rename
or/usr/bin/prename
– glenn jackman Mar 05 '19 at 21:13Q*bert (spring)
be left alone becausespring
is more than one letter? And what aboutAbc (def) ghi (J,K)
? (Note: you said that you were going to respond to Kusalananda’s comment, but you didn’t.) – G-Man Says 'Reinstate Monica' Mar 06 '19 at 18:17