Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch a file - update it's modified time stamp

Is there an efficient way to update the last modified property of a file on the iPad's filesystem i.e. the unix touch command?

I've had a look at NSFileManager but no luck.

like image 658
deanWombourne Avatar asked Aug 18 '10 12:08

deanWombourne


People also ask

Does touch change timestamp?

By default, touch sets both the date of last file modification and the date of last file access to the current time. It maintains the correct release times for software and is useful with the make command.

How do I edit a file without changing the timestamp?

We can use one of the touch command's option -r (reference) to preserve file timestamps after editing or modifying it. The -r option is used to set the timestamps of one file to the timestamp values of another. As stated already, if we change the contents or metadata of this file, the timestamps will also change.

What is a modified timestamp?

Modified Time. The modified timestamp contains the time the file's data has been changed. It means when we make changes to the actual contents of the file, the file system will update the modification time.

Will touch overwrite an existing file?

touch cannot overwrite the contents of an existing file, but the redirect will. If boot. txt does not exist, it is created.


2 Answers

OK, so I'm answering my question just after I've asked it :(

I've found the method setAttributes:ofItemAtPath:error: in the NSFileManager class and I can specify the modified timestamp there.

Thanks anyway!

like image 86
deanWombourne Avatar answered Sep 30 '22 17:09

deanWombourne


To update the modification timestamp (which is, for example, shown in the output of ls -l) of filePath to now:

NSError* error;
NSFileManager* fileManager = [NSFileManager defaultManager];

if (![fileManager setAttributes:@{NSFileModificationDate:[NSDate date]}
                   ofItemAtPath:filePath
                          error:&error]) {
    NSLog(@"Couldn't update modification date: %@", error);
}
like image 32
Ashley Avatar answered Sep 30 '22 18:09

Ashley