Using xargs To Delete Yesterdays File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using xargs To Delete Yesterdays File
# 1  
Old 03-19-2014
Using xargs To Delete Yesterdays File

Using the find command to find files in a directory and automatically delete files older than 24 hours
Code:
find . -mtime +0 | grep file | xargs rm

. Using the find man page but I can't seem to make it work for files that have the previous day's time stamp but are not 24 hours old. Is there a way for find to find files using the date and time stamp? Thanks.
# 2  
Old 03-19-2014
Quote:
Originally Posted by jimmyf
Using the find man page but I can't seem to make it work for files that have the previous day's time stamp but are not 24 hours old.
If that's really what you want, it's the simplest thing in the world -- trash the entire folder daily at midnight. The exact instant the day changes, every file in it will have yesterday's timestamp, guaranteed.

Of course, this means a file which got updated 3 seconds before midnight would be yesterday's file and summarily trashed... Find's behavior is probably more sensible.

You do not need to do find | grep, find has its own features for matching. Also, find | xargs can be unreliable if any of your filenames contain spaces or quotes. Many systems will allow you to use -exec, terminated with '+', to get the same benefit as xargs -- multiple files put into one command -- without the problems.

Code:
find . -mtime +0 -type f -name '*file*' -exec echo rm '{}' '+'


Last edited by Corona688; 03-19-2014 at 06:04 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-19-2014
Yeah, it's a lot easier to just delete those files daily. Cheers!
# 4  
Old 03-19-2014
Corona, your find would also delete the files from today!
But with the following trick you can delete all files with a yesterdays time stamp (and older):
Code:
touch -t `date +%Y%m%d0000` day.start
find .  -type f -name '*file*' \! -newer day.start -exec echo rm {} +

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 03-19-2014
Quote:
Originally Posted by MadeInGermany
Corona, your find would also delete the files from today!
You're right, the + is important.

But that's why I put the 'echo' in these things. Smilie Always test with that first.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exec and xargs mvoe file to directory

Hello, I am trying to move all the file listed by below command to /tmp/testing directory find ./ -maxdepth 1 -type f -mtime +3 I tried using -exec and xargs - none of the combination is working? Please, help (3 Replies)
Discussion started by: saurabh84g
3 Replies

2. Shell Programming and Scripting

How to delete directories and files with xargs?

Hello, i have an dynamical apache_cache that I need to clean all days (older tant one day) with an unix command : find /usr/IBM/HTTPServer/apache_cache/ -name '*' -mtime +1 -print0|xargs -0 rm -r -- but it didn't work. Could you explain me why. So I will put below all my script :... (13 Replies)
Discussion started by: steiner
13 Replies

3. Shell Programming and Scripting

Xargs or Find with output to a file

Hi, I've got to setup a script that will run daily, and find a log file of a certain age, and then compress and transfer this file to a new location. so far i've been able to specify the file i want with: find . -name 'filename.*.log' -mtime 14 -exec compress -vf {} \; this prints out... (4 Replies)
Discussion started by: horhif
4 Replies

4. UNIX for Dummies Questions & Answers

/dev/null a file using xargs

Hi, I'm currently using the following command to wipe clean a log file which can't be straight out RM'd: cat /dev/null > server.log I'm building this into a script and I'm current working on a command to run on each machine to do this automatically however I have multiple files so I need... (11 Replies)
Discussion started by: Deehem
11 Replies

5. Shell Programming and Scripting

echo file deleted by xargs

my command deletes the oldest file from a folder and I'd like to have some type of output when the file is selected or deleted. ls -t -1 | tail -n 1 | xargs rm I'm not sure how to incorporate echo into this. Thanks, (2 Replies)
Discussion started by: evanlivingston
2 Replies

6. Shell Programming and Scripting

File find | xargs grep for pattern file

Folks I've been struggling this with for far too liong now and need your help! I've been happily using grep for a search of a directory, to list the files which contain a string: find . -type f -mtime -5 -print | xargs grep -l 'invoiceID=\"12345\"' Now the list of 'invoiceID' I am... (4 Replies)
Discussion started by: daveaasmith
4 Replies

7. Shell Programming and Scripting

How to find yesterdays file - shell script

Hey guys - i have a script (below) that searches for current files in a particular directory. However i was wondering how to make it search for "yesterdays" file. For instance it looks for a file from yesterday and no older than that. I used stat command to check for file information: ... (6 Replies)
Discussion started by: DallasT
6 Replies

8. Shell Programming and Scripting

Count words on each line in file using xargs

Hi, im having a problem with xargs, i want to cout word of each line in file, and i HAVE to use xargs, i tried: cat file | xargs wc -w .....that uses all words in file like name of files and passed then to wc so it worte wc :somewordformfile is not i afile or directory cat file | xargs -I{} wc... (3 Replies)
Discussion started by: Qwetek
3 Replies

9. Shell Programming and Scripting

Cat all yesterdays file into one file?

I am looking for a command to take files with a specific date and cat them all into big file. I know I can use commands to list all of the files from a certain date. But I want to do that and take those files and make on large files containing all of them. Any help would be great. This is being... (1 Reply)
Discussion started by: Jcheetwood
1 Replies

10. Shell Programming and Scripting

Xargs command outupt to file

Hello, I'm on a mac trying to have the follow cmd list the files after touch, but very unsuccessful. Please can you help. sort $BOTHFILE | uniq -u | xargs -I {} -t touch {} >> $LOGFILE ; BOTHFile contents in form of /directory/file.txt thanx (3 Replies)
Discussion started by: byblos
3 Replies
Login or Register to Ask a Question