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 {} +

This:

$ touch -c -- */

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:

touch -- *(D/)

Try

touch ./*

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.

touch -c ./*

See also the good references in jasonwryan's answer as well as this one.


You shouldn't attempt to parse the output of ls.

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.

Tags:

Bash

Touch