LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2006, 04:50 AM   #1
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
file deletion according to date


hi guys! how do i delete files according to date? e.g. in the list below i want to delete all files dated "Jul 21". i have search this forum but got no luck of finding any thread about this.

drwxr-xr-x 2 giov users 4096 Jul 21 16:53 fileabc/
drwxr-xr-x 3 giov users 4096 Jul 21 16:55 testing/
drwx------ 2 giov users 4096 Jul 24 18:02 ambot/
-rw-r--r-- 1 giov users 4921 Jul 25 13:10 gio
drwxr--r-- 4 giov users 4096 Jul 26 19:25 yong/
-rw-r--r-- 1 giov users 1019 Jul 27 11:14 sql\ statement
-rw-r--r-- 1 giov users 6761 Jul 27 13:56 help.txt
-rw-rw-rw- 1 yong users 1965 Jul 28 14:50 file123
-rw-rw-rw- 1 giov users 15499 Jul 28 15:17 notes.txt
drwxr-xr-x 2 giov users 4096 Jul 28 15:55 aaaaaa
 
Old 07-28-2006, 05:36 AM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Using the "Unix Method": combine simple tools.

First you could use find, for example, to produce a list of the files you'd like to remove. Then you could use this list with rm (maybe through find's -exec) to remove the files; and after you got the idea clear, you could write a small script that removes the wanted files, and maybe even asks for the date to remove.

With find you can also choose files older than date X and then use your head a little to combine this information with rm to remove the files. A one kind of example follows further below.

Read
Code:
man find
man ls
man grep
man rm
And think a bit - brain work is the best work. If you want to learn to do things, it's best to find out yourself (and reading some of the above manpages surely gets you to the point - I'd bet you complete the task yourself in a few minutes if you read the above stuff).

EXAMPLE: to remove every file on the current directory (and in it's subdirectories) that has been accessed two days ago, you could use

Code:
find . -atime +1 -exec rm \{\} \;
Here it reads: "find from the current directory, ., every file that is more than +1 days old (two days is more than +1 days), then execute the command "rm" on those files. The \ is in front of {, } and ; to protect them from the shell interpreter.

For more options on finding files, read
Code:
man find
In the above example, atime stands for "access time", but you could use mtime for "modification time" as well.

Last edited by b0uncer; 07-28-2006 at 05:57 AM.
 
Old 07-28-2006, 06:20 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
for ls -l you can try:
Code:
MONTH="Jul"
DAY="21"
ls -l | grep " ${MONTH} *${DAY} " | sed s/".* ${MONTH} *${DAY} *[^ ]* "//
Edit: '*${DAY} [^ ]*' to '*${DAY} *[^ ]*'

Last edited by konsolebox; 07-28-2006 at 07:32 AM.
 
Old 07-28-2006, 07:07 AM   #4
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Original Poster
Rep: Reputation: 20
thanks bouncer! i really appreciate your response to my Q but i guess you just dont get my point.. but thanks to konsole ur example is much closer to what i wanted.. thanks to both of you!!
 
Old 07-28-2006, 07:34 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
to delete the files from the output:

Code:
MONTH="Jul"
DAY="21"
IFS=$'\n'
for a in $(ls -l | grep " ${MONTH} *${DAY} " | sed s/".* ${MONTH} *${DAY} *[^ ]* "//); do
    rm "$a"
done
IFS=$' :\t\n'

Last edited by konsolebox; 07-28-2006 at 07:35 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File and folder deletion Rick069 Linux - Software 5 02-13-2006 10:08 PM
File Deletion mhkhalqani Linux - General 1 10-12-2005 04:08 AM
Automatic file deletion Azhrarn Linux - Newbie 5 08-30-2005 06:01 AM
secure file deletion shawn59 Linux - Security 2 06-12-2005 10:00 PM
Problem with file-deletion after a while JohnWayne Linux - General 3 07-26-2004 01:57 PM


All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration