Midhun Midhun - 2 months ago 13
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.


Initially I wrote the below script:

#!/bin/bash CURRENT_DATE=date +'%d%m%Y' File_Path=/bishare/DLSFTP/2016-06 cd /appinfprd/biIA83/Client/Scripts/IRP/ cd cd $File_Path for i in $(find . -maxdepth 1 -type d) ; do echo -n $i": " ;
File_Data=find $i type f |ls -R printf '%s\n' "$D" "|" "$File_Data" /n>> /appinfprd/biIA83/Client/Scripts/IRP/Bishare_File_Data_$CURR‌​ENT_DATE.txt done


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 ' \;