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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-31-2004, 07:13 AM
|
#1
|
LQ Newbie
Registered: Aug 2004
Posts: 7
Rep:
|
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
|
|
|
08-31-2004, 07:57 AM
|
#2
|
Member
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172
Rep:
|
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.
|
|
|
08-31-2004, 08:02 AM
|
#3
|
LQ Newbie
Registered: Aug 2004
Posts: 7
Original Poster
Rep:
|
hi
Thanks for ur reply..
But i am only new to linux. can u give me the details with some example..
- arockia
|
|
|
08-31-2004, 08:12 AM
|
#4
|
LQ Guru
Registered: Jan 2001
Posts: 24,149
|
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
|
|
|
08-31-2004, 08:42 AM
|
#5
|
LQ Newbie
Registered: Aug 2004
Posts: 7
Original Poster
Rep:
|
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
|
|
|
08-31-2004, 08:55 AM
|
#6
|
Member
Registered: Mar 2004
Location: London
Distribution: Mandrake 10
Posts: 172
Rep:
|
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.
|
|
|
08-31-2004, 10:07 AM
|
#7
|
LQ Newbie
Registered: Aug 2004
Posts: 7
Original Poster
Rep:
|
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
|
|
|
08-31-2004, 11:23 AM
|
#8
|
Member
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204
Rep:
|
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 11:04 PM.
|
|
|
08-31-2004, 12:03 PM
|
#9
|
Member
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215
Rep:
|
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/
|
|
|
08-31-2004, 11:05 PM
|
#10
|
Member
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204
Rep:
|
I edited my post significantly. Have a look. I tested this on my machine, and it worked.
|
|
|
09-01-2004, 03:22 AM
|
#11
|
LQ Newbie
Registered: Aug 2004
Posts: 7
Original Poster
Rep:
|
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
|
|
|
09-01-2004, 10:37 AM
|
#12
|
Member
Registered: Aug 2004
Distribution: Mandrake 10.1
Posts: 204
Rep:
|
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 10:39 AM.
|
|
|
All times are GMT -5. The time now is 08:18 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|