Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove a specific part of the file name from a lot of files

I have an old backup from a windows pc, but most of the files have a timestamp at the end. All the files need to keep their original name, but without that timestamp.

How do I remove a specific part (2019_04_12 11_03_06 UTC) from all the files in multiple folders.

Example files:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

messages (2019_04_12 11_03_06 UTC).properties
maintenancemode (2019_04_12 11_03_06 UTC).jar

As far as I know all the files have the same timestamp

>Solution :

The perl rename command is powerful. It can look for string patterns and replace these. If your files have a similar time stamp, this would work:

rename -n 's/\(2019.*?\)//' *

This would remove all strings looking like (2019...). So it would not matter if some files have a different time stamp.

The match is made non-greedy by appending ? to .* (with thanks to Steeldriver). Thus, the expression will only match characters until the first occurrence of ), instead of possible additional occurrences later in the string.

The -n option causes the command to only show what is going to be done. The rename is not effectively done. This allows you to first check the output carefully, and if it shows that the command will do what you want, rerun it without the -n option to actually proceed with the rename.

The tool may not be installed by default. You can install it with :

sudo apt install rename
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading