Solved

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

Posted on 2016-10-04
6
70 Views
Last Modified: 2016-10-08
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/
0
Comment
Question by:enthuguy
  • 3
  • 3
6 Comments
 

Author Comment

by:enthuguy
Comment Utility
0
 
LVL 6

Expert Comment

by:huacat
Comment Utility
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

0
 

Author Comment

by:enthuguy
Comment Utility
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
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Expert Comment

by:huacat
Comment Utility
@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
0
 

Author Comment

by:enthuguy
Comment Utility
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
0
 
LVL 6

Accepted Solution

by:
huacat earned 500 total points
Comment Utility
Please run below code at test folder, you save it as a shell script to accept a argument.
folders=`find . -type d -name "changed" | sort`
names=`find ./ -name "*.txt" -follow -exec basename "{}" \; | sort | uniq`
for f in $names; do
   for d in $folders; do
     if [ -e $d/$f ]; then
       last=$d/$f
     fi
   done
   echo $last
done;

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now