Posted in: News
Why Use The Linux Terminal?
Most Linux distributions have GUIs (graphical user interface) allowing you to point and click, drag and drop and load applications by double clicking allowing you to get your work done without having to understand what is happening in the background / under the hood.
CLI (command line interface) is where you type the commands to tell your PC or Server what to do. you may ask yourself why should I bother to understand and learn all the commands when you have a GUI.
Well firstly not all Linux distributions come with a GUI, Ubuntu Server would be a good example, and yes you could install a GUI such as GNOME or KDE but CLI is actually quicker and more powerful than the GUI once you are familiar with the basic commands.
This Blog will provide you with the basic commands to get you started within Terminal.
Executing Commands With Administrative Privileges
"Sudo" (Superuser Do) is the terminal command to execute with administrative privileges, if you are also a Windows user you would have seen the "User Access Control" pop up when an application needs to run with administrative privileges.
Why is this needed? a good example of when the Sudo command would be used if when you are working with directories or files that are not owned by your user account.
When using the Sudo command you will be asked to enter a password, only users with the correct admin privileges will be allowed to run commands as Sudo.
Take caution when executing commands with Sudo as you could damage your operating system.
Let me give you an example of where I have executed a command in terminal without using Sudo, you can see that my account "mike" does not have the appropriate permissions to lock the directory (/var/lib/dpkg)
Now let's run the same command again but this time using Sudo, as you can see I was prompted for a password for the superuser account and once provided the command was able to execute successfully.
Running File and Directory Commands
So before we jump into some basic file and directory commands it is important to know that the tilde symbol (~) represents your home directory, so for a user (~) would represent /home/user.
"pwd" this stands for (Print Working Directory) and it will show you what directory you are currently in. i.e. pwd in ~ would show (/home/user)
"ls" meaning (list) running this command with show you the files that are within your current directory, if you are also a windows user you would know this as dir within CMD, you don't have to be in the directory to see what files are within it, you could run as an example ls /var/log, this would show you the files and folders within the var/log directory.
To see further details such as permissions, owner, time and date of when the file was last modified you can run "ls -lt" for a full list of arguments you can type "ls --help"
"cd" stands for change directory, so when you load up the terminal by default you will be in the home directory /home/user. as an example if you wanted to navigate to var/www/html you would run cd /var/www/html it's useful to know that you can auto complete the command by pressing tab, providing there were no other folders within the directory starting with v (in this example) tab would auto compleat /v to /var. if there were multiple folders or documents starting with v terminal would display them options to you. some other useful cd commands to remember are,
- "cd /" This will navigate you to the root directory.
- "cd" or "cd ~" will take you to your home directory.
- "cd .." will take you up a directory.
- "cd -" will take you to the previous directory.
Another example where you can copy a file from one directory to another without navigating to it would be "sudo cp /var/log/auth.log /home/user"
This would copy the auth.log from /var/log to /home/user/auth.log.
The "mv" command can move a file from one location to another or can also be used to rename, so to rename something, I will use the auth.log located in /var/log again in this example.
"sudo mv /var/log/auth.log /var/log/authold.log
by running the above command it would rename the auth.log to authold.log.
Note that if you run the command as sudo and reference your home directory by ~ it will not place the file in the root home directory it will go to /home/user from user account you ran the sudo command from. to place files in root home directory you will need a root shell, you can get this by typing sudo -i or sudo -s. note you're name@host will change from user@host to root@host.
"rm" will remove a file from you specified directory i.e. "sudo rm /home/user/auth.log"
"rmdir" will remove and delete an empty directory only, to delete a directory that contains files you should use "rm -r" i.e. "sudo rm -r /var/log" would delete the log folder and all its content.
"mkdir" this command will allow you to create a folder and name it as an example "sudo mkdir /var/log/old-logs" would create a folder within /var/log called old-logs.
Running a File
To run / execute a file from within terminal you simply use "./" let me give you an example of this command. within the vm ssh connection detailed below in the screen capture i have executed the command "sudo ./ts3server_startscript.sh status" you can see as its been ran with sudo i need to provide the password, the status is then reported back. to start this i would have ran "sudo ./ts3server_startscript.sh start" and to stop you guessed it "sudo ./ts3server_startscript.sh stop"
"df" this command with show you all mounted partitions and disk space, using "df -h" h meaning human readable it will display M megabytes or G gigabytes.
"du" will show you disk usage for a specific directory i.e., "du /var/log"
"free" this command will show you the free and used memory in the system. "free -m" will show you the information in megabytes.
"top" meaning table of processes command will show you the running processes and system resource each process is consuming.
If you are looking for resource monitoring within terminal i would recommend htop.
"uname -a" command will display all system information including machine name, kernel name and versions. very useful to find out what kernel version you are running.
"date" will show you the date and time of the system.
"cal" will show you a calendar.
"clear" will clear everything within the terminal window.
"ifconfig" this command will show you the network cards configurations such as your ip address, MAC address, gateway, subnet mask.
I hope you have found this overview of linux terminal usefull, feedback and comments welcomed.
Here are two of our favourites