LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-31-2004, 06:13 AM   #1
arockia
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Rep: Reputation: 0
Finding six month old files


Hello

i need some help.

I have to write one shell script.

in this shell script i have to found six months old file and compress that files into zip then i have to remove the six months old files.


IF ANY ONE KNOW HOW TO DO THIS PLEASE LET ME KNOW .
IT'S VERY URGENT
 
Old 08-31-2004, 06:57 AM   #2
BluePyre
Member
 
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172

Rep: Reputation: 30
Well heres what the file needs to do:
ls -l and pipe it into grep to get the appropriate timestamp information (I can only think of how to make it find anything in between six months ago and 12 months ago at the moment).

You could probably then redirect that to a file and then using something like geline (in C++) read the line to a string, save the substring beginning from the back until it reaches a colon (so you know that you've collected the file or directory name (spaces and all) ), and then remove the first 4 characters (a colon then 2 numbers and then a space). You'll be left with the file or directory name. You could then add that to a tar archive and then rm -rf on the filename to remove it.
 
Old 08-31-2004, 07:02 AM   #3
arockia
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
hi
Thanks for ur reply..

But i am only new to linux. can u give me the details with some example..

- arockia
 
Old 08-31-2004, 07:12 AM   #4
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Something like this should or might work, just cd into the directory you want to look in and do:

find . -mtime +(#ofdays)

Say I wanted to find any file that's been modified 7 or more days ago, I'd do a:

find . -mtime +7

If you only want to find files and exclude directories:

find . -type f -mtime +7

And then you can use the -exec to execute particular commands.. and so on.

Its all in the man pages if you want more options.

man find
 
Old 08-31-2004, 07:42 AM   #5
arockia
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
hi

in the previous reply you have specified to use ls -l. but know you have specifed with find examples.
Can u please explain every steps in the previous repliy with example.

the example with find command is very good explanation.

I need the script for the following steps/

find the files which last accessed time/modified time is before 6 months
then we have to zip that files into a zip files
then we have to remove the files.

if u give the example for the above steps then it will be very useful for me.

please don't mind i have asked every steps..

-thanks
arockia
 
Old 08-31-2004, 07:55 AM   #6
BluePyre
Member
 
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172

Rep: Reputation: 30
The find method is much better and much simpler too. Why don't you read up on bash scripts and then check the man pages for find? Getting someone to give you the solution right in your face is hardly ever the best way.
 
Old 08-31-2004, 09:07 AM   #7
arockia
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
hi

u are correct.. i have to found the solution for the problem i don't try to get everything from others..

i have used the following commands

find . *.* -mtime -180 | xargs tar -rfc HalfYear_incremental.tar

but whenever i have used the find command the last accessed time of every files has changed , is it correct?

But whenever i have given 180 i think this is the number of days, but i got the files listed which i have include last week itself..
I don't know anything about linux that's why i have asked the steps in the previous reply , please don't mind.

is anything wrong with the command i have used please inform me and tell me how we can remove the files in the same command itself.

-arockia
 
Old 08-31-2004, 10:23 AM   #8
Linux24
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204

Rep: Reputation: 30
Edited some mistakes I made in here. I put the edited lines in bold and underlined the parts I changed.

Do you want to find files that have never been accessed in the last 180 days, or do you want files that have not been modified in the last 180 days? Those are two different things. Plus, looking at your command, I don't think it will work. Try this for files that have not been accessed in the last 180 days (meaning no one has opened the files at all:

find / -name '*' -atime +179

That will find all files that have been accessed 180 days ago or longer.

Now add the -exec option to execute tar/gizip on it. Tar and gzip have an option to delete the originals after you are done zipping them up.

find / -name '*' -atime +179 -type f -exec tar cvfz --remove-files files_180days_older.tar.gz {} \;

To use exec, you must include {} to represent the found files from the find operation and end the command of -exec with a semi-colon. The semi-colon must be escaped from the shell, so \; comes at the end of the command in -exec.

But this will only process the last file in the list into that tar file, so it will not work. When you use -exec, it handles one found file at a time, so you end up with a tar file with only the last file in it and all the ones you found deleted. Very dangerous - not the way to go.

Instead, use xargs and a pipe to pass it more gracefully.


find / -name '*' -atime +179 -type f | xargs tar cvfz --remove-files files_180days_older.tar.gz

That should actually work, however, when I did it, the tarball had an annoying directory structure in it instead of just dumping the files. I guess you could use the option in tar to ignore paths and directory structure if you wanted.


If you want to use the create date instead of the access date, then atime -> ctime. If you want the modified date, then atime -> mtime.

Before you run this command, you should run parts of it to see if you get what you expect.

First just run the find part and see if it finds what you want my outputing to a text file:

find / -name '*' -atime +179 -type f

Take a look at the contents and test for success. If it finds what you want, then add the tar portion to the end without the remove-files option, and see if that works. Once you have run it two or three times successfully, then you can run the whole thing.

Never run anything that may be destructive to files like this all in one execution - that's a great way to lose data.

Get ready to reinstall if you break something running this as root from the root directory.

Last edited by Linux24; 08-31-2004 at 10:04 PM.
 
Old 08-31-2004, 11:03 AM   #9
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
It would help you understand what you want to do if you downloaded and read
abs-guide-2.8.tar.bz2
at:
http://212.219.56.131/sites/www.ibib...too/distfiles/
 
Old 08-31-2004, 10:05 PM   #10
Linux24
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204

Rep: Reputation: 30
I edited my post significantly. Have a look. I tested this on my machine, and it worked.
 
Old 09-01-2004, 02:22 AM   #11
arockia
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
hi i have used like the following


find . -name '*' -type f | xargs tar cvf --remove-files files_180days_older.tar.gz

but the files have created with the name --remove-files not the name with
files_180days_older.tar.gz.
if i have used as u specoified in the option cvfz i got the invalid option -z error i have.
Another one problem with the find is whenever we have used find command, the lastaccess time of the file has changed.. we need to avoid this.
is there any other way without this problem( changing the last access time)

-arockia
 
Old 09-01-2004, 09:37 AM   #12
Linux24
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204

Rep: Reputation: 30
Quote:
Originally posted by arockia
hi i have used like the following


find . -name '*' -type f | xargs tar cvf --remove-files files_180days_older.tar.gz

but the files have created with the name --remove-files not the name with
files_180days_older.tar.gz.
You will have to experiment on your side, that's how we all learned this stuff. Now that you know that find, xargs, and tar can be used to do what you want, read the man pages for each and continue experimenting with commands until you get the one you want, then come back here and post what you learned for others to find.

My first idea would be to move the --remove-files option to after tar but before cvfz option.


Quote:
if i have used as u specoified in the option cvfz i got the invalid option -z error i have.
The z option passes the tarball through gzip. What distribution of Linux are you running? Most Unix tar commands allow passing through gzip using tar z.

Quote:
Another one problem with the find is whenever we have used find command, the lastaccess time of the file has changed.. we need to avoid this.
is there any other way without this problem( changing the last access time)

-arockia
Then do not search on access time, but instead on "modified time" = mtime.

Either way, I don't think you want to do this, because many programs and files that Linux ships with have created and modified dates greater than 180 days ago, and if you remove them and tar them up, you will break your operating system.

Be careful and only run this in a directory with a -maxdepth set on the find correctly.

Test using "echo" before you test using tar with --remove-files.

Last edited by Linux24; 09-01-2004 at 09:39 AM.
 
  


Reply



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
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
delete files older then a month? Red Squirrel Linux - Software 1 10-05-2005 10:54 PM
Finding files brentos Linux - General 3 03-22-2004 08:24 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM
Finding files Nicksan Linux - Software 3 06-30-2003 07:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:45 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
Open Source Consulting | Domain Registration