批量重命名OSX终端中的文件

时间:2013-08-15 12:56:54

标签: bash terminal

我正在尝试重命名文件,例如使用this SO question中的方法screen-0001.tif0001.tif

for file in *.tif
do
  echo mv "$file" "${screen-/file}"
done

无法改变任何事情。感谢我出错的想法。

2 个答案:

答案 0 :(得分:2)

更容易,恕我直言,这样做的方法是使用Perl的--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

brew install rename 可以使用Homebrew轻松安装,使用:

create table receivers(
country varchar(10) not null,
date datetime not null,
quantity integer not null
);

insert into receivers values ("china", "2000-01-01", 100);
insert into receivers values ("japan", "2000-01-01", 900);
insert into receivers values ("usa", "2000-01-01", 345);
insert into receivers values ("usa", "2000-01-01", 234);
insert into receivers values ("usa", "2000-01-01", 56);
insert into receivers values ("usa", "2000-01-01", 12);
insert into receivers values ("china", "2000-01-01", 300);
insert into receivers values ("china", "2000-01-01", 10);
insert into receivers values ("china", "2000-01-01", 1000);

答案 1 :(得分:1)

两件事:

  1. 您正在回显命令而不是实际执行它们。当我进行大量重命名以确保命令正常工作时,我会这样做。我可以将输出重定向到一个文件,然后将该文件用作shell脚本。
  2. 替换是错误的。有两种方法:
    1. 最左边的过滤器${file#screen-}
    2. 换人:${file/screen/}
  3. 环境变量的名称始终是第一位的。然后是模式类型,然后是模式

    以下是我将如何做到这一点:

    $ 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.