How do I recursively add(or touch) a file into the current directory, as well as all sub-directories?
For example,
I would like to turn this directory tree:
.
├── 1
│ ├── A
│ └── B
├── 2
│ └── A
└── 3
├── A
└── B
└── I
9 directories, 0 files
into
.
├── 1
│ ├── A
│ │ └── file
│ ├── B
│ │ └── file
│ └── file
├── 2
│ ├── A
│ │ └── file
│ └── file
├── 3
│ ├── A
│ │ └── file
│ ├── B
│ │ ├── file
│ │ └── I
│ │ └── file
│ └── file
└── file
9 directories, 10 files
5 Answers 5
How about:
find . -type d -exec cp file {} \;
From man find
:
-type c
File is of type c:
d directory
-exec command ;
Execute command; All following arguments to find are taken
to be arguments to the command until an argument consisting
of `;' is encountered. The string `{}' is replaced by the
current file
So, the command above will find all directories and run cp file DIR_NAME/
on each of them.
-
or, find . -type d -exec touch file {} \;ChuckCottrill– ChuckCottrill2013年10月09日 05:19:10 +00:00Commented Oct 9, 2013 at 5:19
-
5or,
find . -type d -exec touch {}/file\;
Ned64– Ned642015年09月08日 12:24:25 +00:00Commented Sep 8, 2015 at 12:24 -
Also you may want to use a hard/soft link here since changing the file in every location can be a real painFLUSHER– FLUSHER2021年05月08日 09:41:26 +00:00Commented May 8, 2021 at 9:41
If you just want to create an empty file, you can use touch
and a shell glob. In zsh:
touch **/*(/e:REPLY+=/file:)
In bash:
shopt -s globstar
for d in **/*/; do touch -- "$d/file"; done
Portably, you can use find
:
find . -type d -exec sh -c 'for d; do touch "$d/file"; done' _ {} +
Some find
implementations, but not all, let you write find . -type d -exec touch {}/file \;
If you want to copy some reference content, then you'll have to call find
in a loop. In zsh:
for d in **/*(/); do cp -p reference_file "$d/file"; done
In bash:
shopt -s globstar
for d in **/*/; do cp -p reference_file "$d/file"; done
Portably:
find . -type d -exec sh -c 'for d; do cp -p reference_file "$d/file"; done' _ {} +
When wanting to touch
files called $name in the current directory and in all subdirectories, this will work:
find . -type d -exec touch {}/"${name}" \;
Note that the comment by ChuckCottrill to the answer by terdon does NOT work, as it will only touch
the file called $name in the current directory and the directory itself.
It will not create files in subdirectories as requested by the OP, while this version here will.
To actually just create a file, you can use touch
with find
:
$ find . -type d -exec touch {}/file \;
Another example that I was just testing given your's is to create consecutive files in specific sub directory like I have it here.
├── FOLDER
│ ├── FOLDER1
│ └── FOLDER2
├── FOLDER
│ ├── FOLDER1
│ └── FOLDER2
└── FOLDER
├── FOLDER1
└── FOLDER2
I used this command below to create in only the FOLDER2 dir files with consecutive number sequence like file{1..10}
for d in **/FOLDER2/; do touch $d/file{1..10}.doc; done
You must log in to answer this question.
Explore related questions
See similar questions with these tags.