Touch command allows you to change timestamp of files. But it allows you to change timestamp of only one file at a time. Sometimes you may need to change timestamp of large number of files in directory. Here is how to touch all files in directory.
How to Touch All Files in Directory
Since touch command allows you to change timestamp of only 1 file, we will use it along with find command to update multiple files at one go.
If you want to touch all files in a particular directory /home/data then use touch command along with find command as shown below.
$ sudo find /home/data -type f exec touch {} +
The above command basically finds all files in /home/data and passes them to touch command for updating.
If you want to update only the folders and not the files in your target directory, use the mindepth and maxdepth option as shown below.
$ sudo find /home/data -maxdepth 1 -mindepth 1 -type d -exec touch {} +
In the above command we use type -d option to touch only directories, and mindepth & maxdepth options to limit recursive find.
That’s it. In this article, we have looked at how to touch multiple files using a combination of find and touch command. The trick is to customize your find command to get the correct list of files & folders you want to touch, and use the ‘-exec touch’ option to update them.
Also read:
How to Exit For Loop in Linux Shell Script
How to Set Default Python Version in Linux
How to Install MediaWiki with NGINX in Ubuntu
How to Send Email With Attachment in Linux
How to Remove (Delete) Symlinks in Linux
Related posts:
How to Copy Files from Linux to Windows
How to Delete Empty Lines from Text File in Linux
How to Find Oldest File in Directory
How to Check Cron Log in Linux
How to Find All Symlinks to File/Directory
How to Change Default Home Directory of User in Linux
How to Lock & Unlock Users in Linux
How to Run Shell Script on Another Server

Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.