3

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
  • mmv '* *.*' '#1.#3'
    – user986805
    Jan 8, 2022 at 18:27

2 Answers 2

7

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
  • Thx, but I just fixed it with a small program. Your answer could have worked I think.
    – Tjeard
    Jan 8, 2022 at 12:14
2

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

You must log in to answer this question.

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