I have a random folders names structure directories as below:
Main_directory |__folder1 | |__file.txt | |__Case4 | |__file.txt | |__setup0 | |__file.txt | |__Case3 | |__file.txt
Each file.txt has one value e.g. -300
. I am trying to loop through those folders with the random naming and collect the data in one text file in the Main directory maintaining the original directory name as below:
Main_directory |__folder1 | |__file.txt e.g. value of -300 | |__Case4 | |__file.txt e.g. value of 1000 | |__setup0 | |__file.txt e.g. value of -0.0005 | |__Case3 | |__file.txt e.g. value of -349.666 | results.txt
The result.txt
should contain as below:
folder1 -300 Case4 1000 setup0 -0.0005 Case3 -349.666
Advertisement
Answer
In oneliner form:
for f in $(find . -mindepth 1 -maxdepth 1 -type d -printf "%Pn"); do printf $f >> result.txt; printf " " >> result.txt; cat $f/file.txt >> result.txt; done
In readable many liner form:
for f in $(find . -mindepth 1 -maxdepth 1 -type d -printf "%Pn"); do printf $f >> result.txt; printf " " >> result.txt; cat $f/file.txt >> result.txt; done
How it works:
- Find all sub-directories, that are exactly sub-directories(not sub sub-directories), and print them without the leading
./
:
find . -mindepth 1 -maxdepth 1 -type d -printf "%Pn"
- Append the filename to
result.txt
, without adding a newline:
printf $f >> result.txt;
- Add a space:
printf " " >> result.txt;
- Add the
file.txt
contents:
cat $f/file.txt >> result.txt;
One disadvantage of this approach is that it requires file.txt
to have a trailing newline. You can fairly easily fix this with a printf "n" >> result.txt
.