简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM