LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "Find" command (https://www.linuxquestions.org/questions/linux-newbie-8/find-command-4175466759/)

pitfall7 06-20-2013 11:16 AM

"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

jdkaye 06-20-2013 11:22 AM

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

pitfall7 06-20-2013 11:25 AM

Hi,

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

Thx.

szboardstretcher 06-20-2013 11:29 AM

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 {} \;


pitfall7 06-20-2013 11:37 AM

Thanks for the hint.
I am about to check this out and give you feedback.

Madhu Desai 06-20-2013 11:38 AM

Quote:

Originally Posted by pitfall7 (Post 4975485)
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.

pitfall7 06-20-2013 11:41 AM

yeah good point as well...

Thanks

gdejonge 06-21-2013 06:48 AM

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

pitfall7 06-21-2013 11:26 AM

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.

David the H. 06-23-2013 09:43 AM

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

pitfall7 06-24-2013 12:31 PM

Thanks David. i've overlooked -delete and -print param.


All times are GMT -5. The time now is 09:48 PM.