Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

1) in the "A" directory:

find . -type f > a.txt

2) in the "B" directory:
cat a.txt | while read FILENAMES; do touch "$FILENAMES"; done

Result: the 2) "creates the files" [i mean only with the same filename, but with 0 Byte size] ok. But if there are subdirs in the "A" directory, then the 2) can't create the files in the subdir, because there are no directories in it.

Question: is there a way, that "touch" can create directories?

share|improve this question
Cross-posted on ServerFault:… – Bobby Jan 17 '11 at 16:05
up vote 4 down vote accepted

What is your desired end result?

If you are trying to duplicate a directory structure there are easier ways. Syncing solutions come to mind first.


rsync -a /source/dir/ /dest/dir/ --include \*/ --exclude \*

This will recreate the entire directory structure. You can then run your touch command to finish the rest.

As previously mentioned we may be able to find a more elegant solution if we know the full problem :)

share|improve this answer

You could use this script. You need to use chmod +x scriptname

#!/usr/bin/env ruby
# touchMakingDirs: a Ruby script to be able to touch a file and optionally create the directories necessary
# by Dan Rosenstark 2013-03-18 
if __FILE__ == $0
  if ARGV.length == 0
    puts "Sorry, include one argument for filename"

  filename = ARGV[0]
  puts "Will touch file #{filename}"
  path = File.dirname(filename)
  #filename = File.basename(filename)  
  `mkdir -p \"#{path}\"`
  `touch \"#{filename}\"`

It's worked in a few small tests. Let me know if there is a test case I've missed. I use it like: touchMakingDirs blah/blah/whatever/who.txt where I've named the script touchMakingDirs and put it in my path.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .