如何在 BASH 中按创建日期对目录进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15893258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I sort a directory by creation date in BASH?
提问by scktt
I'm trying to create a simple script to list the 16 most recent folders created in a directory on my nas machine as a way to display the most recent movies added to my collection.
我正在尝试创建一个简单的脚本来列出在我的 nas 机器上的目录中创建的 16 个最近的文件夹,作为显示添加到我的收藏中的最近电影的一种方式。
the script i am using at the moment is:
我目前使用的脚本是:
#!/bin/bash
rm -f /volume1/new-movies/*
IFS=$'\x0A'
fresh=$(ls -1ct /volume1/movies | head -16)
for folder in $fresh
do
file=$(find "/volume1/movies/$folder" -maxdepth 1 -type f)
movie=$(basename "$file")
ln -s "$file" "/volume1/new-movies/$movie"
done
ls -1 /volume1/new-movies
which is OK (the movies folder will only ever contain folders). My problem is this is sorted by the file/folders modification time rather than the creation time.
没关系(电影文件夹将只包含文件夹)。我的问题是这是按文件/文件夹修改时间而不是创建时间排序的。
the filesystem is ext4and should support a birth timebut i have had no luck accessing it.
文件系统是ext4并且应该支持 abirth time但我没有运气访问它。
scott@pandora scripts $ stat /volume1/movies/example/
File: '/volume1/movies/example/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 902h/2306d Inode: 373800961 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 1028/ scott) Gid: ( 100/ users)
Access: 2013-04-09 13:39:53.243991684 +1000
Modify: 2013-04-06 13:26:00.965998952 +1100
Change: 2013-04-09 11:46:23.280991727 +1000
Birth: -
however, samba seems to have no issue displaying the correct creation date/time. is there a way to access the same information from bash? or am i going to have to program something in python/other to do what i need by accessing smb directly and listing each folder with the creation date?
但是,samba 似乎没有问题显示正确的创建日期/时间。有没有办法从 bash 访问相同的信息?还是我将不得不在 python/other 中编程一些东西来做我需要的事情,方法是直接访问 smb 并列出每个文件夹和创建日期?
scott@pandora scripts $ smbclient \\localhost\movies\
Enter scott's password:
Domain=[EXAMPLENET] OS=[Unix] Server=[Samba 3.6.9]
smb: \> allinfo "example"
altname: E06KNE~A
create_time: Fri Jun 18 17:23:49 2010 EST
access_time: Tue Apr 9 13:39:53 2013 EST
write_time: Sat Apr 6 13:26:01 2013 EST
change_time: Sat Apr 6 13:26:01 2013 EST
attributes: DA (30)
smb: \> quit
edit: see my below answer for my final solution to this issue.
编辑:有关此问题的最终解决方案,请参阅我的以下答案。
采纳答案by scktt
After some fiddling I have come up with a solution which appears to work a little better than trying to rely on ls -lc. here's the full script i am now using.
经过一番摆弄之后,我想出了一个解决方案,该解决方案似乎比尝试依赖ls -lc. 这是我现在使用的完整脚本。
#!/usr/bin/env sh
# remove the existing symbolic links-
rm -f /volume1/new-movies/*
# retrieve the folder listing from the smbclient (the date used is 'modified'
# which appears closer to 'creation' than anything else)
folders=$(smbclient \\localhost\movies -N -c ls 2>/dev/null)
# format the output for better sorting. eg
# folder DA 0 Mon 2 Oct 12:23:00 2012
# 2012-Oct-2 12:23:00 "folder"
fmt1=$(echo "$folders" | sed -E 's/^ (.*\))\s+DA.* (\w+)\s+([0-9]{1,2})\s+([0-9:]{8})\s+([0-9]{4})$/-- ""/')
# change the month names to numeric representations and pad single digit
# dates to 2 characters. eg.
# 2012-Oct-2 12:23:00 "folder"
# 2012-10-02 12:23:00 "folder"
fmt2=$(echo -e "$fmt1" |sed 's/-Jan-/-01-/;s/-Feb-/-02-/;s/-Mar-/-03-/;s/-Apr-/-04-/;s/-May-/-05-/;s/-Jun-/-06-/;s/-Jul-/-07-/;s/-Aug-/-08-/;s/-Sep-/-09-/;s/-Oct-/-10-/;s/-Nov-/-11-/;s/-Dec-/-12-/;s/-\([0-9]\) /-0 /')
# sort the folders in reverse order
sortd=$(echo -e "$fmt2" | sort -r)
# grab the last 16. (16 items per page are displayed on the wd tv streaming)
latest=$(echo -e "$sortd" | head -16 | cut -d\" -f2)
# loop through each folder using new line rather than dft. space
IFS=$'\x0A'
for l in $latest
do
# find the movie file in the directory, dont look in subdirectories and
# only match movie files. skips subtitles, bonus features, etc.
f=$(find "/volume1/movies/$l" -maxdepth 1 -type f \
\( -iname \*.avi -o \
-iname \*.mp4 -o \
-iname \*.mkv -o \
-iname \*.mpg -o \
-iname \*.ts -o \
-iname \*.m4v \
\) -exec echo "{}" \;)
# grab just the filename
b=$(basename "$f")
# link the file $f to the new movie folder with just the base name.
ln -s "$f" "/volume1/new-movies/$b"
done
IFS=
ls -1 /volume1/new-movies
回答by Mzzl
Linux file systems never used to support file creation time, but apparently ext4 does. It's not integrated very conveniently in the standard tools like ls and stat, but you can do this as root:
Linux 文件系统从不支持文件创建时间,但显然 ext4 支持。它没有很方便地集成在 ls 和 stat 等标准工具中,但您可以以 root 身份执行此操作:
debugfs -R 'stat /full/path/to/my/file.txt' /dev/sda1
debugfs -R 'stat /full/path/to/my/file.txt' /dev/sda1
where sda1 is the device your filesystem is on
其中 sda1 是您的文件系统所在的设备

