A shell is a computer program interface (generally, a command-line interface) that allows users to interact with the operating system using the instructions written in alphanumeric characters. A shell accepts human-readable commands and utilities, which are processed by the kernel for performing various tasks.
Most shell programs make use of the terms standard input and standard output while interacting with the users. The standard input refers to the stream of data that is read by the program and standard output refers to the data stream given as output by the program. The shell can either be in your local machine or over a network in the form of a virtual machine. PuTTY is an open-source application that enables Windows users to log in to the Linux shell (virtual machine) over a network.
The name ‘shell’ is quite self-explanatory. It is the outermost part of the operating system that interacts with the user. The instructions are then passed to the inner portion, i.e. the Kernel . It is responsible for communication between hardware and software. The shell assists you in performing tasks like file management , process management , and system configuration . This article discusses some shell commands that will be needed while performing system configuration and other tasks in your system.
There are a variety of shells available like MS-DOS shells, PowerShell and csh. In this article, we will be discussing the commands related to Linux and other UNIX-like operating systems’ shells i.e. Linux shell commands. So, let’s dive into the commands section!
Shell Commands in Linux
The section consists of several domains. Each domain will have a certain number of commands along with the respective outputs in the terminal. This article uses the Ubuntu distribution of Linux for providing outputs. However, feel free to use the same commands on other distributions as well.
1. User management commands
Linux is a multi-user operating system. This means that multiple users can remain active at a time on the same machine. Due to this, managing the sessions and users becomes an essential task. You may need to create users or groups in Linux and even delete them or end their session forcefully. Most of the time, you will need to be a root user in order to run some shell scripting commands. Let’s discuss a few common commands that are used while performing user and session management in Linux:
a. sudo
The sudo command provides you the facility to run certain programs and complex and basic shell commands with certain security privileges. With this command, you are giving instructions as a Linux superuser . The Linux superuser is a special account that has all rights and privileges to perform all kinds of system tasks. Operations like the creation of users/groups, installing or removing an application, updating a package, rebooting your system, etc. can only be done using the sudo command or otherwise, the system will deny your access.
Depending on the OS, the name of this account can be root, admin, administrator, etc. Giving instructions with a sudo command prompts you for the root password. The program executes once you get authenticated as a superuser. Let’s see a few examples of how to interact with your OS using the sudo command:
- To list all the privileges of the superuser.
$ sudo -l
- The below command is used to update the packages that are looking for updates. If you are facing issues in installing any package, the following command may resolve most of the installation issues.
$ sudo apt-get update
- Let’s write the commands to install and remove a package from the system. In Linux distributions, a “package” refers to a compressed file archive containing all of the files that come with an application. To install a package into your system, execute:
sudo apt-get install {package_name}
As an example, let’s download git in our system using the above command.
- To remove a package into your system, execute:
sudo apt-get remove {package_name}
Let’s try removing git from the system that we just installed.
- To reboot your system, execute:
sudo reboot
Rebooting is a process in which your computer system is restarted to either recover from errors or to complete the installation of certain programs and applications.
b. logout
The logout command is used to end the session of a particular user. Ending your current session is really easy in Linux. You just need to type the following shell scripting command:
$ logout
Linux also provides you the power to end the session of other users. However, you need to be a root user in order to perform this task. The ‘ who’ command can be used to see the list of users that are currently logged in to your system. Ending the session of another user can be done by using the pkill command. The syntax for this is:
$ pkill -KILL -u {username}
Let’s end the session of a user named “ test ” that we created already for the demonstration purpose:
Refrain from killing the root user by using this command, as this will end all the processes running in your system.
c. useradd
This command allows you to create a new user in your system. You will need to be a root user in order to create a new user.
$ sudo useradd user_name
The above command prompts for the root password and creates a new user. If not prompted for the password of this new user, you can set the password for this new user by running the following command:
$ sudo passwd user_name
d. userdel
Linux provides the utility to delete the account of a certain user along with all the records and entries associated with this user. The syntax for deleting a user in Linux is:
$ sudo userdel [option] username
Let’s see a few examples of how to use this command with different options:
$ sudo userdel -f test
- This command forces the deletion of the user irrespective of whether the user is logged in or not in the system.
- You can view other options for this command by using the -h option after the userdel command. Please note that not providing any option with the userdel command will throw an error.
e. groupadd
A group in Linux is nothing but a set of one or more users. Creating groups makes it easy to perform common operations on multiple users. For instance, if you want to set privileges for some users then doing this operation individually for every user becomes a tedious task. Instead of doing the aforementioned, you can create a group of users and perform common operations on all users. Let’s create a new group named “grp” and add a few users to it. The syntax for creating a group is:
$ groupadd [option] group_name
Similar to the above examples, groupadd requires you to be a root user to run the command. Let’s create a new user named “develop” and add it to the existing group. To add the user in the group while creating it, we need to specify the -g option with the useradd command.
$ sudo useradd -g grp develop
To check if a user belongs to the same group, you can use the groups command. Let’s use the above commands one by one.
To add an existing user into the group, write the following command:
$ usermod -g grp develop
f. delgroup
The command is used to delete a group from the system. The syntax for this command is:
$ delgroup [option] group_name
To check various options available for this command, you can use the --help option. --group is the default option that is invoked when this command is executed.
2. Terminal navigation commands
Let’s look at another set of commands that help you in navigating through your Linux terminal:
a. pwd
The current working directory is the directory where your command-line interface currently points to. You can check your current working directory using the pwd command. Run the following command in your terminal and you will see the full path of your current working directory:
$ pwd
b. ls
This command lists all the files and directories in the current working directory. For this, you just need to type ls in your terminal. However, the ls command can also list the content of other directories. For this, you will need to write the name of that particular directory after the command. For instance, the following command lists all the files and directories in the “ test ” directory:
$ ls test
To list the files recursively, provide the flag -R after the command. Flags are the options that change the way a listing of files and directories is done.
c. cd
It is the shorthand for “change directory”. The command is used to switch the present directory with another directory. The syntax for the command is:
$ cd [directory_name]
This command will take you to the sub-directory whose name is mentioned in the command. To go to the root directory, type the following command in your CLI:
$ cd /
To move to the parent directory of the present working directory, write the following command:
$ cd ..
Let’s see the above commands one by one with an example. We have used the pwd command to show you the switching of the directory after using cd.
d. locate
locate is used to find a particular file by name. It works faster than its alternative i.e, the find command. The syntax for the command is:
$ locate file_name
e. du
This shell command is shorthand for “disk usage.” It is used to estimate file space usage. You can use it to see the objects that are consuming excess space in your disk.
3. Files and directory management commands
This is one of the most common and important portions of Linux shell commands. While working with software, you will frequently experience file and directory management commands. These commands usually deal with the management and creation/deletion of files or a group of files (a directory). These commands also allow us to open a file in the same command-line interface window and edit the file using various text editors like nano and vim. Let’s see a few shellscript commands examples that you will find helpful while working in this domain.
a. touch
The touch command is a basic shell command, which is used to create an empty file in your system. This is usually used when you don't have any content for the file right now but may add the content later in the file. The syntax for the command is:
$ touch test.txt
The above command creates an empty file named " test.txt " in your present working directory. You can even create multiple files in Linux by separating the names of respective files with spaces.
You can verify if the file was created using the ls command that we discussed above.
b. mkdir
The mkdir command is used to create a new directory or folder. Please note that the user needs to have enough permissions while creating a new directory, otherwise the system will deny the permission. The syntax for creating a directory in the present working directory is:
$ mkdir [options] [directories]
To create a new directory in one of the sub-directories of the present working directory, we use the -p option. For instance, if you wish to create a new directory named "test2" in the "test" sub-directory and you are in the directory named "Desktop" , then you should use this command:
$ mkdir -p Desktop/test/test2
c. cat
It is a frequently used command in Linux that is used to display, create and manage the content of multiple files. As we discussed earlier, the touch command is used to create an empty file in your system. In many cases, you may need to insert some content into the file during its creation. The cat command can be a handy tool in this case. You can also view the content of the existing files using the following command:
$ cat file1 file2 ...
Let’s also create a new file named “file.txt” and add content to it at the time of creation. Type the following command in the terminal and press Enter to add the content to this file. To save the file once the content is added, press Ctrl+D .
$ cat > file.txt
To append to an existing file, use the >> symbol after the cat command as shown below:
d. rm
It is a command-line utility to remove objects like files and directories from your system. The general syntax for the rm command is:
$ rm [option] file/directory..
Providing the -i option after rm will prompt the user before removing the file. It is usually recommended to use -i with this basic shell command. In order to remove the directory, you can include the -rf option in the command and give the directory a name:
e. grep
This command is helpful in searching for a pattern in a file. Let’s see an example to understand how this command will find out the matching content:
In the above example, we searched for the letter “H” in “test.txt” that got matched at one place.
f. sort
This command sorts the content of the file line by line. For clear understanding, see the example below:
4. System status commands
It is quite common that you will need to configure or get the status of your system from time to time. The following shell commands (Linux) are helpful when you need to get certain status or information regarding your system.
a. ps
Since Linux is a multi-tasking operating system, multiple processes can run simultaneously without interfering with each other. The ps command is helpful in listing the currently running processes in your system. Like other shell scripting basic commands, it also has some options that you can provide along with the command. The ps is shorthand for “Process Status . ” The command lists the following 4 columns in the terminal:
- PID - process ID.
- TTY - terminal type.
- TIME - total accumulated CPU utilization time for a particular process.
- CMD - the name of the command that launched the process.
b. kill
The kill command in Linux is a built-in command that is used to terminate processes manually. It sends a signal to a process, which terminates the process. If the user doesn’t specify any signal, which is to be sent along with the kill command then a default TERM signal is sent that terminates the process. To kill a process with a certain ID, you can list the currently running processes using the ps command as we discussed above and then use the following syntax to kill a process:
$ kill pid
c. date
This is among the basic shell commands. As the name suggests, it prints the current date of the system.
d. uptime
uptime returns information about how long your system has been running together with the current time , the number of users with running sessions , and the system load averages . It can also filter the information displayed at once depending on your specified options. Load average is the average number of processes that are in a runnable or uninterruptible state. A process is in a runnable state when it is using the CPU or waiting to use the CPU; while a process is in an uninterruptible state when it is waiting for I/O access like waiting for a disk.
e. time
The time command is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent executing a command when it terminates. ‘real‘ time is the time taken by a command to get executed, ‘user‘ and ‘sys‘ time are the number of CPU seconds that command uses in user and kernel mode, respectively. The syntax for the time command goes:
$ time [option] [command]
Let’s run this for the sleep command and see the results.
5. Computer networking and communication commands
These Linux shell commands are used to establish communication between multiple machines. These may be helpful when you need to get or send a file between multiple systems or to test a connection with the server. Let's look at some of the important communication and networking shell commands:
a. telnet
The telnet command is used to create a remote connection with another system over a TCP/IP network. It allows us to administrate and access other systems using our terminal. This utility uses the TELNET protocol due to its simplicity and a wide variety of use. However, this protocol has some security issues. To connect with some other system, you can either enter the hostname or its IP address.
$ telnet hostname/ip_address
Please note that the above command may not run for the www.google.com host. It’s just for demonstration purposes. However, you can try the same basic shell command for your custom hosts or IP addresses.
b. ping
This Linux shell command is used to check whether a particular host is active. As the name suggests, this basic shell command sends some number of packets to a host and checks if the host is returning the same number of packets. This is often useful in debugging and checking the connectivity status of the host. The ping command uses the ICMP(Internet Control Message Protocol) to send an ICMP echo message to the specified host; if that host is available then it sends an ICMP reply message. The syntax for the ping command goes:
$ ping hostname/ip_address
The above command keeps flooding the host with data packets until you cancel the transmission. Using the -c option with the command limits the number of packets you need to send in order to check the network status.
c. curl
This command is among the shell scripting commands for automation tasks where you need to send and receive the data without human interaction from some host. You can send large files using this utility and even resume your downloads if they get interrupted due to network issues. It supports almost all kinds of protocols including FTP , SMTP , HTTP , and POP3 . The syntax for the curl command is:
$ curl -o [file_name] [url]
Using the -o option saves the file with the name given in the next option. Let’s download a sample file in our system using this command:
We can also send mails using curl, which uses the SMTP protocol using the following command:
curl -url [SMTP URL] -mail-from [sender_mail] -mail-rcpt [receiver_mail] -n -ssl-reqd -u {email}:{password} -T [Mail text file]
Conclusion
In this article, we discussed several shell commands that you may use while working with a popular (or not) Linux distribution . These shell script commands help you to perform operations like configuration, debugging, and analysis in your system. You will be using these commands quite often in almost every project or task that you will do on Linux. There are a lot more utilities that Linux provides. Feel free to explore more commands and utilities, analyze their behavior with different options, and compare to your needs.
People are also reading:
Leave a Comment on this Post