Find command

Posted on 2011-09-22
Last Modified: 2012-05-12
find /tmp/mydirectory -type f –iname -mtime +30 -exec rm -f {} \;

I want to find  and remove specific files with extensions .dat,.txt,.TXT,.csv,.XLS from /tmp/mydirectory which are 30days old.
Can some body tell me how to pass the combination of file extensions so that only files with these extensions are picked and deleted?(Any files other than these set of extensions should not be removed.I'm using redhat Red Hat Enterprise Linux).Thanks in advance
Question by:prashubk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
LVL 84

Expert Comment

ID: 36584809
-iname '*.dat' -o -iname '*.txt' -o -iname '*.csv' -o -iname '*.xls'
LVL 21

Expert Comment

ID: 36584841
find /tmp/mydirectory -type f –iname '*.dat' -o -iname '*.txt' -o -iname '*.csv' -o -iname '*.xls' -mtime +30 | xargs rm -f

Open in new window

If you have -o's and an exec, then you will need an exec for each -o.  xargs is more efficient as well, in some cases at least.
LVL 84

Accepted Solution

ozo earned 250 total points
ID: 36584852
you can use
\( -iname '*.dat' -o -iname '*.txt' -o -iname '*.csv' -o -iname '*.xls' \) -mtime +30 -exec
but xargs is generally more efficient
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 36584870
thanks for the answers.  Why do we say xargs is more efficient over exec?. What is the advantages of using xargs over exec in this context?
LVL 21

Assisted Solution

Papertrip earned 250 total points
ID: 36584884
exec will spawn a new process (rm -f) for each match.  xargs contructs a list and will spawn the process once it hits the limit defined by the system.

[root@broken etc]# xargs --show-limits
Your environment variables take up 1699 bytes
POSIX upper limit on argument length (this system): 2617693
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2615994
Size of command buffer we are actually using: 131072

Open in new window

LVL 21

Expert Comment

ID: 36584913
Thanks for the assist, and no offense to ozo of course, but why was http:#36584852 chosen as Accepted Solution?

Are you not going to use xargs like we both suggest?  If you are then technically http:#36584841 is exactly what you want and should have been the Accepted Solution.

Author Comment

ID: 36584925
sorry ,oh yes i should have accepted 36584841 as this is what i'm intended to use.Well Ozo contributed with the idea  to pick files with desired extension
LVL 21

Expert Comment

ID: 36584948
All good, just wondering, thanks for replying.

Good luck!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux rpm install 5 236
Understanding curl command 1 62
Linux : Booting Error after restoring image from OLD PC 23 95
Linux : adding the new user with password option, 11 125
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question