简体   繁体   中英

Shell Command to Create a text file in all sub-directories

Hi everyone is there any linux command to create a custom text file in current directory and its 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 this

.
├── 1
│   ├── A
│   │   └── downloadedFrom.txt
│   ├── B
│   │   └── downloadedFrom.txt
│   └── downloadedFrom.txt
├── 2
│   ├── A
│   │   └── downloadedFrom.txt
│   └── downloadedFrom.txt
├── 3
│   ├── A
│   │   └── downloadedFrom.txt
│   ├── B
│   │   ├── downloadedFrom.txt
│   │   └── I
│   │       └── downloadedFrom.txt
│   └── downloadedFrom.txt
└── downloadedFrom.txt

9 directories, 10 files

i want to make a txt file in all directories (txt file content: my website name) is there any way? or is there any way to overwrite files content in sub directories

for dir in `find . -type d` ; do echo "mywebsite.com" > $dir/downloadedfrom.txt ; done

Is this what you are looking for, sort of? The find command iterates through all directory type items in the current directory, and then we echo the contents of the file to a new file in all those directories. However, this will overwrite stuff that's already in those directories, so watch out!

Using only a single find command:

find . -type d -exec sh -c 'echo "yourwebsite" > "$1"' _ {}/downloadedFrom.txt \;

or, alternatively, using bare bash with globstar option (since bash 4):

shopt -s globstar
for dir in . **/; do echo "yourwebsite" > "$dir/downloadedFrom.txt"; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM