0

I found this in another post but would like to be able to call a script of function to reduce the amount of typing.

ls -a | \
grep -v '^\.$\|^\.\.$' | \
xargs -I {} mdls -name kMDItemFSName -name kMDItemDateAdded {} | \
sed 'N;s/\n//' | grep -v '(null)' | \
awk '{print $3 " " $4 " " substr($0,index($0,$7))}' | \
sort -r

This is the other post Can I list files ordered by date added to a folder from a command-line tool like ls?

1

Simply create .bash_profile in your home folder or open it, if it already exists:

nano .bash_profile

and add the following lines:

function added {
    ls -a | grep -v '^\.$\|^\.\.$' | xargs -I {} mdls -name kMDItemFSName -name kMDItemDateAdded {} | sed 'N;s/\n//' | grep -v '(null)' | awk '{print $3 " " $4 " " substr($0,index($0,$7))}' | sort -r;
}

Save the mods with ctrl-o, Enter and quit nano with ctrl-x.

Close the Terminal window, open a new one and then simply enter added and you'll get the sorted list of files and folders in your home folder with some glitches though.

The accepted answer in the linked question presents a much faster solution:

function added2 {
    mdls -name kMDItemFSName -name kMDItemDateAdded -raw * | xargs -0 -I {} echo {} | sed 'N;s/\n/ /' | sort;
} 
| improve this answer | |
  • to avoid changing directories, ~/ can be used right? – ankii Oct 12 '19 at 12:00
  • @ankii The command (or function) is executed in the working directory.... – klanomath Oct 12 '19 at 12:07
0

Here's another shell function ( I'm assuming your shell is ksh, zsh, or bash ) - Sets the date, time and UTC offset to zeros if the Date Added metadata is missing

dateadded () {
mdls -n kMDItemFSName -n kMDItemDateAdded -raw -nullMarker '0 0 0' ./* |
tr '\0' '\n ' | paste -d ' ' - - | sort -rn
}
| improve this answer | |
  • @ankii, That was a totally unnecessary edit! – user3439894 Oct 12 '19 at 12:07
  • I like appropriate colors @user3439894, you can see rendered output. And it was already on home page – ankii Oct 12 '19 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .