LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Finding six month old files (https://www.linuxquestions.org/questions/linux-software-2/finding-six-month-old-files-224732/)

arockia 08-31-2004 06:13 AM

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

BluePyre 08-31-2004 06:57 AM

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.

arockia 08-31-2004 07:02 AM

hi
Thanks for ur reply..

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

- arockia

trickykid 08-31-2004 07:12 AM

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

arockia 08-31-2004 07:42 AM

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

BluePyre 08-31-2004 07:55 AM

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.

arockia 08-31-2004 09:07 AM

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

Linux24 08-31-2004 10:23 AM

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.

Dave Kelly 08-31-2004 11:03 AM

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/

Linux24 08-31-2004 10:05 PM

I edited my post significantly. Have a look. I tested this on my machine, and it worked.

arockia 09-01-2004 02:22 AM

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

Linux24 09-01-2004 09:37 AM

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.


All times are GMT -5. The time now is 05:09 PM.