blog.johanka.net

Introduction to the command line (cat, alias, grep)

If you've had the contents of your user home folder listed, you may have noticed that it contains several hidden files and folders. These are mostly application data that get created when you first use each particular program, and not much will happen if you delete them - they are simply created again, only any changes you may have made get lost.

While still in the user home folder, let us now have a look at how to display the content of a text file in the command line prompt (Terminal). We will use the command cat. Like this:

cat .bashrc

What you see now is the file which let's the more experienced user alter the command line settings.

This way you can have a look at all the system files without having to open them in an external application. That is possible, too, just type in

xed thefileofyourchoice.txt

and the file will pop up in the window of a text editor. (Replace Xed with any other editor if not on Mint, Cinnamon.)

It is perfectly possible to use the command line tool to type into the file directly, of course. I don't find it very convenient, though, so I'm not going to go there this time.

Now, when you close the external editor and get back to the command line prompt, type in

grep alias .bashrc

(while still in the user home folder). We've just instructed the system to only display those lines in the file that contain the word "alias". Analogically, this works with any other file or group of files. If, for instance, you typed in

grep alias .bash*

(where * represents any character) it would attempt to search for said expression in all the hidden files in this folder whose name begins with ".bash".

Let us now have a look at how aliases actually work. When you use certain command quite often (simply because it is more convenient than performing the same operation using the graphical interface) and want to save yourself extra time (or to avoid unnecessary error) you can create a shortcut. Like in the examples given in the .bashrc we have had displayed earlier.

ll

is a shortcut, so whenever you'll want a complete directory listing including the hidden files you can just type in ll to get the same result. Again, you can add your own aliases that follow a similar pattern to the file or create your own .bash_aliases file and edit it in the basic text editor of your choice. (Do not use LibreOffice for this.) If you want to add the aliases from the command line directly, you can do it like this

echo "alias rm='rm -i'" >> .bashrc

(If you use only one > the newly added line overwrites existing content.)

and then do the

cat .bashrc

to see the new addition at the end of the file. System will now ask if you really want to remove the files or folders you've specified.

It is, indeed, possible to save the output of any command as separate file or add it to an existing file. If you type in

ls -la > Desktop/file.txt

and the directory listing will get copied to the file you specified. You don't even have to create this file first. It will get created automatically when you use this command.

I will continue introducing basic commands in the following article. If you bare with me I will eventually show you how to create your own basic scripts in bash to automate repeated tasks on your computer. Now, how exciting is that? :-)

copy left Johanka Piskovská, 2019