Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

How to check files recursively from multiple directories and get latest file in bash script

HI Experts,
How to check files recursively and get the latest file from the highest numbered directory.

Scenario:
1. We will have numbered directories 11110, 11114, etc.
2. Each above directory will have sub directories called changed and bkp
3. Each changed directory will have set of files...file1.txt, file2.txt, file3.txt etc. (bkp directory should not be considered in this logic)
4. We need to get latest numbered files in a output.txt

Based on the attached sample zip file below is the output
/tmp/test/11110/changed/file2.txt
/tmp/test/11114/changed/file5.txt
/tmp/test/11130/changed/file1.txt
/tmp/test/11130/changed/file3.txt
/tmp/test/11130/changed/file4.txt

Please help me with bash funciton where it accepts the directory path e.g /tmp/test/
Avatar of enthuguy
enthuguy
Flag of Australia image

ASKER

Avatar of huacat
huacat

Try below command:
please remember changed the ./test to your path
for l in `find ./test -type d -name "changed"`; do find $l -type f -name "file*" | sort -r | head -1; done

Open in new window

hi Huacat,
could you help with this

Below is the out from your suggestion
./test/11110/changed/file5.txt
./test/11114/changed/file5.txt
./test/11130/changed/file4.txt

Based on the attached sample zip file below is the output
/tmp/test/11110/changed/file2.txt
/tmp/test/11114/changed/file5.txt
/tmp/test/11130/changed/file1.txt
/tmp/test/11130/changed/file3.txt
/tmp/test/11130/changed/file4.txt



Below is the structure of test directory
+--- test
|   +--- 11110
|   |   +--- bkp
|   |   |   +--- file1.txt
|   |   |   +--- file2.txt
|   |   |   +--- file3.txt
|   |   |   +--- file4.txt
|   |   |   +--- file5.txt
|   |   +--- changed
|   |   |   +--- file1.txt
|   |   |   +--- file2.txt
|   |   |   +--- file3.txt
|   |   |   +--- file4.txt
|   |   |   +--- file5.txt
|   +--- 11114
|   |   +--- bkp
|   |   |   +--- file1.txt
|   |   |   +--- file3.txt
|   |   |   +--- file4.txt
|   |   |   +--- file5.txt
|   |   +--- changed
|   |   |   +--- file1.txt
|   |   |   +--- file3.txt
|   |   |   +--- file4.txt
|   |   |   +--- file5.txt
|   +--- 11130
|   |   +--- bkp
|   |   |   +--- file1.txt
|   |   |   +--- file2.txt
|   |   |   +--- file3.txt
|   |   |   +--- file4.txt
|   |   +--- changed
|   |   |   +--- file1.txt
|   |   |   +--- file3.txt
|   |   |   +--- file4.txt
@enthuguy, Im a bit of confuse the "latested numbered" file now.
Can you explain more about it?
How 11110 folder return file2.txt
How 11114 folder return file5.txt
How 11130 folder return file1.txt and file3.txt and file4.txt
sorry if I had confused you.

latest numbered
11110
11114
11130

from above example. Since file1.txt is available in all three directories...we need to get the file1.txt from 11130

since file2.txt is available only under 11110 We should consider the file under 11110

basically if same file available in all 3 we should pick the latest numbered directory in this 11130

Thanks
ASKER CERTIFIED SOLUTION
Avatar of huacat
huacat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial