Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-12-2007, 08:42 PM   #1
Registered: Jul 2003
Location: Fort Worth, Texas
Distribution: Mepis Linux 2004
Posts: 547

Rep: Reputation: 30
Bash, deleting only files in a directory tree?


I've got a unique problem I think can be resolved with the 'find' command but I'm unsure. Perhaps somebody could help?

I have a directory tree that I want to only delete the files from.



All of the files reside in a or b of this directory structure. If I could search and clear all files in each sites A or B folder systematically using the find command, this would ease a lot of trouble. New sites with a and b directories are added regularly, so if I had a static script just calling rm, it would have to be updated each time.

Is this possible using any assortment of commands like find? Code examples would be great. Thanks for your assistance!
Old 05-12-2007, 08:48 PM   #2
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The following command will delete files but not directories.
find search_cache -type f -exec rm {} \;
You were correct thinking that the find command would be part of the solution. The find command is the Swiss Army Knife of file manipulation.

Last edited by stress_junkie; 05-12-2007 at 08:50 PM.
Old 05-12-2007, 08:52 PM   #3
Registered: Jul 2003
Location: Fort Worth, Texas
Distribution: Mepis Linux 2004
Posts: 547

Original Poster
Rep: Reputation: 30
Had no clue about the type -f option. Thanks very much. ;-)
Old 05-12-2007, 10:29 PM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
assuming only 2 levels deep , and there might be possibility of c,d,e etc directories which you don't want files to remove, you can use rm with wildcards also
rm search_cache/*/a/*
rm search_cache/*/b/*
Old 05-13-2007, 11:36 AM   #5
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Of course, if you have a large number of files, you can increase efficiency by using xargs:

find search_cache -type f | xargs rm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM
How to copy files from an entire directory tree using terminal commands??? lusius188 Linux - General 3 04-10-2006 11:23 AM
Deleting unwanted files from any directory without reading each Karthikeyan Gurusamy Linux - Newbie 3 12-24-2005 01:48 AM
locate and copy files - without directory tree rodda Linux - General 1 01-16-2005 02:10 AM
How to copy a directory tree without copying the files in it? system Linux - General 2 01-15-2002 09:36 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration