2017年6月7日 星期三

[Linux 常見問題] How to recursively find and list the latest modified files in a directory

Source From Here
Question
Sometimes, I reboot the machine and I want to list the latest file(s) being modified before reboot under specific directory. How to achieve this?

How-To
To achieve this purpose, you need to combine commands find/sort/cut/head. e.g.:
# find ./ -type f // Locate all files recursively under current directory
./a/2017-06-08_13-39-41
./a/2017-06-08_13-40-23
./b/2017-06-08_13-39-56
./b/2017-06-08_13-40-26
./c/2017-06-08_13-40-05
./c/2017-06-08_13-40-20



// Format of stat command:
// %Y: time of last modification, seconds since Epoch
// %y: time of last modification, human-readable
// %n: file name

# find ./ -type f -print0 | xargs -0 stat --format '%Y :%y %n' // Bring in modification time information
1496900381 :2017-06-08 13:39:41.837791854 +0800 ./a/2017-06-08_13-39-41
1496900423 :2017-06-08 13:40:23.508391763 +0800 ./a/2017-06-08_13-40-23
1496900396 :2017-06-08 13:39:56.461353308 +0800 ./b/2017-06-08_13-39-56
1496900426 :2017-06-08 13:40:26.210495508 +0800 ./b/2017-06-08_13-40-26
1496900405 :2017-06-08 13:40:05.874714732 +0800 ./c/2017-06-08_13-40-05
1496900420 :2017-06-08 13:40:20.233266018 +0800 ./c/2017-06-08_13-40-20


// Argument of command sort:
// -n, --numeric-sort: compare according to string numerical value
// -r, --reverse: reverse the result of comparisons

# find ./ -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr // Sorting the result
1496900426 :2017-06-08 13:40:26.210495508 +0800 ./b/2017-06-08_13-40-26
1496900423 :2017-06-08 13:40:23.508391763 +0800 ./a/2017-06-08_13-40-23
1496900420 :2017-06-08 13:40:20.233266018 +0800 ./c/2017-06-08_13-40-20
1496900405 :2017-06-08 13:40:05.874714732 +0800 ./c/2017-06-08_13-40-05
1496900396 :2017-06-08 13:39:56.461353308 +0800 ./b/2017-06-08_13-39-56
1496900381 :2017-06-08 13:39:41.837791854 +0800 ./a/2017-06-08_13-39-41


# find ./ -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- // Use command cut to remove the first segment
2017-06-08 13:40:26.210495508 +0800 ./b/2017-06-08_13-40-26
2017-06-08 13:40:23.508391763 +0800 ./a/2017-06-08_13-40-23
2017-06-08 13:40:20.233266018 +0800 ./c/2017-06-08_13-40-20
2017-06-08 13:40:05.874714732 +0800 ./c/2017-06-08_13-40-05
2017-06-08 13:39:56.461353308 +0800 ./b/2017-06-08_13-39-56
2017-06-08 13:39:41.837791854 +0800 ./a/2017-06-08_13-39-41


# find ./ -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head -n 1 // Finally, use command head to show first line
2017-06-08 13:40:26.210495508 +0800 ./b/2017-06-08_13-40-26 


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...