2013-04-09 80 views
2

我試圖創建一個簡單的腳本來列出在我的nas機器上的一個目錄中創建的16個最新文件夾,作爲顯示添加到我的集合中的最新電影的一種方式。如何在BASH中按創建日期排序目錄?

我用此刻的腳本是:

#!/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 

這是OK(電影文件夾將只包含文件夾)。我的問題是這是按文件/文件夾修改時間而不是創建時間排序的。

文件系統是ext4,應該支持birth time但我有沒有運氣訪問它。

[email protected] 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: - 

但是,桑巴似乎沒有顯示正確的創建日期/時間的問題。有沒有辦法從bash訪問相同的信息?或者我將不得不編程的東西在Python /其他做我需要通過直接訪問SMB並列出每個文件夾與創建日期?

[email protected] 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 

編輯:看我下面的答案我最終解決這個問題。

+0

你可以在這裏看看[這個答案](http://unix.stackexchange.com/a/67895)。 – 2013-04-09 06:46:11

回答

1

一些擺弄我想出這似乎不是試圖依靠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})$/\5-\2-\3 \4 "\1"/') 

# 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\1 /') 

# 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 
0

Linux文件系統從來不用於支持文件創建時間,但顯然是ext4。它不是像LS和統計標準的集成工具很方便,但你可以這樣做,因爲根:

debugfs -R「STAT /full/path/to/my/file.txt」的/ dev/sda1的

其中sda1的是設備文件系統是

+0

不幸的是,執行該命令超過800+項目將永遠。該腳本也作爲非特權用戶運行,因此使用debugfs將無法工作。 – scktt 2013-04-15 03:54:12