linux - How can I sort a directory by creation date in BASH? -
i'm trying create simple script list 16 recent folders created in directory on nas machine way display recent movies added collection.
the script using @ moment is:
#!/bin/bash rm -f /volume1/new-movies/* ifs=$'\x0a' fresh=$(ls -1ct /volume1/movies | head -16) folder in $fresh 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 ok (the movies folder ever contain folders). problem sorted file/folders modification time rather creation time.
the filesystem ext4
, should support birth time
have had no luck accessing it.
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 have no issue displaying correct creation date/time. there way access same information bash? or going have program in python/other need accessing smb directly , listing each folder creation date?
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 below answer final solution issue.
after fiddling have come solution appears work little better trying rely on ls -lc
. here's full script using.
#!/usr/bin/env sh # remove existing symbolic links- rm -f /volume1/new-movies/* # retrieve folder listing smbclient (the date used 'modified' # appears closer 'creation' else) folders=$(smbclient \\\\localhost\\movies -n -c ls 2>/dev/null) # format output 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 month names numeric representations , pad single digit # dates 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 folders in reverse order sortd=$(echo -e "$fmt2" | sort -r) # grab last 16. (16 items per page displayed on wd tv streaming) latest=$(echo -e "$sortd" | head -16 | cut -d\" -f2) # loop through each folder using new line rather dft. space ifs=$'\x0a' l in $latest # find movie file in directory, dont in subdirectories , # 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 filename b=$(basename "$f") # link file $f new movie folder base name. ln -s "$f" "/volume1/new-movies/$b" done ifs= ls -1 /volume1/new-movies
Comments
Post a Comment