I would like to rename multiple files in one folder.

I am looking to keep all of the original filenames minus the tag at the end.

for example I have:





I want to remove [file] from all files in the folder recursively, and would like to do so from the command line.

Any help would be greatly appreciated.

  • Welcome to Ask Ubuntu! Are all the tags the same, or it could be anything within the brackets just before the file extension? – M. Becerra Jan 25 '17 at 17:56
  • Are these just generic examples, or you have filenames that are actually different ? – Sergiy Kolodyazhnyy Jan 25 '17 at 19:46
  • As you're a new user, don't forget to accept the answer that you found most helpful by clicking the grey round check button on the left of it. You can learn more about how our site works by investing only two minutes into taking the little tour. Thank you and welcome to Ask Ubuntu. – Byte Commander Jan 25 '17 at 23:22

I assume your current working directory is the one that contains the files you want to rename, like in this example:

$ tree

├── FILE1[file].doc
├── FILE2[file].doc
├── FILE3[file].doc
└── subdir
    ├── FILE1[file].doc
    ├── FILE2[file].doc
    └── FILE3[file].doc

Now you can run the following command to recursively list all files in the current directory with the extension .doc:

$ find . -type f -iname '*.doc'


To rename all those files and remove the [file] part right before the .doc extension (if it is present) from each name, run this:

$ find . -type f -iname '*.doc' -exec rename -v 's/\[file\](?=\.doc$)//' {} +

./subdir/FILE3[file].doc renamed as ./subdir/FILE3.doc
./subdir/FILE2[file].doc renamed as ./subdir/FILE2.doc
./subdir/FILE1[file].doc renamed as ./subdir/FILE1.doc
./FILE3[file].doc renamed as ./FILE3.doc
./FILE2[file].doc renamed as ./FILE2.doc
./FILE1[file].doc renamed as ./FILE1.doc

This executes the command rename -v 's/\[file\](?=\.doc$)//' FILES, where FILES is replaced with the list of files returned by the find command shown above.

What the rename 's/PATTERN/REPLACEMENT/' does is to apply the regular expression pattern \[file\](?=\.doc$) (matches the exact string "[file]" if followed by the exact string ".doc" at the end of the line) to each of the specified file names. If it matches, that matching part of the file name is replaced with the replacement string, which is empty in our case.

  • Thankyou, Byte Commander, this command worked a treat. If possible could you just explain to me a few things about it? What does the -v after rename do, and is the replacement string here -----> {} + – John Blaze Jan 25 '17 at 23:12
  • The -v option of rename means "verbose" and makes it print a line of output for every file it renames. You can omit it if you want to run the command quietly. The {} + is part of find's -exec option. Everything after the -exec in the line is the command that shall be called with the search results as argument. The {} is the placeholder where the result file name(s) will be inserted and + means to insert many file names into the same command at once, while \; would mean to call the command separately for each single file name. Learn more by typing man rename and man find. – Byte Commander Jan 25 '17 at 23:19

Python approach, with re.split() and os.rename().

$ ls
FILE1[file].doc  FILE2[file].doc  FILE3[file].doc
$ python -c "import os,re;[os.rename(f,'.'.join(re.split('\[|\]|\.',f)[::3])) for f in os.listdir('.')]"                 

$ ls
FILE1.doc  FILE2.doc  FILE3.doc

What happens here is that we loop over each filename in current working directory as provided by os.listdir() function, use re.split() to break down filename into list of strings using multiple separators, extract first and last string and make new name out of those two using '.'.join() function. The rest is simply os.rename(old,new)

Note that this is simple approach and doesn't protect against renaming directories or other stray files that you might have in that folder.

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.