Visit Our UNIX and Linux User Community

Delete all files with specific extension in directory tree

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all files with specific extension in directory tree
# 1  
Old 05-31-2013
Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing).

I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't need anymore have the extension .back, so I need to troll through all of the folders and sub-folders and delete everything with the .back extension. I thought I would need to do some kind of recursive ls and pipe the results to rm, but I'm not sure what that would look like so I did a search.

Many of the solutions I found use find and look like,
find . -name *.back -exec file {} \; -exec rm -i {} \;

find /path . -name '*.back' -type f -delete

find /path -iname "*.back" -type f -delete

As usual, there appear to be many ways of doing things and I have no basis on which to make a choice. These files are copies, so I could always rebuild the backup if there was a disaster with the cleaning, but that would take time and I try to avoid putting my foot in it to that extent.

Any suggestions?


Last edited by Franklin52; 05-31-2013 at 04:17 AM.. Reason: Please use code tags
# 2  
Old 05-31-2013
Go to the main directory and run
find . -name "*.back" -type f -exec rm -f {} \;

This User Gave Thanks to jaiseaugustine For This Post:
# 3  
Old 06-02-2013
Alright, I will give that a go. If you have a minute to answer, what is the difference between the method you posted and the other examples I gave in my original post?

# 4  
Old 06-03-2013
IMO, the first command will not work because the o/p of 'find' command is not piped in any way to the exec command. But the second and third should work for sure.
I wasn't sure about the -delete' action. That is why I gave you the command I am familiar and experienced with.

Any better explanations from geeks are welcome. Smilie
This User Gave Thanks to jaiseaugustine For This Post:
# 5  
Old 06-03-2013
Oh dear, I'm classing myself as a geek. Well, if the name fits, ......

The way you have tried, the shell will expand *.back before trying to run the command. If you happend to have a file at the top level called this.back then the command actually run will become:-
find . -name this.back -exec file {} \; -exec rm -i {} \;

so you will not actually match anything other than the file at the top level.

The others are various errors. What jaiseaugustine has suggested is the correct format for you. It will pass in *.back as it is to the find command and then it can be used for pattern matching.

If there are no files at the top level, you might get away with it depending how your shell reacts, but if there is more that one file called *.back, e.g. this.back & that.back, then you will probably get the error:-
find: There is a missing conjunction

because the shell will try to run:-
find . -name that.back this.back -exec rm {} ;

I hope that this clarifies things a bit.

These 2 Users Gave Thanks to rbatte1 For This Post:
# 6  
Old 06-03-2013
Further, the -exec file {} \; -exec rm -i {} \; is tailored for interactive use,
while -type f -exec rm -f {} \; is tailored for scripts.

BTW -delete is a new option in new find versions. Would be more efficient if thousands of files are deleted.
These 3 Users Gave Thanks to MadeInGermany For This Post:
# 7  
Old 06-03-2013
Originally Posted by jaiseaugustine
That is why I gave you the command I am familiar and experienced with.
This is more or less always what I end up doing and I guess it is a reasonable way to proceed in most cases. I keep notes on what I have used for various situations, especially those methods that worked well.

The command,

find . -name "*.back" -type f -exec rm -f {} \;

worked well and cleared out about 50GB or older incremental versions. I ran this while I was out for a while and I didn't run it under time, so I can't comment on how fast the method is compared to other possibilities. I generally presume that there is no fast script based method to process a directory tree with 4+ million files.

I also did a defrag/optimize (auslogics) and clean out of MFT records. All told it took almost 24 hours to run, but I find I need to keep these backups well maintained, or they eventually bork and you have to reformat and start again. It seems as if rsync tends to lead to very fragmented repositories. I have never quite understood why you get lots of fragmenting on a drive with 500GB of empty space.

Thanks for all the additional explanations. I do always try to understand what a script is doing and why you would choose one method over another. I think I need to read a bit about exec.

This User Gave Thanks to LMHmedchem For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

2. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

3. Shell Programming and Scripting

Specific directory parsing in a directory tree

Hi friends, Hello again :) i got stuck in problem. Is there any way to get a special directory from directory tree? Here is my problm.." Suppose i have one fix directory structure "/abc/xyz/pqr/"(this will be fix).Under this directory structure i have some other directory and... (6 Replies)
Discussion started by: harpal singh
6 Replies

4. UNIX for Dummies Questions & Answers

How do I delete all files except one of a certain extension?

Let's say I wanna Delete all the files of a certain extension exept one. How do I do it? I know, if you wanna delete them all is with the command: find ~/ -type f -iname '*.txt' -exec rm {} ~/ ';' But If I want to keep an Specific file? Let's say I wanna keep 'Log.txt'. How do I do it? (1 Reply)
Discussion started by: lsteamer
1 Replies

5. UNIX for Dummies Questions & Answers

Copy directory tree with files

Iam in the process of copying a directory with thousands of directories and files into a new directory. I need to preserve permissions, owner, group, date and timestamps, everything. Iam using AIX and would need help of writing the command whether it is cp-RP or cpio. Apprecaite your... (3 Replies)
Discussion started by: baanprog
3 Replies

6. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

7. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies

8. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

Hi I am a shell-script newbie and am looking to synchronize certain files in two directory structures. Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over. I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)
Discussion started by: sharpsharkrocks
3 Replies

9. Shell Programming and Scripting

How to get files with a specific extension

Hi All, How do I get only the files with the .csv extension. I have a shell script something like below: #!/usr/bin/ #Directory to scan for files SCANDIR="/cmb/data/exstream/scriptslogs/"; LOGFILE="/cmb/data/exstream/scriptslogs/test.log"; cd $SCANDIR for FILE in * ; do FILENAME=$FILE... (9 Replies)
Discussion started by: janavenki
9 Replies

10. Shell Programming and Scripting

delete files in specific directory

i have a directory "ABC" with lots of old files and sub directories in it. the issue now is i want to delete away files which are older than 15 days in "ABC" without deleting the files in the sub directories and without deleting the sub directory. i tried using find command but it will drill down... (2 Replies)
Discussion started by: legato
2 Replies

Featured Tech Videos