0

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?

1 Answer 1

1

Note: the following assumes that you are using the Perl-based rename command, as implied by your observation that rename 's/[a-z0-9]{32}//g' "works for files". See


To find directories as well as files, you need to remove the -type f restriction.

In addition you will need to prevent directories from being renamed before their containing files - with GNU find, that may be done using -depth to specify depth-first traversal.

Finally, you need to make sure that the rename command operate only on the file basename rather than its path components - your rename implementation may provide an option to do that (ex. -d, --filename, --nopath, --nofullpath) but you can also invoke rename directly from find using the -execdir action.

So

find . -depth -execdir rename -n 's/ [a-z0-9]{32}//' {} +

(remove the -n once you are satisfied with the proposed substitutions). Note that you don't need the g replacement flag to replace a single instance of the pattern per file.

You could also consider replacing [a-z0-9] with [[:xdigit:]].

See also:

7
  • Could you also provide a brief explanation for {} and +?
    – psygo
    Commented Mar 23, 2021 at 1:28
  • 1
    @PhilippeFanaro I have added a link which explains them better than I can Commented Mar 23, 2021 at 1:31
  • I'm getting back a rename: not enough arguments from the code above, both with or without -n. Given that there's a space right before the hashcodes, shouldn't we also eliminate it with the pattern?
    – psygo
    Commented Mar 23, 2021 at 1:37
  • Nice additional resources, thanks.
    – psygo
    Commented Mar 23, 2021 at 1:38
  • 1
    @PhilippeFanaro are you sure you are using the Perl-based rename command rather than the util-linux one? Regarding the extra space, I just used the pattern that you suggested - of course you may remove the space as well if you wish. Commented Mar 23, 2021 at 1:41

You must log in to answer this question.

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