Skip to content
Advertisement

How to replace a hash pattern inside file and directory names (Linux)

I’ve just backed up the notes I have in Notion, and they use hashcodes in the notes names to make them unique apparently.

For example, this is might be a note-folder with some notes:

Archive e74f698cb73b42c790d08999bdbe47e4
├── Note 1 cda63bef095a493f9847d516cf34fa5f.md
├── Note 2 dda63bef095a493f9847d516cf34fa5f.md

(Sometimes there’s also a .csv file for tables)

I was trying to use a regex like [a-z0-9]{32} to replace the pattern, but was having difficulty making it work with s/:

find -type f -print0 | xargs -0 rename 's/[a-z0-9]{32}//g'

(I’m not sure I really understand what the purpose of -print0 and -0 is.)

The code above only works for files, but I’ve also tried to use something like mv $f ${f:0:-34} though I couldn’t make it work either. Is there a way of making this replacing with only one line for both files and folders, without fors?

I’ve tried many other answers on StackOverflow about this but almost all I’ve found so far either only deal with files or with fixed words as patterns.

I wanted to use the Mass-Move command line tool (mmv), but there’s no official distribution for Arch Linux?

Advertisement

Answer

steeldriver has created a very complete and thorough answer on the Unix & Linux StackExchange. The whole discussion was originally supposed to be all here. But the question was at some point closed and I had to reask it there. When it came back up here I gave steeldriver a heads up, but he isn’t apparently either interested or aware. I’ll leave this answer until he chooses to copy-paste or link his own content here.

Sorry for the unexpected cross-/copy-paste-posting.

Advertisement