简体   繁体   English

bash选择目录中最旧的文件夹并写入日志

[英]bash to select oldest folder in directory and write to log

In the bash below the oldest folder in a directory is selected. 在目录中最旧文件夹下面的bash中,选择了该文件夹。 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. 如果目录/home/cmccabe/Desktop/NGS/test3文件夹,并且没有对它们进行任何处理(即未删除,重命名任何文件),则bash正确地将f1标识为最早的文件夹。 However, if something is done to the folder then the bash identifies f2 as the oldest. 但是,如果对文件夹执行了某些操作,则bash会将f2标识为最旧的。 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"

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? 有关更多信息,请参见此问题。 如何在Bash / Debian中获取文件创建日期/时间?

Your idea of using find is right, but with a little tweaking like this 您使用find想法是正确的,但是需要进行一些调整

$ 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 . 类似于这里回答的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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