Basic Commands in Linux
In this tutorial we will learn about some basic commands in Linux. Although, Linux is an open-source OS. It could be a twin of older UNIX software. However, Linus Torvalds created the Linux software from scratch. Additionally, Linux software package is a free and open-source software system. Therefore, it implies that anyone can simply change the Linux code and compile and redistribute it in one’s own name. Although , in reality, there are many Linux Distributions within the market. However, Linux distributions are also called “distros”. A number of the samples of distros are:
- Red hat Enterprise Linux
- Linux Ubuntu Linux
- Arch Linux
- SUSE Linux
Today Linux is obtainable in Desktop and Server editions. Moreover, the Server Editions of Linux is mostly employed within the industry. Although it’s impossible to grasp the accurate percentage share of Linux Servers within the industry but it’s assumed that over 70% of the net is powered by Linux servers. This is often because Linux is free, fast, and secure. The high cost of Windows Servers acts as deterrent for using it in contrast to Linux Servers. Moreover, the Android OS that runs about 70% of smartphones within the world, also has the Linux kernel. Additionally, a virus may be a major problem in Windows, but Linux id free from viruses.
Linux Shell or “Terminal”
The Linux command shell is a systems program. Moreover, it receives Linux commands from any input device and transfers to the Operating System for processing. After the Linux command is processed, the Linux shell shows the output of the Linux command on the screen. Also, Linux shell is much similar to Windows PowerShell or Command shell. In this tutorial, we are going to learn Basic Commands in Linux that we use in the Linux shell. We will be using Ubuntu Linux for this tutorial.
To open the Linux Terminal you have to press Ctrl+Alt+T simultaneously, or you have to press Alt+F2 key.
Basic Commands in Linux
The “clear” Command
The most Basic Commands in Linux “clear”. We can use the “clear” command to clear the terminal.
The “ls” Command
“ls” command is similar to the “dir” command of windows. We use this command to view a list of files in a directory or sub-directory. We can view all the hidden files by using the “a” switch in “ls” command like “ls -a”.
The “rm” Command
We use the “rm” command to delete files. We use the “r” switch of the “rm” command to delete the directory as well as the files in the directory. If the directory name has two or more words with space in between them. Then this directory name will be the argument of the “rm” command.
The “cd” Command
We use the “cd” command to move to any directory. For example, if we are in the home directory, and we want to move on to the Desktop directory, then we have to type in “cd Desktop” in the Linux terminal. This has to be kept in mind that Linux is case sensitive. We have to type in the name of the directory exactly as it is. If the directory name has two or more words having space between them, then we have to type the directory name in between the inverted commas. For example, if the directory name is “Program Files” then the “cd” command will be cd “Program Files”. If we type the “cd” command in the terminal and then press enter, then this will take us to the home directory of the user. To move to one level up, we shall type “cd ..”. The two dots represent one level-up directory.
The “pwd” Command
It is one of the Basic Commands in Linux. When we first open the terminal, by default we are in our home directory. To know in which directory we are in, we shall use “pwd” The “pwd” command displays the absolute path of the Home Directory. The root directory of the Linux System is the base directory of the Linux file system. The root directory in the Linux System is represented by a forward slash (/). The Linux user directory is usually represented as “/home/username”.
The “mkdir” & “rmdir” Command
We use the “mkdir” command when we need to create a new directory. For example, if we want to create a directory called “Test”, then we have to type “mkdir Test”. If the directory name has two or more words with space between the words. Such directory name must be enclosed within the inverted commas. We use the “rmdir” command to remove a directory. The “rmdir” command is used to remove an empty directory. To remove a directory containing files, we shall use “rm-r”.
The “echo” Command
We use the “echo” command to write some data, usually text data into a file. For example, if we want to create a new text file or add some text to an already existing text file, we need to type in, “echo the text data to be appended in the file >> test.txt”. If we use single > sign then the existing data in the file will be overwritten by the new data.
The “cp” Command
We use the “cp” command to copy files from one directory to another or to make a duplicate copy of a file with another name. This command takes two arguments. The first argument is the location and name of the original file. The second argument is the location of the copy of the original file. Also the new name of the original file.
The “mv” Command
We use the “mv” command to move files and directory from one location to another or to rename a file or a directory. For example, if we want to rename the file “test1” to “test2”, we can use “mv test1 test2”. The mv command needs two arguments, just like the “cp” command described above.
The “cat” Command
We use the “cat” command to display the contents of a file. The syntax is “Cat <File Name>”.
The “touch” Command
The “touch” command is used to create a new file. The file can be of any type, it can be an empty txt file or an empty zip file. For example, “touch test.txt”.
The “man” and “help” Command
The “man” command shows the operating system command manual. Additionally, the “man” command displays the syntax of Linux commands. It also shows the manual pages of the command we want to know about. For example, the command “man cd” shows the manual pages of the “cd” command. Typing in the command name as the argument helps it show which ways the command can be used (e.g., cd –help).
The “locate” Command
The “locate” command is used to locate a file in a Linux system. This command has used the location of a file. If we use the “-I” switch with the command it ignores the case. if we want to find a file that has the word “test”, it displays the list of all the files in our Linux operating system containing the word “test”. If the file name has two or more words having space between the words. Then the file name will be typed between the inverted commas as we have seen earlier in “cd” command. For example, to locate a file containing the words “program” and “data”, we can use the command “locate -i *program*data”.
The “nano” and “vi” Editors
When the Linux Desktop system is installed the “nano” and “vi” text editors are installed by default with the Operating System. The “nano” text editor is easier and better than the “vi” editor for text editing and is a multilingual editor. We can create a new file or modify a file using these editors. For example, if we need to make a new file named “test.txt”, we can create it by using the command “nano text.txt”. We can save files by using the key combination Ctrl+O.
The “sudo” Command
The most used command in the Linux command line, “sudo” stands for “SuperUser Do”. To execute a command with administrative or root privileges. We can use the “Sudo”. For example, if we want to edit a file like viz. “pentarock.conf”, which needs root permissions. Then we can use the command “sudo nano pentarock.conf”. We can enter the root command line using the command “sudo bash”, then typing in our root user password. We can also use the command “su” to do this.
The “df” Command
We use the “df” command to see the available disk space in each of the partitions of the hard disk in our system. We can just type in “df” on the command line and we can see each mounted partition of our hard disk and its used and available space in % and in KBs. If we want it shown in megabytes, we can use the switch “m” as “df -m”.
The “du” Command
We use the “du” command to find the disk usage of a file or directory in our system. If we want to know the disk usage for a particular directory or file in Linux, we can type in the command “du” and the name of the directory or file. For example, if we want to know the disk space used by the Downloads folder in Linux, we can use the command “du Downloads”. We can also use the command “ls -lah” to view the file sizes of all the files in a directory.
The “tar” Command
The “tar” command is used to compress and decompress files and directories in the Linux system. However, the tar command has a long list of switches. To compress and decompress files use Tar command. Tar has different types of tar archives like .tar, .tar.gz, .tar.bz2 etc. The examples are, “tar -cvf” for creating a .tar archive, “tar –xvf” to decompress a tar archive, “tar-tvf” to list the contents of the tar archive.
The “zip” and “unzip” Commands
We use the “zip” command to compress files into a zip archive, and the “unzip” command to extract files from a zip archive.
The “uname” Command
We use the “uname” command to display the information about the system, our Linux distro is running. Using the command “uname -a” displays most of the information about the system. This “uname -a” command displays the kernel release date, version, processor type, etc.
The “apt-get” Command
We use the “apt” command to work with packages in the Linux command line. We use “apt-get” to install Linux packages. This command requires root privileges, so we have to use the “sudo” command with it. For example, if we want to install the text editor “vim”, we shall type in the command “sudo apt-get install vim”. Update the repository before installing a new package. We can do this by typing “sudo apt-get update” and upgrade the system by typing “sudo apt-get upgrade”. We can also upgrade the distro by typing “sudo apt-get dist-upgrade”. To search for a Linux package in the system we use the command “apt-cache search”. If we want to search the “vim” package, then we can type in “apt-cache search vim” (this command does not require root permissions).
The chmod Command
The chmod command is used to change the permissions of a file or a directory in the Linux system. To make a file executable in a Linux system we use “chmod +x FileName” Just think if we have a python file named hello.py in our system. We will need to run “python hello.py” every time we need to run this file. When we make this file executable, we just need to type “hello.py” in the terminal to run this file. The command “chmod +x hello.py” is makes a file executable in Linux. To provide root permissions to this file we shall use the “chmod 755 hello.py” command.
The “hostname” Command
We use the “hostname” command to know our hostname in our host or network. It displays our hostname and IP address. Typing “hostname” gives the output. Typing in “hostname -I” gives us our IP address in our network.
The “ping” Command
We use the “ping” command to check our connection to any server on the internet. The “ping” command is a computer network administration command. It checks whether a client computer could connect to the host server computer in the network. When we type in “ping google.com”, the “ping” command checks whether it can connect to the Google server and get a reply from the google server. It measures this round-trip time in connecting to the google server and gives us the details about it. We use this command is to check whether our internet connection is live or not. If the ping command gets a reply from the google server. Then the internet connection is active and working fine.
Some Tips and Tricks
Use the TAB key to auto-complete commands in the Linux terminal. For example, we just need to type “cd Dow”. Then if we press the TAB key on the keyboard and if the Downloads directory exists in the system. Then the command autocomples to “cd Downloads”.
Similarly, the Ctrl+C key combination stops a running command in the terminal. If any command does not terminate on pressing the Ctrl+C key combination then to terminate such command forcefully we use the Ctrl+Z key combination on the keyboard.
We can exit from the terminal by using the exit command or the key combination Ctrl+D.
We can power off or reboot our computer by using the command “sudo halt” and “sudo reboot”.