As you keep working on linux you get better and better on the command line. it comes a point where you say to yourself: Damn I have learned some many commands, I went from being a total newbie to an intermediate linux user. At least you know what are you doing most of the time with your system.
You know how to list files in a directory by using the ls command, you know how to change the working directory by using the cd command, you can download a file with the wget utility and maybe you understand how to properly use the grep command to find different files on your linux computer.
To be honest this is only the beginning of working with the linux console. Do you know how to create a new directory, do you know how to remove it? Can you empty your trash from the commandline?
It is time to show you guys some other useful linux commands that have helped me working on my ubuntu system. Lets start with the mkdir command.
The mkdir command
The mkdir command is used to create a new directory. It’s syntax is very simple. If you would like to create a new directory on your linux machine you can do it right now by using the following command.
In the above command mkdir is the command, and linuxcommands is the name of the directory you want to create. You can also use the -v while working with the mkdir command to print a useful message for each directory you create. A simple example of such usage is show below.
mkdir -v newdirectory
Running the above command on my system gives me the following output on my Ubuntu console.
mkdir: created directory `newdirectory'
In short words the above output is very self explanatory. It tells the user that a new directory has been created with the use of the mkdir command.
The rm command
The rm command is very useful when it comes to removing files or an entire directory including everything in it such as files and subdirectories.
To remove a directory and their contents use the following command.
rm -r newdirectory
The -r option tells the rm command to remove the content of a directory recursively. In order to get some information what is being done while using the rm command put the -v option in use. This helpful option usually stands for verbose.
rm -rv newdirectory
I get the following output.
removed directory: `newdirectory'
Another option which is being used with the rm command is the -f option. It forces the remove process and never prompts the user to ask if they are sure what they are doing. I do not recommend this option when working with very important files.
What I highly recommend to do when using the rm command to delete files is to make use of the -i option. This option will help you to securely delete files and never lose a single one that you don’t want to delete. What does securely mean in this case?
The -i option prompts the use about the operating that is going to take place. In short words they are being asked if they want to really want to remove the file or not.
The following is an example which demonstrates the usage of the rm command with the -i option.
rm -i myfile.txt
When running the above command I get the following prompt.
rm: remove regular file `myfile.txt'?
Am I sure about this? If yes then I type y and hit return. The best thing you can do in order to get rm to prompt you each time you want to remove a file is to create an alias.
Creating an alias of a command
Sometimes you need a customized command for your personal usage in your linux system. Consider the rm command case when you want it to prompt you each time you want to remove a file on your system. I am sure you know now that making use of the -i option is going to do you for that, but what if you forget to use the -i option and somehow you delete a very important file?
The solution to this very simple problem is to create an alias. What is an alias? According to wikipedia is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command.
So long story short we can create an alias for the rm -i command. You can name it whatever name fits best for you. A good one would be to myrm. Do not make the mistake of making the following alias.
Why? It looks perfect to me, why not? Because you will get used to it and when you work on a new system that does not have the rm=rm-i alias you will take it for granted that your files are being delete in a secure way.
Now that you know about aliases and how risky the rm command can be I am sure you want to know how to create an alias for this command on your system.
I am supposing that you make use of the bash shell. To create an alias on your system open the .bashrc file with a text editor you like. I use vim for my text editing.
Put your alias inside this file.
alias myrm='rm -i'
Once you are sure you have created the right alias then save and close the file. To make use of your alias just load them with the following command.
Now each time you start a new shell your alias is going to get activated. You can use the new alias you created just you use any other command on your system.
The myrm command is going to delete the files for you and before doing that it is going to prompt to telling you if you are sure what you are doing or not. Very useful, isn’t it?
The echo command
The echo command is used to display something on your screen. It is useful when you want to pass some text to a file. This can be done by redirecting.
The following example is useful if you want to learn how this redirecting this works.
echo 'Test' > file.txt
To see if it worked, I mean if the text Test was saved inside the text file file.txt then just use the cat command to display its output on the console.
The echo command is very useful when writing shell scripts in bash. It can be used to echo output on the console for a program you want to build.
In bash we can create variables using the syntax like shown below.
We can easily display the value of the variable name by running the following command.
The ps command
The ps command is used to display various information about processes running on your system. Do you want to see every process running on your system?
Cool, one single command can do that for you. The following command allows the user to see every process on the system using BSD syntax.
The following is a list of processes running on my machine.
19625 pts/5 Ss 0:00 bash 19732 ? S 0:00 [kworker/0:2] 19733 ? Sl 0:01 /opt/google/chrome/chrome --type=renderer --enable-de 19771 ? S 0:00 [kworker/u:0] 19774 ? S 0:00 [kworker/0:0] 19792 pts/5 R+ 0:00 ps ax 20868 ? S 0:00 /usr/sbin/apache2 -k start 20871 ? S 0:00 /usr/sbin/apache2 -k start 30818 ? S 0:00 /usr/sbin/apache2 -k start 30908 ? Sl 0:12 /opt/google/chrome/chrome --type=renderer --enable-de 31197 ? Ss 0:00 /bin/sh -c gnome-terminal 31198 ? Sl 3:18 gnome-terminal 31202 ? S 0:00 gnome-pty-helper 31203 pts/2 Ss+ 0:01 bash
Learning new commands and new skills on the terminal takes time but for sure it pays off one day. I am not a ninja on the console but for sure I know how to securely delete files, how to find processes that are consuming my entire memory and also how to create directories for saving my projects and files.