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,
or
or
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?
LMHmedchem
Last edited by Franklin52; 05-31-2013 at 03:17 AM..
Reason: Please use code tags
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?
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.
This User Gave Thanks to jaiseaugustine For This Post:
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:-
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:-
because the shell will try to run:-
I hope that this clarifies things a bit.
Robin
Liverpool/Blackburn
UK
These 2 Users Gave Thanks to rbatte1 For This Post:
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.
LMHmedchem
This User Gave Thanks to LMHmedchem For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)