Midhun Midhun - 1 year ago 39
Bash Question

how to read sub directories file name in unix

i want to read files names from the directories which starts with 2016. The output should be created to another files.
For example, i have directory /bishare/DLSFTP/ in this i have 2016-06 , 2016-06 , and 2016-07.
All the directories having sub directory as well.
I need 3 output file which will be having the details of directory starts with 2016.

Answer Source

you can use find command with exec param and ls command as below;

find . -name "2016*" -type d -exec bash -c 'ls "{}"> "{}".txt ' \;

ls output is written <directoryName>.txt

To find all directories which starts with 2016 as below and type -d are found only directory;

find . -name "2016*" -type d

To perform ls for each of directories and write outputs to folderName.txt files

-exec bash -c 'ls "{}"> "{}".txt ' \;