Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
rename "s/\[.*\] *//" "[bla bla bal ] document 1.zip"
If the filename is stored in a shell variable, you can try a parameter substitution like this:
Code:
mv "$file" "${file/\[*\] }"
Please note that the rename command uses perl-like regular expressions, whereas the parameter substitution in bash uses patterns, hence the difference between the two forms.
Colucix's substitution is good, but this may be easier:
Code:
filename="[bla bla bal ] document 1.zip"
mv "$filename" "${filename#*] }"
It simply removes everything from the start of the name up to the first "]" and the space following it.
As for the rename command above, it will generally work, but since "*" is "greedy" in regex, if there ever happen to be two "]" characters in the string it will delete everything up to the second one.
In addition the using "[]" character range expression to match individual characters is considered more reliable than backslash escaping them (if not quite as readable in this specific case ).
So this version is a bit more robust:
Code:
rename 's/[[][^]]+[]] //' "[bla bla bal ] document 1.zip"
Well, to match a literal backslash in a regular expression you have to escape it using a... backslash:
Code:
\\
If you add this to the rename command suggested by David the H. followed by an asterisk, it should match both the files w/ and w/o a leading backslash in their name. Since you mentioned multiple file names, it should be useful at this point to see a list of the real files you want to rename or at least establish an exact criterion to remove all the unwanted parts, e.g.
remove all the parts embedded in square brackets (brackets included) possibly with leading backslashes and trailing blank spaces.
I think colucix has seized upon one of the essential concepts: the distinction between the use of patterns and regular expressions. It is also worth mentioning that assigning the filename to a shell variable provides opportunity for manipulation, and such manipulation may be performed both piece-wise and iteratively. This may lead to code that is easier to develop, easier to understand and easier to modify. In other words, you don't have to make the transformation in one step if it is confusing or overly complex.
Are you absolutely sure the name starts with "\["? If you use tab completion, then the shell will automatically backslash escape all shell-reserved characters in the completed match.
If you run echo *.zip, for example, what output matches do you get?
And see these links for details on how the shell handles whitespace and other reserved characters:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.