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?


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 :)


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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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