Score:3

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

de flag

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

bac0n avatar
cn flag
`mmv '* *.*' '#1.#3'`
Score:6
cn flag

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
Tjeard avatar
de flag
Thx, but I just fixed it with a small program. Your answer could have worked I think.
Score:2
de flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.