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..
 
pwd(1T) 						       Tcl Built-In Commands							   pwd(1T)

__________________________________________________________________________________________________________________________________________________

NAME
pwd - Return the absolute path of the current working directory SYNOPSIS
pwd _________________________________________________________________ DESCRIPTION
Returns the absolute path name of the current working directory. EXAMPLE
Sometimes it is useful to change to a known directory when running some external command using exec, but it is important to keep the appli- cation usually running in the directory that it was started in (unless the user specifies otherwise) since that minimises user confusion. The way to do this is to save the current directory while the external command is being run: set tarFile [file normalize somefile.tar] set savedDir [pwd] cd /tmp exec tar -xf $tarFile cd $savedDir SEE ALSO
file(1T), cd(1T), glob(1T), filename(1T) KEYWORDS
working directory ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+-----------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-----------------+ |Availability | SUNWTcl | +--------------------+-----------------+ |Interface Stability | Uncommitted | +--------------------+-----------------+ NOTES
Source for Tcl is available on http://opensolaris.org. Tcl pwd(1T)
All times are GMT -4. The time now is 08:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy