LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-20-2013, 12:16 PM   #1
pitfall7
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Rep: Reputation: Disabled
"Find" command


Hello

I would like to achieve following results:
1) delete all the files and folders that are older than 1 month
2) delete all the files that have .pcap extension, its size is greater than 1M and are older than 14days

Assuming that:
- all the files and folders are located in /home/user/
- directories are 1 lvl depth (so /home/user/One/, /home/user/Two/ ...) from the perspective of /home/user directory
i have prepared commands for this:
1) find /home/user/ -maxdepth 1 -mtime +30 -exec -exec rm -rf "{}" \;
2) find /home/user/ -maxdepth 1 -type f -size +1M -mtime +14 -exec rm -rf *.pcap "{}" \;

Could you please confirm or provide me with other wayout of how to achieve my goals.

Thanks,
Mac
 
Old 06-20-2013, 12:22 PM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
Why not try out these commands yourself (easy enough to set up a directory structure for them to work on) and see if they give you what you want.
jdk
 
Old 06-20-2013, 12:25 PM   #3
pitfall7
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi,

I don't want to delete all the files
Just want to double check first.

Thx.
 
Old 06-20-2013, 12:29 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
As mentioned, Copying some files to an alternate directory and testing will always be the best way to double check.

Anyway, Cleaned the commands up, and used full path to ls to ignore any aliases. And used ls to prove that we are looking at the right files. Just change ls -alh to rm -f once you are satisfied.

Code:
find /home/user/ -maxdepth 1 -mtime +30 -exec /bin/ls -alh {} \;
find /home/user/ -maxdepth 1 -type f -size +1M -mtime +14 -exec /bin/ls -alh *.pcap {} \;
 
1 members found this post helpful.
Old 06-20-2013, 12:37 PM   #5
pitfall7
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the hint.
I am about to check this out and give you feedback.
 
Old 06-20-2013, 12:38 PM   #6
mddnix
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch, Ubuntu
Posts: 498

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by pitfall7 View Post
Hi,

I don't want to delete all the files
Just want to double check first.

Thx.
Make a temp folder. And instead of -exec rm -rf, use -exec mv to that temp folder. see if all the files you wanted to delete are in temp folder. if you are good, delete temp folder and your find command works fine. else just restore.
 
2 members found this post helpful.
Old 06-20-2013, 12:41 PM   #7
pitfall7
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
yeah good point as well...

Thanks
 
Old 06-21-2013, 07:48 AM   #8
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
For your second command you should use -name "*.pcap" as search criteria otherwise if there is some file that is larger than 1M and older than 14 days you would remove ALL pcap files.

Cheers
 
1 members found this post helpful.
Old 06-21-2013, 12:26 PM   #9
pitfall7
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Code:
find /home/user/ -maxdepth 1 -type f -size +1M -mtime +14 -name "*.pcap" -exec rm -rf "{}" \;
is working fine

Thanks a lot for prompt answers.
 
Old 06-23-2013, 10:43 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
gnu find has a -delete action built-in, so there's no need to "-exec rm {}" any more.

If you want to test the command before you commit to it, just run find with the -print option first, of course. This will list out all the files that will be processed when you finally use -delete (or -exec or any other action option).

Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html
 
2 members found this post helpful.
Old 06-24-2013, 01:31 PM   #11
pitfall7
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks David. i've overlooked -delete and -print param.
 
  


Reply


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
Using a single "Find" Command to find files bases on multiple criteria roboxooo Linux - Newbie 6 01-15-2009 05:13 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
What does "2>" do when added to "find" command? O(V)eGA_l2el) Fedora 8 08-06-2007 03:25 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM


All times are GMT -5. The time now is 10:21 AM.

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