Bash 批重命名OSX终端中的文件
我正在尝试使用以下方法将文件重命名,例如Bash 批重命名OSX终端中的文件,bash,terminal,Bash,Terminal,我正在尝试使用以下方法将文件重命名,例如screen-0001.tif为0001.tif: 无法改变任何事情。感谢你告诉我哪里出了问题。两件事: 你在重复命令,而不是实际执行它们。我将在执行大规模重命名时执行此操作,以确保命令正常工作。我可以将输出重定向到一个文件,然后将该文件用作shell脚本 替换是错误的。有两种方法: 最左边的过滤器${file#screen-} 替换:${file/screen/} 环境变量的名称始终位于第一位。然后是模式类型,然后是模式 我会这样做: $ for fi
screen-0001.tif
为0001.tif
:
无法改变任何事情。感谢你告诉我哪里出了问题。两件事:
${file#screen-}
${file/screen/}
$ 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.
两件事:
${file#screen-}
${file/screen/}
$ 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.
更简单的方法是使用Perl的
rename
脚本。在这里,我将它与--dry run
一起使用,因此它只是告诉您它将做什么,而不是实际做什么。如果/当您对命令满意时,您只需删除--dry run
:
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'
它还有一个额外的好处,即它不会覆盖任何碰巧以相同名称出现的文件。因此,如果您有文件:
screen-001.tif
0screen-01.tif
如果你这样做了,你会得到:
rename 's/screen-//' *tif
'screen-001.tif' not renamed: '001.tif' already exists
rename
可通过以下方式轻松安装:
更简单的方法是使用Perl的
rename
脚本。在这里,我将它与--dry run
一起使用,因此它只是告诉您它将做什么,而不是实际做什么。如果/当您对命令满意时,您只需删除--dry run
:
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'
它还有一个额外的好处,即它不会覆盖任何碰巧以相同名称出现的文件。因此,如果您有文件:
screen-001.tif
0screen-01.tif
如果你这样做了,你会得到:
rename 's/screen-//' *tif
'screen-001.tif' not renamed: '001.tif' already exists
rename
可通过以下方式轻松安装:
您正在
回显它,而不是执行命令本身。另外,您最好使用${file/screen-/}
删除初始的“screen-”text.Cheers。把它作为答案,我会投票给你,你是echo
ing它,而不是执行命令本身。另外,您最好使用${file/screen-/}
删除初始的“screen-”text.Cheers。把这个作为答案,我会投票的