2

I have 195 files in one other folder. Every single one has a combination of numbers and letters in [] brackets in front of its description. Example: [12hjk534m45] description.

Is it possible to delete the brackets and its contents with a command?

I hoped to do it with rename command and the syntax [abc]?
How would that command look like?

2 Answers 2

1
rename -n 's:^\[.*\](.*):$1:' *

[02Rarev]QgysLJF renamed as QgysLJF
[0rwCtGh]cGdxnWH renamed as cGdxnWH
[1B2CMhq]M91oPHh renamed as M91oPHh
[1BMD7N3]0nYVP0b renamed as 0nYVP0b
[1MjkCZu]WBPYXKG renamed as WBPYXKG
[2HoSmD7]aLWLmpf renamed as aLWLmpf

^\[.*\] matches from beginning^ of files for brackets and everything inside pair of it which is followed by anything(.*). So with (.*) we just captured a group of anything after brackets that contains only files description then in replacement section s/.../REPLACEMENT/ of rename command I replaced all files names with description part by using back-reference of captured group($1 refer to index of first captured group(.*)).

You can also use rename with class of characters matching:

rename -n 's:^\[([0-9A-Za-z]*)\](.*):$1:' *

How to create multiple files with different names and different extensions?

2
  • I think something like rename 's/\[[^]]\]//' * would be slightly faster (but not to any extent noticeable with just 200 files). Also, it shpuld probably be noted that the -n flag tells rename to just report what it will do; to actually rename the files, you need to get rid of the -n.
    – evilsoup
    Dec 12, 2014 at 16:10
  • @evilsoup I tested it with two different commands on 100,000 files. one with deleting part of match and second with renaming but I get a little slow than from fist one. test result Dec 12, 2014 at 16:41
0

There's a really brilliant program for doing things like this called GPRename, it's in the software centre.

(Here's info on the commands:

http://tips.webdesign10.com/how-to-bulk-rename-files-in-linux-in-the-terminal

Looks to like what you want is there but you'll have to figure it out.. or just use GPRename!

great explanation of it here too: How to easily rename files using command line?)

You must log in to answer this question.

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