I have the following command that finds the total number of files in my root directory:
command 01:
ls -dq *dog* | wc -l
I also have a command to create multiple folders using a numeric sequence:
command 02:
mkdir -p {0..3}
I know in the root directory there are 3 files that have the string "DOG" in your name.
But I would like to use the 01 command in other root folders and that counted how many files have "DOG" in your name, store the result in variable result and used this variable in place of "3" as seen in command 02.
Below all my possible attempts without having been able to create subfolders 1, 2 and 3:
result=$ls -dq *dog* | wc -l) mkdir {0..$result}
result=$(ls -dq *dog* | wc -l) | mkdir {0..$result}
ls -dq *dog* | wc -l | xargs I{} mkdir {0..{}}
ls -dq *dog* | wc -l | while read file; do mkdir -p {1.."$file"}; done
But for any attempt above I only have two results: or is created a folder called {0..3}
or nothing happens besides receiving a printing of the result of
ls -dq *dog* | wc -l
Solved so bellow:
Looking at this answer it was possible to build a model capable of getting the desired result:
eval "mkdir -p {1..`ls -dq *dog* | wc -l`}"
While strictly is not the most appropriate answer because I had proposed that the problem was to "use a variable inside mkdir {1 .. $variable}
"