LINUX: Help me recursively search an entire directory for last accessed time on files

bashlinux

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: https://stackoverflow.com/questions/5566310/how-to-recursively-find-and-list-the-latest-modified-files-in-a-directory-with-s

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.

Best Answer

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