Reputation: 23
Actually I’m a newbie at Bash and I’m learning with some hands on.. I used the following stat command:
find "$DIRECTORY"/ -exec stat \{} --printf="%w\n" \; | sort -n -r | head -n 1 > timestamp.txt
where DIRECTORY is any path say, c:/some/path . It contains a lot of folders. I need to extract the creation date of the latest created folder and store it in a variable for further use. Here I started by storing it in a txt file. But the script never completes. It stays stuck at the point it reaches this command line. Please help. I'm using cygwin. I had used --printf="%y\n" to extract last Modified date of the latest folder and it had worked fine.
Upvotes: 0
Views: 584
Reputation: 85835
You could do with a -type d
option to include only the directories from the current folder, and as discussed in the comments section if you need the output from the stat
in just yyyy-mm-dd
format, use awk
as below.
find "$DIRECTORY"/ -type d -exec stat \{} --printf="%w\n" \; | sort -n -r | head -n 1 | awk '{print $1}'
To store the value in a bash
variable:-
$ myvar=$(find "$DIRECTORY"/ -type d -exec stat \{} --printf="%w\n" \; | sort -n -r | head -n 1 | awk '{print $1}')
$ echo $myvar
2016-05-20
Upvotes: 0
Reputation: 14841
The command is okay (save for escaped \{}
which I believe is a mistake in the post). It only seems so that it never finishes, but given enough time, it'll finish.
The main bottleneck lies in executing stat
for each file. Spawning process under Cygwin is extremely slow, and executing one for each of possibly thousands of files is totally infeasible. The only way to circumvent this is not spawning processes like this.
That said, I see few areas for improvement:
-type d
to your find
command to filter out any files.If you need only modification time (see what means directory modification time on Linux here, I guess this may be similar in Cygwin), you can use find
's built in facilities rather than stat
's like this:
find "$DIRECTORY"/ -type d -printf '%TY-%Tm-%Td %TH:%TM:%TS %Tz %p\n' \
| sort -nr \
| head -n1 \
| cut -f4 -d' '
Example line before we cut the path with cut
- most of stuff in -printf
is used to format the date:
2014-09-25 09:41:50.3907590000 +0200 ./software/sqldeveloper/dataminer/demos/obe
After cut
:
./software/sqldeveloper/dataminer/demos/obe
It took 0.7s to scan 560 directories and 2300 files.
The original command from your post took 28s without -type d
trick, and 6s with -type d
trick when ran on the same directory.
Last but not least, if $DIRECTORY
is empty, your command will prune whole directory tree, which will take massive amount of time.
If you only need creation date of a subdirectory within a directory (e.g. not the path to the directory), you can probably just use stat
:
stat --printf '%Y' "$DIRECTORY"/
I'm not sure whether this includes file creations as well, though.
Since getting the last created folder is clearly expensive, you could also either:
ddddyymm-name-of-directory
which doesn't require any extra syscalls - just find -type d|...
.Upvotes: 1