To remove files before certain date | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

To remove files before certain date

UNIX for Dummies Questions & Answers


Tags
faq

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2008
Attitude Attitude is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 23 October 2008, 9:38 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question To remove files before certain date

I juz started to pick up unix nott long.
What i am gonna do here is to try and remove some files before a date. (example 1st Oct 2008)
Format of files name: fileA_2008MMDD
I did a ls -lrt to list all the files
Followed by rm 200801**
..
..
..
rm 200809**

Is there a more efficient way to this?

Appreciate for ur help
Thz a million
Sponsored Links
    #2  
Old 10-23-2008
palsevlohit_123's Avatar
palsevlohit_123 palsevlohit_123 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 2 May 2013, 8:00 AM EDT
Location: India-Chennai
Posts: 132
Thanks: 0
Thanked 7 Times in 7 Posts
you can use the find command with mtime option. it will be specific.
Sponsored Links
    #3  
Old 10-23-2008
Attitude Attitude is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 23 October 2008, 9:38 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by palsevlohit_123 View Post
you can use the find command with mtime option. it will be specific.
find "url of directory" -name "*.txt" -mtime -60 -exec rm {} \;

something like that and it should work?

Thks
    #4  
Old 10-23-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 28 November 2014, 4:48 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,338
Thanks: 45
Thanked 839 Times in 662 Posts
Definitely the "find" command is it, as palsevlohit_123 already said.

Use either the "-mtime", "-ctime" or "-atime" clause to select based upon modification time, creation time or access time of the files or use the "-newer" clause to compare files with a reference files time stamp.

How to use "find"

"Find" works basically like that: you specifiy a starting directory and "find" will find all files and directories from there on. With additional clauses you can assign an action to every item found that way. Example:


Code:
find /home/myuser -print

This will traverse to search all files and directories in the directory "/home/myuser" and then execute the "-print" clause for every item. "-print" only prints the files name and is the default action, so we wouldn't had to specify it, but its a good example to show the mechanism.

Try the same and replace "-print" with "-ls". This will basically work the same way, but the output format will be different. It is like the output of "ls -ails".

You can now specify additional criteria to exclude or include certain groups of files. Every clause you specify returns a logical value. If the value returned is "TRUE" the file is included, otherwise it is excluded. "-print" and "-ls" return always TRUE, therefore they do nothing to limit the result set, but others do:


Code:
find /home/myuser -type d -print

This will print only the directories, not the filenames. The reason is the "-type" clause, which gets a type of filesystem item (in our case: "d" for "directories") and returns "TRUE" only for these. "Find" will still find all items in the directory, then present each to "-type", which will return "TRUE" for the directories and "FALSE" for all the other things (files, links, devices, etc.). The remaining items (the ones for which "-type" has returned "TRUE") are now being presented to "-print" which will print their names.

You can also combine several exclusion clauses. Example: you want all files, but only the ones with a starting character of "x" in their name. You want the output in the long style:


Code:
find /home/myuser -type f -name "x*" -ls

There is a special clause which allows to do some work on the items found that way: the "-exec" clause. "-exec" gets a complete command like it would be entered on the command line. The filename involved is represented by "{}" in this. find will now execute this commandline substituting the filename for "{}" and use the returncode as the returncode of the clause:


Code:
find /home/myuser -name "x*" -type f -exec echo "This is the file named {}" \;

The reason for the "\;" is that the shell has to know where the "template commandline" stops and the normal commandline resumes, so this is obligatory.

Another example: You want to remove all the leftover "*bak" files from your home directory which your editor leaves when editing a file. Use the following command which you by know sould be able to interpret:


Code:
find /home/myuser -type f -name "*bak" -exec rm -f {} \; -print

Read the man page of "find" to find more phantastic clauses to work with.

One suggestion: i said that "-exec" returns the commands error level as return value (an error level of 0 means "TRUE", everything else "FALSE"). This way you can use also an "-exec" clause to include or exclude arbitrary files, directories or other filesystem items in your set. You can also use the same clause several times. Therefore, if the clauses of "find" are not suited to your problem you can easily contruct your own criteria by giving the file in question to a script, which returns TRUE or FALSE depending on some arbitrary criteria, then use another "-exec" clause to carry out some fancy action on the files selected this way:


Code:
find /some/dir -exec script_returning_TRUE_or_FALSE {} \; -exec some_command {} \;

I hope this helps.

bakunin
Sponsored Links
    #5  
Old 10-23-2008
Attitude Attitude is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 23 October 2008, 9:38 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Thks a lot for help!!
find command is a useful one hah
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove files based on date kanakaraju UNIX for Dummies Questions & Answers 1 10-21-2010 10:13 AM
Remove files which created date before 10 days on HP-UX murad_fayez Shell Programming and Scripting 1 12-21-2008 06:05 AM
Sorting Files by date and moving files in date order rebel64 Shell Programming and Scripting 2 03-11-2008 01:45 PM
how to remove files with a date justintime Shell Programming and Scripting 5 09-10-2006 10:50 PM
Remove files based on date hshapiro UNIX for Dummies Questions & Answers 4 12-09-2005 12:21 PM



All times are GMT -4. The time now is 03:12 PM.