How to touch all files in subtree recursively Linux?

Touch is a command in Linux that is used to create and modify the timestamps of a file. Additionally, it can create files with no content and a timestamp specified by the user. Often, we have a directory in Linux with multiple subdirectories and files, and we wish to change the timestamps of each subdirectory and file.

In this article, we will look at how to touch all such files and subdirectories recursively, along with a comprehensive breakdown of the commands that are used for this purpose.

What is the touch command, and how to use it?

The purpose of the touch command in Linux is twofold; it can be used to create empty files with a timestamp specified by us, and it can be used to modify and change the timestamps of already existing files. It is a very useful command since it can change a wide range of timestamps, including but not limited to access (the last time the file was accessed) and modified (the last time the file was modified) timestamps. We will list some of the common uses of the touch command and their formats.

  • touch filename: creates an empty file with the name specified by the ‘filename’ argument.
  • touch -a: used to change the last access timestamp of a file.
  • touch -c: checks if a file with the specified name exists or not. If such a file does not exist, it avoids creating it.
  • touch -c -d: used to update the access and modification time.
  • touch -m: used to update the last modification time of the specified file.
  • touch -r: used to store the timestamp of another file as a reference.
  • touch -t: used to create a file with a specified timestamp.

Now that we have got our readers acquainted with the fundamental uses of the touch command, we will move forward to learning how we can use it to touch all files and subdirectories in a subtree recursively.

Step 1 – Open a terminal

We will be using the command-line interface to touch all files in a subtree recursively. To begin, we will open a new command terminal. You can open a terminal window either by accessing it through the application menu or by pressing the keyboard shortcut Ctrl + Alt + T.

Step 2 – Execute the touch command

For demonstration purposes, we will be taking an example directory to freely experiment with the touch command. In this case, our directory is located in ~/example. Obviously, you should follow along with the directory of your choice to ensure maximum learning. You should change it according to the directory you want to touch. After setting the correct directory, all that is left to do is to execute the following command to touch all files and subdirectories in the specified folder.

$ find ~/example -exec touch {} ;

As we saw earlier, there are many variants of the touch command that one can execute to achieve different purposes. One such case is when the user wants to print all the directories in question. This, too, can indeed be done through the touch command. All that needs to be done is a little bit of modification in the syntax and the addition of “-print” to the command. Run the command below to test out the printing feature (It is once again advised that you make sure that the address is set to your desired directory that needs to be touched).

$ find ~/example -print -exec touch {} ;

As you can see from the picture above, the directories are not only just touched but also printed as we ran the previous command.

Breakdown of the commands

Now that we have covered the main part of the process, those that are curious about how the previous commands worked can stick around to learn more details about them. This section will break down each part of the commands we executed one by one and go through their functionalities.

First of all, let us take a look at the find command.

The find command is used to look for files and directories in a particular directory. Some possible methods of its usage are given below.

  • find -name: used to search for a particular filename in a directory.
  • find -iname: used to conduct a case insensitive name search.
  • find -f: used to find a regular file.
  • find -d: used to find directories.
  • find -l: used to find symbolic links.
  • find -c: used to find character devices.
  • find -b: used to find block devices.
  • find -s: used to find sockets

Now, you should understand what the find command does and how to use it. Essentially, it lists all the files and directories at an address given by the user.

Moving forward, we specify the directory where we want to search for files and directories.

-print tells the terminal to show the files and directories that the command finds.

-exec touch executes touch command on each of the found files and directories.

{} makes sure that the names of the files and the directories that they find command lists are being forwarded to touch command as arguments.

; is used to escape from the exec command.

With that being said, we have covered the purpose behind every part of this command.

Conclusion

This article looked at how to touch all files in a subtree recursively using find and touch commands. Moreover, an overview of the touch and find command is presented, along with a breakdown of their combination used to list and touch files in a subtree recursively.