分页: 1 / 1
问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-01 18:47
由 mimixi666
比如我有A,B,C这三个目录,我想把这三个目录下的所有包含(1)字符串的文件,重新命名删除(1)字符串:
这样子:
A:
a(1)
aa(1)
aaa(1)
aaaa(1)
B:
b(1)
bb(1)
bbb(1)
bbbb(1)
c:
c(1)
cc(1)
ccc(1)
cccc(1)
更改后,变成这样:
A:
a
aa
aaa
aaaa
B:
b
bb
bbb
bbbb
c:
c
cc
ccc
cccc

Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-01 19:07
由 cao627
设在当前目录下包含A B C三个目录
代码: 全选
$ rename 's/\(1\)//' {A,B,C}/*
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-01 22:05
由 mimixi666
cao627 写了:设在当前目录下包含A B C三个目录
代码: 全选
$ rename 's/\(1\)//' {A,B,C}/*
这样子啊,那如果是在A,B,C的子目录或者子子目录或者子子子目录的话,又该怎么办??
假设A,B,C这三个目录是在一个D的目录下的话,
是不是这样??
代码: 全选
$ rename 's/\(1\)//' {D}/*
[/quote]
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-02 9:05
由 cao627
mimixi666 写了:cao627 写了:设在当前目录下包含A B C三个目录
代码: 全选
$ rename 's/\(1\)//' {A,B,C}/*
这样子啊,那如果是在A,B,C的子目录或者子子目录或者子子子目录的话,又该怎么办??
假设A,B,C这三个目录是在一个D的目录下的话,
是不是这样??
代码: 全选
$ rename 's/\(1\)//' {D}/*
有兴趣的话去看一下shell基础这方面的书。
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-02 9:15
由 eexpress
find . -iname "*\(1\)" -exec rename 's/\(1\)//' {} \;
大概这样。
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-02 9:17
由 eexpress
rename 带上 -n 先预览下结果。再去掉-n实际执行。
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-03 21:02
由 mimixi666
eexpress 写了:find . -iname "*\(1\)" -exec rename 's/\(1\)//' {} \;
大概这样。
成功了,十分谢谢你。。。
顺便问下 -exec是啥意思??
是把前面的结果过滤到后面那里吗??
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-03 21:09
由 eexpress
是的。你应该看man find的说明
使用{} \; 是每一个文件都单独执行一次,相当于用{}替代找到的文件。
还有
-exec command ;
Execute command; true if 0 status is returned. All following arguments
to find are taken to be arguments to the command until an argument con‐
sisting of `;' is encountered. The string `{}' is replaced by the cur‐
rent file name being processed everywhere it occurs in the arguments to
the command, not just in arguments where it is alone, as in some versions
of find. Both of these constructions might need to be escaped (with a
`\') or quoted to protect them from expansion by the shell. See the
EXAMPLES section for examples of the use of the -exec option. The speci‐
fied command is run once for each matched file. The command is executed
in the starting directory. There are unavoidable security problems sur‐
rounding use of the -exec action; you should use the -execdir option
instead.
-exec command {} +
This variant of the -exec action runs the specified command on the
selected files, but the command line is built by appending each selected
file name at the end; the total number of invocations of the command will
be much less than the number of matched files. The command line is built
in much the same way that xargs builds its command lines. Only one
instance of `{}' is allowed within the command. The command is executed
in the starting directory.
Re: 问个命令,怎么批量删除命名几个目录下的文件??
发表于 : 2014-04-04 23:44
由 男菜鸟
mark