touch all folders in a directory
All the answers so far (as well as your example in the question) assume that you want to touch everything in the directory, even though you said «touch all folders». If it turns out the directory contains files and folders and you only want to update the folders, you can use find :
$ find . -maxdepth 1 -mindepth 1 -type d -exec touch <> +
Or if your find implementation doesn’t support the non-standard -mindepth / -maxdepth predicates:
$ find . ! -name . -prune -type d -exec touch <> +
Should work in most shells except that:
- it will also touch symlinks to directories in addition to plain directories
- it will omit hidden ones
- if there’s no directory or symlink to directory, it would create a file called * in shells other than csh , tcsh , zsh , fish or the Thompson shell (which would report an error instead). Here, we’re using -c to work around it, though that could still touch a non-directory file called * .
With zsh , to touch directories only, including hidden ones:
The <> \+ is special find syntax — read the man page for more. I think touch -c ./*/ would work as well.
You should include -maxdepth 1 to the command, so that find doesn’t recurse into subfolders. OP seems to want to touch only those folders in the cwd, not subfolders.
It avoids the unnecessary for loop which would spawn a new process for every single file and works for all file names, even ones with spaces or ones that look like options (like -t ). The only time it wouldn’t work is if you have no (non-dot) files in the directory in which case you would end up creating a file named * . To avoid that, for the specific case of touch most implementations have a -c option (also called —no-create in GNU versions) to not create nonexistent files, i.e.
See also the good references in jasonwryan’s answer as well as this one.
If you run into «argument list too long» type problems it’s probably best to switch to «heavier» tools like find . -type d -maxdepth1 -print0 | xargs -0 touch , or MichaelMrozek’s answer above.
Also, you should quote your «$file» to capture any whitespace. See http://www.grymoire.com/Unix/Quote.html
Something like this might achieve what you are after:
for file in *; do touch "$file"; done
See the first two Bash Pitfalls for a more thorough explanation.
Close but not quite. A touch ./* should work in most cases. There really is no need for the for loop since touch can take multiple files, and you need the ./ to handle files with names like —help properly. This is a good resource on the subject.
Note that * does not capture files and directories whose name begins with a dot. One way to fix that is to enable the dotglob shell option via shopt -s dotglob .
Just in case you wanted to update the timestamps for all files/directories that descend from that directory try either (note my find here below is an alias for what some systems call gfind, i.e., the gnu version of find).
for f in **; do touch -am $f; done
The -am means update both access and modification. Note you likely can’t just do touch -am ** since you can’t run an exec on a list that exceeds getconf ARG_MAX
find . -maxdepth 8 -mindepth 1 -type d -exec touch <> +
What are mindepth and maxdepth levels?
maxdepth levels : Descend at most levels (a non-negative integer) levels of directories below the starting-points. -maxdepth 0 means only apply the tests and actions to the starting-points themselves.
mindepth levels : Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the starting-points.
Generally, we don’t have directories with depth more than 8. Hence is the above command, I have kept the maxdepth as 8.
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.
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).
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.