Fisher Coder
Fisher Coder

Reputation: 3576

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!

Upvotes: 1

Views: 1154

Answers (6)

J Mohan Kumar
J Mohan Kumar

Reputation: 1

Apply suitable UNIX commands for listing the files present in the specified directory and then remove the same directory

Upvotes: 0

mpapec
mpapec

Reputation: 50677

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) }, ".")'

Upvotes: 3

isedev
isedev

Reputation: 19641

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 {} +

Upvotes: 2

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2366

This should do the job

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

Upvotes: 0

Miguel Prz
Miguel Prz

Reputation: 13792

If you want to use Perl to do it, use a specific module like File::Remove

Upvotes: 0

seacoder
seacoder

Reputation: 514

find . -name "*~" -exec rm {} \;

or whatever pattern is needed to match the tmp files.

Upvotes: 0

Related Questions