Sunday, February 06, 2022

Adding or Changing Default Aliases

 I wanted to add a shorter ls command to use in the Terminal that showed directories and their contents in the format I wanted to see them in.  I decided that I'd also shorten the command by 50% (well one letter) by using just 'l' instead of 'ls'.  I read the 'man' page for ls and a few trusted websites and finally settled on the following parameters;  

ls -h -F -l -o -g --group-directories-first --color=auto

These parameters when added to 'ls' basically lists the directories first then files in alphabetical order A-Z, show how many files are in the directory, the permissions of the directory/file, the directory/file size, it's creation date/time, and it's name all in pretty colors. 

Once I figured out what parameters I wanted I created a '.sh' shell script file called 'l.sh' and put it in my home directory's 'bin' folder which is also in my PATH.  It worked well but I had to type 'bash l.sh' to get it to work.  While testing I used just the 'l' command a few times and it worked but it did not run my l.sh script file it used something else and it was different from the standard 'ls' command too.  

After some reading I figured out that there was a special file called '.bashrc' and it had sections in it for ALIASES.  The .bashrc file was located in my home folder and can be opened with your favorite text editor.  

I found this section in my .bashrc file which was using the 'l' command I wanted but it had it's own parameters (not the one's I wanted)  

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF' 

So I edited the .bashrc file and replaced the alias l='ls -CF' to my parameters;
alias l='ls -h -F -l -o -g --group-directories-first --color=auto'

I saved the .bashrc file and rebooted the computer (The .bashrc file loads at start up which is why I rebooted, you may not have too but better safe then sorry).  So when I was done this section of my .bashrc file looked like this;

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -h -F -l -o -g --group-directories-first --color=auto'

And just like that I can now just type 'l' (without the ' ' ) and it runs my version of the ls command.  You can also add other alias in this section (my audio breaks sometimes so I have a fixaudio.sh script file that I alias here too, so now I just type 'fixaudio' and my audio fix program executes).  If you are going to do a lot of aliases you can also link to another file you create and add it's file name to a section in .bashrc near this section.


NOTE:  Backup your original .bashrc file before you edit it.  There is other stuff in it, not just the alias section.