简体   繁体   English

在 linux 的目录中查找最旧的文件夹,即使里面的文件被修改

[英]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.

相关问题 linux将子文件夹中的文件移动到当前目录 - linux move files inside sub folder to current directory Linux昨天修改了所有文件夹的文件 - Linux all files of folder modified yesterday 在Linux中复制具有目录结构的修改文件 - Copy modified files with directory structure in linux 如何检测何时在Linux中的符号链接目录中修改了一个文件 - How to detect when one file is modified inside a symbolic link directory in Linux 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder 使用python在linux中的日期范围内修改目录中的文件 - getting files in a directory modified within a date range in linux using python Linux脚本获取上次修改文件夹的时间(以秒为单位) - Linux Script to get time in seconds when folder was last modified Linux 查找重复项将附加文件发送到新目录 - Linux finding duplicates send appended files to new directory 在Linux中相应目录内的文件中添加每个目录的名称 - Add name of each directory to files inside the corresponding directory in linux 在Linux中的父目录下显示文件和文件夹详细信息 - Display the Files and Folder details under a parent directory in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM