Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to update the timestamps of all folders in the cwd using this:

for file in `ls`; do touch $file; done

But it doesn't seem to work. Any ideas why?

share|improve this question

3 Answers 3

up vote 17 down vote accepted

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 {} \+
share|improve this answer
    
What is the '{} \+' about? –  javamonkey79 Oct 8 '11 at 1:25
2  
The {} \+ is special find syntax - read the man page for more. I think touch -c ./*/ would work as well. –  jw013 Oct 8 '11 at 2:12
2  
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. –  Steven Monday Oct 8 '11 at 2:53
1  
@jw013 It would -- you keep attaching good ideas to other people's answers :) –  Michael Mrozek Oct 8 '11 at 4:22

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.

share|improve this answer
1  
What will happen if you have 1 million folders in the cwd? –  Steven Monday Oct 8 '11 at 2:54
    
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. –  jw013 Oct 8 '11 at 3:02

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.

share|improve this answer
2  
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. –  jw013 Oct 8 '11 at 0:32
    
Why does '*' work? –  javamonkey79 Oct 8 '11 at 0:33
    
@javamonkey79 It's shell globbing. –  jw013 Oct 8 '11 at 0:35
1  
@jw013 Excellent point: and thank you for the wheeler link. –  jasonwryan Oct 8 '11 at 0:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.