Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I recently inherited a linux webserver that the previous admin used to basically dump any and everything into. What I would like to do is get a list of all files in the directory sorted by their last accessed time with the intention of wiping out anything that is not being used at all by the company.

I'm assuming I should probably just go back 3 months as the company is very active with their web services so anything that hasn't been touched at all in the past 3 months is most likely dead weight.

I tried using the answers in this: How to recursively find and list the latest modified files in a directory with subdirectories and times?

But it didn't seem to work well.

As a caveat, my linux admin experience is very junior. I am well versed in deployment and configuration but I've never had to do something like this (I mean, this thing is a real mess and it sorely needs it).

I'm not against putting together a PHP script to run it but, again, I'm not that hot with PHP either so I really need some straightforward help here. Ideally, having a script to run that will also have a delete file option would make my day, or even a BASH command sequence that can have rm included if I decide everything it finds is junk.

Thanks in advance for any and all help you can offer, I really appreciate it.

share|improve this question

migrated from stackoverflow.com Dec 17 '11 at 20:14

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

This sounds right up find's alley:

$ cd $ROOT_OF_JUNKYARD
$ find . -atime 90

Should list all the files accessed within the last 90 days.

NOTE: Keeping track of the atime attribute is somewhat costly for the file system, so some administrators turn it off, for file systems that support doing so (ext2 on Linux does). If your machine is configured to not maintain atime, this approach won't work. :|

Once you're happy all the files listed are junk, you can trivially extend the command to delete the results:

$ find . -mtime 90 -exec rm -i \{\} \;

The -i option to rm makes it interactive, it will prompt you to make sure about every removal. This is to protect you from blowing your foot off, which is otherwise quite easy with recursively applied rm commands.

share|improve this answer
    
@undwind you are talking to a linux newbie... He'll end up deleting a good chunk from his home directory if he was to follow you verbatim without paying attention. –  Ahmed Masud Dec 16 '11 at 15:43
    
@Ahmed: That's a good point, I added the -i option to rm to make it slightly safer. –  unwind Dec 16 '11 at 15:47
    
I think mtime isn't going to help. It can't be the modified time as these are files that have been written a while back but are still in use. It has to be an accessed time. I'm assuming using atime instead of mtime would suit that requirement? Yes? –  MT_Mike Dec 16 '11 at 15:48
    
@MT_Mike: I think so, yes, I edited the answer. –  unwind Dec 16 '11 at 15:54
    
@unwind hehe +1 for -i but you forgot to update the second line to -atime :) –  Ahmed Masud Dec 16 '11 at 15:55
show 3 more comments

First things first :) back up everything!

For purpose of this example i'll assume your files are in /var/www change that to whatever it ACTUALLY is on your system.

    tar fcvz www-backup.tar.gz /var/www 

will create a back up of your existing web environment in a tar file (in case things go wrong you can always retrieve missing files)

Now to find files that have not been accessed in 90 days or so:

    cd /var/www
    find . -atime 90 -type f -ls

Lists the files on screen with modified times.. this way you can make sure that the files that you are trying to delete are INDEED the files that you WANT to delete. You can pipe that to less so that you can scroll up and down:

   find . -atime 90 -type f -ls | less

use up and down arrows to view the list, quit type 'q'.

If everything looks okay:

    pwd

make sure you are still in the right working directory.

    find . -atime 90 -type f echo rm -f {}  \; > doit.sh 

this will create a file called doit.sh which will actually contain the removal commands.

Read it once more:

    less doit.sh 

if all the files look okay for removal then you can run it:

    sh doit.sh
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.