Midhun
Midhun

Reputation: 375

How to get the timestamp for the sub directories recursively

I want to get the time stamp of sub directories recursively and write to a new file. Below is the sample script that i wrote. But it is not happening recursively.

#!/bin/bash
CURRENT_DATE=`date +'%d%m%Y'`
Year=`date +%Y`
Temp_Path=/appinfprd/bi/infogix/IA83/InfogixClient/Scripts/IRP/
File_Path=/bishare/DLSFTP/
cd $File_Path
echo $Year
find /bishare/DLSFTP/$Year*  -type d | ls -lrt > $Temp_Path/New_Bishare_File_Data_$CURRENT_DATE.txt

Upvotes: 1

Views: 1210

Answers (1)

John1024
John1024

Reputation: 113924

The problem is that ls ignores stdin. Instead, try:

find /bishare/DLSFTP/$Year* -type d -exec ls -dlrt {} +

Discussion

As an example, let's run ls -d with no arguments and no stdin:

$ ls -d
.

Now, let's supply the name of a directory, A, on stdin:

$ echo A | ls -d
.

As you can see, ls ignored stdin. Now, let's supply the name A on the command line:

$ ls -d A
A

This succeeds. ls returns the name of the directory A.

In the find command, -exec ls -lrt {} + tells find to put the names of directories it finds on the command line following ls -lrt and then to execute ls. This is what you need.

Note also that ls -lrt will report on the files in the directories. To only show information on the directories, use -d: ls -dlrt.

Alternative 1

If the find on your system supports -printf, then we can eliminate the use of ls and get a custom format for the output (hat tip: tripleee). For example:

find .  -type d -printf '%t %p\n'

%t prints the time stamp and %p prints the name of the directory. This is supported by GNU find.

Alternative 2

Although it lacks the formatting flexibility of ls or find -printf, there is another option: find has a -ls action:

find .  -type d -ls

This too may require GNU find.

Upvotes: 2

Related Questions