I need to create a script to scan folders in a directory and take the name of each folder, make a .txt file, name it with the name of the folder and put inside that folder.
For example: A directory that has 3 folders in it named "1" "2" and "3" I want to create a .txt file in each folder named with the name of that folder so that folder 1 has "1.txt" in it folder 2 has "2.txt" in it etc..
I managed to do that with this script:
for i in $(ls -d */); do touch "$i $(basename $i).txt" ; done
The problem is I want to do the same thing but with folders that has spaces in their names. For example: If I have a folder named "Test Me" It will give an error saying something like this:
Me: no directory with that name
It will see only the word "Me" and treat it as a folder and not "Test Me" as a whole.
How can I do that ?
Update
This is the solution, it is commented below by TAAPSogeking (Thanks again to him)
for dir in *; do [ -d "$dir" ] && touch "$dir/$dir.txt"; done
ls
is only causing problems. In this case you should simply use*
, sofor i in */; do ...
– sudodus Aug 01 '22 at 22:40for dir in */; do touch $dir${dir%/}.txt; done
(@sudodus) – dave_thompson_085 Aug 02 '22 at 03:06