简体   繁体   中英

Can we store the creation date of a folder (not file) using bash script?

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.

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

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.

Direct approach - getting the path

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:

  • If you need only directories like the title of your post suggests, you can pass -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.

Another approach - getting just the date

If you only need creation date of a subdirectory within a directory (eg 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.

Alternative approaches

Since getting the last created folder is clearly expensive, you could also either:

  1. Save the directory name somewhere when creating said directory, or
  2. Use naming convention such as ddddyymm-name-of-directory which doesn't require any extra syscalls - just find -type d|... .

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