justaguy
justaguy

Reputation: 3022

bash to select oldest folder in directory and write to log

In the bash below the oldest folder in a directory is selected. If there are 3 folders in the directory /home/cmccabe/Desktop/NGS/test and nothing is done to them (ie. no files deleted, renamed) then the bash correctly identifies f1 as the oldest. However, if something is done to the folder then the bash identifies f2 as the oldest. I am not sure why or how to prevent that from happening. Thank you :).

folders in directory

f1
f2
f3

Bash

# oldest folder used analysis and version log created
dir=/home/cmccabe/Desktop/NGS/test
{
read -r -d $'\t' time && read -r -d '' filename
} < <(find "$dir" -maxdepth 1 -mindepth 1 -printf '%T+\t%P\0' | sort -z )
printf "The oldest folder is $filename, created on $time and analysis done using v1.3 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/Desktop/NGS/test/log
echo "$filename"

Upvotes: 0

Views: 607

Answers (2)

Inian
Inian

Reputation: 85620

Your idea of using find is right, but with a little tweaking like this

$ IFS= read -r -d $'\0' line < <(find . -maxdepth 1 -type d -printf '%T@ %p\0' \ 
    2>/dev/null | sort -z -n)

$ printf "The oldest directory: %s\n" "${line#* }"

Similar to the one answered here.

Upvotes: 2

Jenever
Jenever

Reputation: 510

When you edit a folder or a file in a folder, the modification date of the folder is updated. The creation date of a folder is not saved. See this question for more information How to get file creation date/time in Bash/Debian?

Upvotes: 2

Related Questions