touch multiple files in linux

How to Touch All Files in Directory

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

Leave a Reply

Your email address will not be published.