Batch renaming files in OSX terminal

后端 未结 2 487
慢半拍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条回答
  •  执笔经年
    2021-01-23 01:54

    Two things:

    1. You're echoing the commands and not actually executing them. I will do this when I do massive renames just to make sure that the command works correctly. I can redirect the output to a file, and then use that file as a shell script.
    2. The substitution is wrong. There are two ways:
      1. Left most filter ${file#screen-}.
      2. Substitution: ${file/screen/}

    The name of the environment variable always goes first. Then the pattern type, then the pattern

    Here's how I would do this:

    $ for file in *.tif
    > do
    >   echo "mv '$file' '${file#screen-}'"
    > done | tee mymove.sh   # Build a shell script 
    $ vi mymove.sh           # Examine the shell script and make sure everything is correct
    $ bash mymove.sh         # If all is good, execute the shell script.
    

提交回复
热议问题