Reputation: 522
By default when I use the command ls on my folder, I have the result sorted by name. What if I want to change that behavior system wide for created_at ?
Upvotes: 5
Views: 7283
Reputation: 211
Assuming you're using GNU ls
, you'll want to use
ls -t --time=creation
The --time=creation
option is documented as
When sorting by time …, sort according to the birth time.
Because most filesystems don't store creation time of files, this option uses
the file creation timestamp if available, falling back to the file modification timestamp (mtime) if not.
To pass these options to ls
without writing them yourself, you should edit the file ~/.bashrc
in your favorite text editor and add the following line at the bottom:
alias ls='ls -t --time=creation'
Save and close the file.
The next time you open a terminal, ls
will give the list in sorted order of time.
If you need to make it take effect immediately, you can:
source ~/.bashrc
Upvotes: 12
Reputation: 887
I will brutally chop your question in two:
How to sort by creation time:
There may not be an answer to that. This post https://unix.stackexchange.com/questions/20460/how-do-i-do-a-ls-and-then-sort-the-results-by-date-created seems to suggest that the creation date is NOT stored in many Unix systems, but I'm not sure about linux. Also, ls in MacOS IS able to sort by creation date. If you have a mac, do ls -tU
How to change a command's default behavior:
Suppose that you have found a way to make ls sort by creation date (for example, on MacOS, I do so by typing in ls -tU
), then you could use the alias command. Simply do alias ls='ls -tU'
and in the future all ls commands would be replaced by ls -tu
. This solution persists until your close your terminal window, but you can always add it to a startup script (for example, .bashrc
) to apply it every time you pull up your command shell.
Upvotes: 2