Skip to content

Instantly share code, notes, and snippets.

@protrolium
Last active March 5, 2021 21:50
Show Gist options
  • Save protrolium/71f68844679507287bd0767a0b573c49 to your computer and use it in GitHub Desktop.
Save protrolium/71f68844679507287bd0767a0b573c49 to your computer and use it in GitHub Desktop.
rename command utility

rename | syntax and examples

brew install rename
to install

rename "s/SEARCH/REPLACE/g" *

This will replace the string SEARCH with REPLACE in every file (that is, *). The /g means global, so if you had a SEARCH_SEARCH.jpg, it would be renamed REPLACE_REPLACE.jpg. If you didn't have /g, it would have only done substitution once, and thus now named REPLACE_SEARCH.jpg. If you want case-insensitive, add /i (that would be, /gi or /ig at the end).

Examples

Uses Perl REGEX for i.e. ^ target beginning $ target end

Delete a string
rename 's/myString//' *

Add a Prefix
rename 's/^/MyPrefix_/' *

document.pdf renamed to MyPrefix_document.pdf


rename -n -N 0001 's/.*/$N.jpg/' *.jpg
sequentially rename a series of images (e.g. if there is an image sequence with some numbers missing in the sequence and you would like to rename everything sequentially).

rename 's/^CD RIP //' *

CD RIP 01 Song.mp3 to 01 Song.mp3

Notice the extra space in '^CD RIP ', without the space all files would have a space as the first character of the file. Also note, this will work without the ^character, but would match CD RIP in any part of the filename. The ^ guarantees it only removes the characters if they are the beginning of the file.


Dry Run

use -n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment