Blog |Follow Nick on Twitter| About
 

One of the most useful features of OSX's finder is bing able to sort files by modified or added. In my typical workflow I use a $SHELL to move around directories and then use open . to open finder in the right location.

To speed things up a little more, I've knocked up a couple of alias's for my .bash_profile

lm

Works on both linux and OSX

    listmodifiedfiles() {

        lmdir="."
        lmdir=${1:-$lmdir}

        if [ -z "$2" ]
        then
            lmnum="-5"
        else
            lmnum=$(($2 * -1))
        fi

        ls -1t $lmdir | head $lmnum
    }
    alias lm=listmodifiedfiles

This adds an lm alias/command that will list the last 5 modified files. The command takes two options the first being a directory, the second being the number of files to list.

E.g.

    linickx:dotcom nick$ lm ./content/posts/ 3
    bash-ls-modified-or-added-files.md
    css-styling-nginx-directory-listings.md
    good-bye-wordpress-hello-pelican.md
    linickx:dotcom nick$

la

Works on OSX only

    listaddedfiles() {

        ladir="."
        ladir=${1:-$ladir}

        if [ -z "$2" ]
        then
            lanum="-5"
        else
            lanum=$(($2 * -1))
        fi

        ls -lrt -d -1 $ladir/* | grep -v '^\.$\|^\.\.$' | xargs -I {} mdls -name kMDItemFSName -name kMDItemDateAdded {} | sed 'N;s/\n//' | grep -v '(null)' | awk '{gsub(/"/, "", $7);print $3 " " $4 " " $7}' | sort -r | awk '{print $3}' | head $lanum
    }
    alias la=listaddedfiles

This add an la alias/command that will list the last 5 added files. As before, the command takes two options the first being a directory, the second being the number of files to list. e.g.

    linickx:~ nick$ la ~/Downloads/
    GZC4573714.zip
    AdobeFlashPlayerInstaller_17au_ltrosxd_aaa_aih.dmg
    Defaultselfsignedservercerti.pem
    linickx:~ nick$

NOTE This command takes a long-ish time to run on big directories as each file has to have it's meta data checked prior to output. If anyone a suggestion to make this quicker I'm all ears !

References:

  • http://stackoverflow.com/questions/15691359/how-can-i-list-ls-the-5-last-modified-files-in-a-directory

  • http://apple.stackexchange.com/questions/86307/can-i-list-files-ordered-by-date-added-to-a-folder-from-a-command-line-tool-like

rgds,

Nick

 
Nick Bettison ©