How to change a file’s last modified date

Ever had the need to change a file’s last modification date? Maybe not and you may be thinking why would you do that?

The reason I wanted to do that was because I was writing a Artisan command to clean up files in a given directory where the modification date was older then one day.

But while writing this command all the files in the directory I wanted to clean up where created and modified the last time on the same day as writing the command. So my script won’t do anything and waiting a day to test it was no option.

If you want to changes the last modification date of a file you can simply run the following command from your terminal:

touch -mt YYYYMMDDhhmm path/to/some/file.jpg

You can also run the command to do this for all the files in a direcotry:

touch -mt YYYYMMDDhhmm path/to/some/dir/*