1

I have a folder with several sub-folders and files. I work on a server where any files that are older than 60 days are automatically deleted. What are some simple commands to update the access time (atime) of the files so that this kind of deletion does not occur (Sometimes I don't work with the files for more than 60 days, and it's a pain to set up all the folders again) ?

I thought of a very crude way - use

vim *
vim */* ... and so on

to open all the subfolders and then just close them with :q. However this is again a tedious process. Any ideas on how to set up a script or to use a tool other than vim?

5

touch -- change file access and modification times

touch(1)

Note: while you could do touch * this won't recurse into subdirectories and will create a file named * if no files exist. A more robust solution would be to use:

find /path/to/root_dir/ -exec touch -a {} \;

Additionally it seems like you only want to update the modify time of the directories? This is probably better since I'm sure the deletion policy on that server is to save space and you don't want to be keeping all files blindly just to save a few. To only touch directories you can do:

find /path/to/root_dir/ -type d -exec touch -a {} \;

note: touch with no options will modify both the access and modified times to the current time, where -a will only update the access time.

Additionally, if you want to have a command that will just show you which files are close to deletion, you can run something like;

find /path/to/root_dir/ -atime +55

That will list all files in /path/to/root_dir/ which have an access time older than 55 days. This could be useful in letting you know which files are coming up on their max age. You can change 55 to another number if you want to check even further back.

  • And the touch command definitely doesn't modify the file itself right? Only the metadata? – V-Red Jun 14 at 19:21
  • @V-Red: Correct although it will create the file if it doesn't exist. – Jesse_b Jun 14 at 19:22
  • Sorry for my ignorance but I'm kind of new to linux systems; what exactly do you mean by ' /path/to/root_dir/ ' Do I have to simply specify the root directory ? – V-Red Jun 14 at 19:26
  • @V-Red: No worries, that is the path to the directory you would like to make these changes in. So if the files are in your home directory it would be /home/v-red/, etc. You could also specify just . to start at the current directory, but make sure you are actually in the correct directory before doing so. – Jesse_b Jun 14 at 19:28
  • 2
    If @V-Red want to update only the access time (as per question title), should use touch -a. touch should modify both the atime and the mtime, so this would work regardless of which the cleanup routine examines, but touch -a might be less intrusive. – user4556274 Jun 14 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.