Ls -l and rm


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Ls -l and rm
# 1  
Old 09-05-2019
Ls -l and rm

Hello I would like to have an advice I'm trying to delete some files that were written on a certain date by doing an ls -l | grep "Aug 1" | xarg doesn't work.
You have suggestions Thank you Greetings

Last edited by rbatte1; 09-05-2019 at 12:33 PM..
# 2  
Old 09-05-2019
You can use find with the -exec flag for this.

For example in Linux:

Code:
find /path/to/files* -mtime +30 -exec rm {} \;

Note that there must be spaces between rm, {} and \;

More Explanation:

The first argument is the path to the files you want to delete. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.


The second argument, -mtime, and this switch is used to specify the number of days old that the file is. If you enter +30, it will find files older than 30 days.

The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required at the end the of the command.


NOTE:

Do not run this without testing it first... for example. always run first and look at the output, for example:

Code:
find /path/to/files* -mtime +30  > /tmp/testing_123.txt


After you are happy it is working as you wish, then you can run it.. but honestly, here is what I do:

Code:
mkdir /tmp/files_to_delete
find /path/to/files* -mtime +30 -exec mv {} /tmp/files_to_delete \;

Better to move first and delete later!!! ALWAYS

Get in the very good habit of moving files before you delete them, especially using scripts where one fat finger mistake can ruin your day!

Last edited by rbatte1; 09-05-2019 at 12:36 PM..
# 3  
Old 09-05-2019
It is also doable with
Code:
ls -l | while read a b c d e f g h fn; do if [ "$f" = "Aug" ] && [ "$g" = "1" ]; then echo rm "$fn"; fi; done

If you are happy then remove the echo before the rm.
Caution: this will match all "Aug 1" files, regardless of the year.

BTW the same technique works with find ... -exec echo rm {} \;
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question