Batch renaming files in OSX terminal

后端 未结 2 488
慢半拍i
慢半拍i 2021-01-23 01:34

I\'m trying to rename files e.g. screen-0001.tif to 0001.tif using the approach in this SO question:

for file in *.tif
do
  echo mv \"$         


        
2条回答
  •  -上瘾入骨i
    2021-01-23 01:59

    The easier, IMHO, way to do this is using Perl's rename script. Here I am using it with --dry-run so it just tells you what it would do, rather than actually doing anything. You would just remove --dry-run if/when you are happy with the command:

    rename --dry-run 's/screen-//' *tif
    
    'screen-001.tif' would be renamed to '001.tif'
    'screen-002.tif' would be renamed to '002.tif'
    'screen-003.tif' would be renamed to '003.tif'
    'screen-004.tif' would be renamed to '004.tif'
    

    It has the added benefit that it will not overwrite any files that happen to come out to the same name. So, if you had files:

    screen-001.tif
    0screen-01.tif
    

    And you did this, you would get:

    rename 's/screen-//' *tif
    'screen-001.tif' not renamed: '001.tif' already exists
    

    rename is easily installed using Homebrew, using:

    brew install rename
    

提交回复
热议问题