Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default ls behaviour on Linux

Tags:

linux

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 ?

like image 281
andrefurquin Avatar asked Dec 18 '22 06:12

andrefurquin


2 Answers

Edit the file ~/.bashrc in your favorite text editor and add the following line at the bottom:

alias ls='ls -tU'

Save and close the file.

The next time you open a terminal, ls will give the list in sorted order of time.
But if you need to make it take effect immediately, you can:

source ~/.bashrc
like image 138
Sanjay Barnwal Avatar answered Dec 31 '22 03:12

Sanjay Barnwal


I will brutally chop your question in two:

  1. 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

  2. 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.

like image 21
Cedar Avatar answered Dec 31 '22 03:12

Cedar