Sponsored Content
Full Discussion: Ls -l and rm
Top Forums UNIX for Beginners Questions & Answers Ls -l and rm Post 303038520 by Neo on Thursday 5th of September 2019 05:58:22 AM
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..
 
ZGREP(1)                                                      General Commands Manual                                                     ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 02:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy