简体   繁体   中英

How to write a unix command or script to remove files of the same type in all sub-folders under current directory?

Is there a way to remove all temp files and executables under one folder AND its sub-folders?

All that I can think of is:

$rm -rf *.~

but this removes only temp files under current directory, it DOES NOT remove any other temp files under SUB-folders at all, also, it doesn't remove any executables.

I know there are similar questions which get very well answered, like this one: find specific file type from folder and its sub folder but that is a java code, I only need a unix command or a short script to do this.

Any help please? Thanks a lot!

Perl from command line; should delete if file ends with ~ or it is executable,

perl -MFile::Find -e 'find(sub{ unlink if -f and (/~\z/ or (stat)[2] & 0111) }, ".")'

You can achieve the result with find :

find /path/to/directory \( -name '*.~' -o \( -perm /111 -a -type f \) \) -exec rm -f {} +

This will execute rm -f <path> for any <path> under (and including) /path/to/base/directory which:

  • matches the glob expression *.~
  • or which has an executable bit set (be it owner, group or world)

The above applies to the GNU version of find .

A more portable version is:

find /path/to/directory \( -name '*.~' -o \( \( -perm -01 -o -perm -010 -o -perm -0100 \) \
     -a -type f \) \) -exec rm -f {} +
find . -name "*~" -exec rm {} \;

或需要什么模式来匹配tmp文件。

如果要使用Perl进行操作,请使用特定的模块,例如File :: Remove

这应该做的工作

find -type f -name "*~" -print0 | xargs -r -0 rm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM