Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Within my ~/docs directory, I want to "touch" all files ending with .txt

How can I do this?

share|improve this question
add comment

1 Answer 1

up vote 14 down vote accepted

With find:

find ~/docs -name "*.txt" -exec touch {} \;
  • You search in ~/docs
  • The name option will match all txt files - exec will execute the command touch on the file name, which is substituted in {}
  • \; ends the command and touch will be called once for each file found

Note:

  • A slight variation, \+ at the end constructs one single command to run touch on all of these files at once. This is not possible with all commands, but it works for touch and saves you a few calls if you have a lot of files that are affected.
share|improve this answer
    
+1 for explaining this command ! –  LoremIpsum Jun 4 '12 at 11:42
3  
{} \+ would be better here... touch can handle many filenames on its command line, so for example, with 10 thousand files and {} \; 10 thousand calls will be made to touch... Using {} \+ will call touch only once (depending on available memory)... Here is an excerpt from find's man-page: -exec command {} + ... The command line is built in much the same way that xargs builds its command lines. There is more detail in the man find documentation. –  Peter.O Jun 4 '12 at 14:36
    
@Peter.O True, just a habit of mine to use the other syntax. –  slhck Jun 4 '12 at 14:46
    
@slhck: And much slower with higher overhead. –  Hello71 Jun 4 '12 at 20:49
add comment

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.