Timeline for How to make a text file in every subdirectory so that the text file contains name of the directory
Current License: CC BY-SA 4.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 22, 2020 at 16:16 | history | edited | Stalin Vignesh Kumar | CC BY-SA 4.0 | added 2 characters in body |
| May 22, 2020 at 15:59 | comment | added | fra-san | What I meant is: find . -name 'dir_[0-9]*' -type d -execdir bash -c 'name=$1; printf "%s\n" "${name##*/}" > "$name/file_${name##*_}.txt"' mybash '{}' \;. Please have a look at it - I'm not further bothering you, thank you for your patience. | |
| May 22, 2020 at 15:45 | comment | added | Stalin Vignesh Kumar | yes , positional arguments starts with $0 , so i preferred that...thanks... | |
| May 22, 2020 at 15:40 | history | edited | Stalin Vignesh Kumar | CC BY-SA 4.0 | added 7 characters in body |
| May 22, 2020 at 15:36 | comment | added | fra-san | The first argument after bash -c 'name="$1"; ...' is used as the name for the script and is assigned to $0, you need some string there, before '{}'. Otherwise $1 just remains unset (as you have seen). I'd also use "$name/file_${name##*_}.txt" (in double quotes). And give '%s\n' as a format string to printf. | |
| May 22, 2020 at 15:36 | history | edited | Stalin Vignesh Kumar | CC BY-SA 4.0 | deleted 3 characters in body |
| May 22, 2020 at 15:29 | history | edited | Stalin Vignesh Kumar | CC BY-SA 4.0 | added 9 characters in body |
| May 22, 2020 at 15:27 | comment | added | Stalin Vignesh Kumar | no problem , explanation about small things helps a lot ...your points are correct , also i would add that instead of bash -c 'name="$1"; ...' sh '{}' \; we can use bash -c 'name="$1"; ...' '{}' \; just without sh ....will edit my answer... | |
| May 22, 2020 at 15:21 | comment | added | fra-san | (...cont) 3) please refer to my comment to the other answer about why printf should be preferred to echo. 4) Expansions should always be quoted, unless you really need them not to be: think about people who modifies your script to make it also find files whose name may contain spaces. | |
| May 22, 2020 at 15:20 | comment | added | fra-san | Sorry for being a pest here, but: 1) * isn't the only special character in dir_[0-9]\*, the full pattern should be quoted ('dir_[0-9]*'): as it is, it breaks if the current directory contains a directory named e.g. dir_1* (with a literal *). 2) Embedding {} in shell code is an injection vulnerability - it'd be better to use bash -c 'name="$1"; ...' sh '{}' \; (cont...) | |
| May 22, 2020 at 14:41 | history | answered | Stalin Vignesh Kumar | CC BY-SA 4.0 |