In the home folder of most Linux systems that use bash as the command shell there should be a hidden file called .bashrc. This file will be called each time I start a new terminal window, so it is a good place to do things like set what the format of the bash command prompt should be. However there are many other things that I can do with the script, and one such thing that is pretty helpful is setting up some bash aliases for commands.
Often there might be a command that I use with a bunch of options that take a long time to type over again. There are also a number of situations in which I can produce a result that I want, but not with a single command, rather a long string of commands involving piping of standard output to the standard input of another. So with many of these it makes sense to create a line in a file where I assign a command with a long string of options, and or a bunch of commands piped together that will result in just one command with a short name. I can then just call that single short command name each time, rather than typing the same long string of text each time.
I can also make it so an alias is a call to a bash script, and from there I can use positional arguments, look at environment variables, and so forth in order to make the thing look and work like a real Linux command.
First off in the home folder of the current user there should be a hidden .bashrc file, if not one should be written however at least some care should be taken when doing so. Having such a file will override any system wide files for what is going on so they should be looked at as a way to know how to get started with such a file at a user level.
In any case if the file is there it would be a good idea to start out by taking a look at it. So there is starting out by using the Linux cd command to change the current working directory to the home folder, and using Linux cat to print the contents of the file to the standard output of a terminal.
I could write aliases in the .bashrc file itself, however I think that it is a good idea to keep them in one or more independent files. So in the .bashrc file there should be something like this:
If this bash code is there then I just need to create a .bash_aliases file, and in this file I will be setting up some aliases for commands that I find myself typing all the time. If the code is not there, or something like it at least, then place an if statement that will look for and call a .bash_aliases file.
So in the bash aliases file lets start out with at least a few basic commands that are just aliases for other Linux commands such as ls.
So now I can just type la for a quicker way to type ls -A.
Maybe this is not the most compelling example of an alias, but the basic idea is there. If I am doing something that involves a lone string of commands using piping and redirection, and it is something that I find myself doing often, then maybe it is a good idea to turn that into an alias. So lets look at some more examples of Linux aliases to get a better idea of why these can often help to save a lot of time typing.
I use the source control command git all the time, not just for projects, but also for maintaining the mark down files of these blog posts. So of course I take a moment to set up at least a few for common git tasks such as pushing and pulling.
I just need to make sure I am not taking any kind of command that is not taken all ready. For example when I am making an alias for git status, I can not use stat as that is a command for displaying file, and file system info at least for me on the system I am using. However it would seem that status is free, so it makes sense to take a look in the bin, sbin and usr/sbin, ect folders to make sure I am not taking a command that is in use.
Another thing that I can do when it comes to setting up some aliases is to have a bash folder, and write a few scripts that I can then also turn into my own commands. When it comes to anything that I find myself doing over and over again in the command line as a long series of commands, chances are that is a good example of something that I can turn into a script. I can then place that script in a main folder in my home path, and set up some aliases so that I can call them from any location in a terminal window.
For example say I have a whole bunch of git folders in a certain path and I find myself going threw each of them to do a git pull to make sure they are all up to date. Say I would also like to do the same for push, status, and maybe a few other git sub commands. I could just repeat that over and over again each time I starting working on things, or I could write one or more scripts for a bash folder in my home path. I could also have a bash_aliases file for the collection of scripts and make that something that I call from my main bash_aliases file in the home folder.
The first script that I am going to want it one that will get the contents of a folder of git folders, and then for each folder switch to that folder as the current working path, and call a given git command for it.
I can then save this file as something like git-all.sh in a bash folder in my home folder, and make it executable with chmod.
I can then call the script like this:
Hey all right seems to work great.
So then it is just a question of making additional scripts or aliases for this so I can just type a few keys into bash, and preform a pull request for all git folders in my github folder. Or any git command for that matter.
So now I thought I might make a git-folder.sh that will just return the folder where I have my git folders. The idea here is that I have one script that will return this, so if I change the location I can update that in just one place.
I then thought I would use a git-all-base.sh file that will use the git-all.sh script, and use my git-folder.sh script each time for the first argument. I can then just call this script, and pass just one argument that is the git sub command that I want to use on all git fodlers.
I can now use my git-all-bash.sh script for each sub command if I want.
This is just a simple example, but if need be I could add additional options for some of these.
So now it is just a question of making some bash aliases for these. I could just add them in at the main hidden bash_aliases file in the home path. However I think I might end up making scripts like this for a few commands and tools that I make. I could have a bash_aliases fiel for each folder in a bash folder in my home folder and have it look like this:
I can then just add, or comment out lines like this in my main bash_aliases file.
So these seem to work great, and now something that used to take up a but of time is now nothing at all. pretty cool, I think I might want to make scripts like this for a a wide range of stuff that I use.
So I am sure that I will be finding more to write about with Linux aliases as time goes by. However this really is something that should be figured out on a per user basis. Whatever a single users work flow is that is what should be set up when it comes to working out what aliases should be. One user might use a different set of command line tools, or they might be using a slightly different distribution where the options in terms of commands will be a little different. However the basic idea of aliases should be more or less the same in most Linux systems where bash is used as the command line interface shell.
I like to use Raspberry PI single noard computers, and for the most part thus far I like to stick to using Raspberry PI OS in terms of the OS to run on a Raspberry PI. When it comes to using a raspberry PI the OS image is housed on a MicroSd card, and with that said I have more than one card, and I re-image them every now and then. With that said sense I first wrote this post I have now started a bash script example that is a bunch of additional scripts that have to do with a custom setup of a raspberry ps os image. One of the scripts has to do with copying over a custom bashrc file, and I also intend to set up custom aliese with this setup script also.