[英]Finding the oldest folder in a directory in linux even when files inside are modified
I have two folders A and B, inside that there are two files each.我有两个文件夹 A 和 B,里面各有两个文件。 which are created in the below order
按以下顺序创建
mkdir A
cd A
touch a_1
touch a_2
cd ..
mkdir B
cd B
touch b_1
touch b_2
cd ..
From the above i need to find which folder was created first(not modified).从上面我需要找到首先创建的文件夹(未修改)。
ls -c <path_to_root_before_A_and_B> | tail -1
Now this outputs as "A" (no issues here).现在这个输出为“A”(这里没有问题)。 Now i delete the file a_1 inside the Directory A. Now i again execute the command
现在我删除目录 A 中的文件 a_1。现在我再次执行命令
ls -c <path_to_root_before_A_and_B> | tail -1
This time it shows "B".这次它显示“B”。
But the directory A contains the file a_2, but the ls
command shows as "B".但是目录 A 包含文件 a_2,但
ls
命令显示为“B”。 how to overcome this如何克服这个
How To Get File Creation Date Time In Bash-Debian 如何在 Bash-Debian 中获取文件创建日期时间
You'll want to read the link above for that, files and directories would save the same modification time types, which means directories do not save their creation date.您需要阅读上面的链接,文件和目录将保存相同的修改时间类型,这意味着目录不会保存它们的创建日期。 Methods like the
ls -i
one mentioned earlier may work sometimes, but when I ran it just now it got really old files mixed up with really new files, so I don't think it works exactly how you think it might.像前面提到的
ls -i
这样的方法有时可能会起作用,但是当我刚刚运行它时,它会将真正的旧文件与真正的新文件混合在一起,所以我认为它的工作方式与您想象的完全不一样。
Instead try touching a file immediately after creating a directory, save it as something like .DIRBIRTH
and make it hidden.而是在创建目录后立即尝试触摸文件,将其保存为
.DIRBIRTH
之类的文件并将其隐藏。 Then when trying to find the order the directories were made, just grep for which .DIRBIRTH
has the oldest modification date.然后,当尝试查找目录的创建顺序时,只有 grep
.DIRBIRTH
具有最早的修改日期。
Assuming that all the stars align (You're using a version of GNU stat(1)
that supports the file birth time formats, you're using a filesystem that records them, and a linux kernel version new enough to support the statx(2)
syscall, this script should print out all immediate subdirectories of the directory passed as its argument sorted by creation time:假设所有星星都对齐(您使用的是支持文件诞生时间格式的GNU
stat(1)
版本,您使用的是记录它们的文件系统,以及 linux kernel 版本足够新以支持statx(2)
系统调用,此脚本应打印出作为其参数传递的目录的所有直接子目录,并按创建时间排序:
#!/bin/sh
rootdir=$1
find "$rootdir" -maxdepth 1 -type d -exec stat -c "%W %n" {} + | tail -n +2 \
| sort -k1,1n | cut --complement -d' ' -f1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.