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:

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

1

2 Answers

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 
1

I fixed the issue with this code

find . -maxdepth 20 -type f | while read LINE do mv "$LINE" `echo $LINE | sed 's/\s(2019_04_12 11_03_06 UTC)//'` done 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy