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
# 8  
Old 06-04-2013
As MadeInGermany informs us that the -delete is available in newer versions, then that will probably run better than -exec rm {} \; as the latter will spawn a new process for each file, and that in itself will take a small amount of time. Multiply by perhaps 100,000 hits suddenly becomes a lot of time spent just creating a new process for each delete.

To my embarrassment, most of my servers are rather behind the times (AIX 4.3.3 for some) so I've only got this flag in RHEL 6.3.

Interestingly in the RHEL man page, I found this:-
Quote:
NON-BUGS
$ find . -name *.c -print
find: paths must precede expression
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] {path...] [expression]

This happens because *.c has been expanded by the shell resulting in
find actually receiving a command line like this:

find . -name bigram.c code.c frcode.c locate.c -print

That command is of course not going to work. Instead of doing things
this way, you should enclose the pattern in quotes or escape the wild-
card:
$ find . -name '*.c' -print
$ find . -name \*.c -print
Perhaps a neater way (with examples) of what I was trying to say earlier.



Glad that we could collectively help, and I've learned something too Smilie

Robin
# 9  
Old 06-04-2013
Hi.
Quote:
Originally Posted by LMHmedchem
... 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. ...
You mention MFT -- is this filesystem NTFS? ... cheers, drl
# 10  
Old 06-04-2013
Quote:
Originally Posted by drl
You mention MFT -- is this filesystem NTFS?
Yes, this is windows XP, but I do the heavy lifting with cygwin. Nothing beats a linux tool box for large scale file operations. I suppose there may be a dos equivalent, but I never bothered to learn dos when I could use cygwin and learn a real shell like bash instead.

LMHmedchem
# 11  
Old 06-04-2013
Hi.

I generally advise people to use whatever seems best for them. However, we need to accept drawbacks along with the advantages.

For your rsync / fragmentation issue, I think it's the XP NTFS filesystem, not the application. Some links you may want to look over are:

Defragmentation - Wikipedia, the free encyclopedia

A forum discussion (mostly slanted toward Linux, against MS): Why does Windows suffer from disk fragmentation when Linux doesn't? [Archive] - Ubuntu Forums

A blog that is more MS-centric view: Rants and Raves Linux File System Fragmentation

File system fragmentation - Wikipedia, the free encyclopedia

And many many more from Google searches.

My own experience with rsync (via rsnapshot): my workstation backup is run 6 times per day. I have been running it for more than 1 year. The numbers from fsck are:
Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdc1             241G   35G  194G  16% /media/big_disk_1

big_disk_1: 1,285,708/16,007,168 files (0.3% non-contiguous), 8984671/64000944 blocks

So very low fragmentation for a disk that sees a fair amount of activity. This does not necessarily parallel your use, but I have never used a defragmentation code on any Linux filesystem. When I used W2K, I seemed to need a defragmentation run quite often.

Best wishes ... cheers, drl
# 12  
Old 06-04-2013
Quote:
Originally Posted by LMHmedchem
find . -name "*.back" -type f -exec rm -f {} \;
That's a very inefficient method of deleting a large number of files with Cygwin. The fork required to delete each file performs very, very poorly. Either use -delete or the + version of -exec.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 13  
Old 06-04-2013
Quote:
Originally Posted by alister
That's a very inefficient method of deleting a large number of files with Cygwin. The fork required to delete each file performs very, very poorly. Either use -delete or the + version of -exec.
So would this be the preferable version if there is the potential for a large number of files to be involved?

find . -name "*.back" -type f -delete

I'm not familiar with what the "+ version of -exec" would be.

What is the difference between using double and single quotes for the extension? I have seen both, meaning "*.back" or '*.back'?

LMHmedchem
# 14  
Old 06-04-2013
To reduce the calls to programs (like rm), there are two implementations
1. Unix, later defined by Posix
Code:
find . -name "*.back" -type f -exec rm -f {} +

I.e. you replace \; by + and the program (here rm) must accept multiple arguments.
This implementation seems difficult; I have met some buggy ones.
2. GNU find, by means of the xargs program that converts an input stream to multiple arguments:
Code:
find . -name "*.back" -type f -print0 | xargs -0 rm -f

Note the corresponding -print0 and -0; without them there is incorrect handling of filenames with space characters.
--
A demonstration of quoting types:
Code:
echo "this is $HOME"
echo 'this is $HOME'

Concerning *.back there is no difference.
Login or Register to Ask a Question

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
Login or Register to Ask a Question