问个命令,怎么批量删除命名几个目录下的文件??

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
mimixi666
帖子: 52
注册时间: 2014-03-28 8:57

问个命令,怎么批量删除命名几个目录下的文件??

#1

帖子 mimixi666 » 2014-04-01 18:47

比如我有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
:em06
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#2

帖子 cao627 » 2014-04-01 19:07

设在当前目录下包含A B C三个目录

代码: 全选

$ rename 's/\(1\)//' {A,B,C}/*
mimixi666
帖子: 52
注册时间: 2014-03-28 8:57

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#3

帖子 mimixi666 » 2014-04-01 22:05

cao627 写了:设在当前目录下包含A B C三个目录

代码: 全选

$ rename 's/\(1\)//' {A,B,C}/*
这样子啊,那如果是在A,B,C的子目录或者子子目录或者子子子目录的话,又该怎么办??
假设A,B,C这三个目录是在一个D的目录下的话,
是不是这样??

代码: 全选

$ rename 's/\(1\)//' {D}/*
[/quote]
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#4

帖子 cao627 » 2014-04-02 9:05

mimixi666 写了:
cao627 写了:设在当前目录下包含A B C三个目录

代码: 全选

$ rename 's/\(1\)//' {A,B,C}/*
这样子啊,那如果是在A,B,C的子目录或者子子目录或者子子子目录的话,又该怎么办??
假设A,B,C这三个目录是在一个D的目录下的话,
是不是这样??

代码: 全选

$ rename 's/\(1\)//' {D}/*
有兴趣的话去看一下shell基础这方面的书。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#5

帖子 eexpress » 2014-04-02 9:15

find . -iname "*\(1\)" -exec rename 's/\(1\)//' {} \;

大概这样。
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#6

帖子 eexpress » 2014-04-02 9:17

rename 带上 -n 先预览下结果。再去掉-n实际执行。
● 鸣学
mimixi666
帖子: 52
注册时间: 2014-03-28 8:57

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#7

帖子 mimixi666 » 2014-04-03 21:02

eexpress 写了:find . -iname "*\(1\)" -exec rename 's/\(1\)//' {} \;

大概这样。
成功了,十分谢谢你。。。
顺便问下 -exec是啥意思??
是把前面的结果过滤到后面那里吗??
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#8

帖子 eexpress » 2014-04-03 21:09

是的。你应该看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.
● 鸣学
头像
男菜鸟
帖子: 1382
注册时间: 2008-12-16 14:01
来自: 漂在江湖

Re: 问个命令,怎么批量删除命名几个目录下的文件??

#9

帖子 男菜鸟 » 2014-04-04 23:44

mark
回复