Menu Close

Ubuntu Common Shell Commands

This article talks about the basic commands that can be used in the Ubuntu Shell, which provides a basis for all future commands.

For more on this subject, please refer to the SOC Table of Contents.

 

Shell commands are most commonly used in Embedded Development, and the Shell syntax is shared universally with all Linux operating systems. Thus, you are not restricted to just Ubuntu, but you may also use the same commands in other Linux flavors such as Debian and Kali.

 

Getting directory information using ls

This allows for the basic operation of browsing files using Shell. The formatting is as follows:

ls [option] [path]

The main purpose of ls is to display what is stored under a specific directory. The main options to choose from are:

-a        Displays all of the files and directories, including hidden files that are prefixed with a “.”

-l        Displays the details of the file, which includes information such as the status, permissions, authors, and size.

-t        Lists the files in chronological order based on the date the file was created.

-A        Does the same as -a, but doesn’t list the parent directory (..) and the subdirectory (.).

-R        Recursively lists all of the files including the files contained in subdirectories.

Note that you can combine multiple options together. For example, if we use -al, it will display the details of all the files under the current directory, including hidden files prefixed with a “.”

Changing the directory using cd 

If you want to change the directory using the Shell, you will need to use the cd command to do so. The formatting is as follows:

cd [path]

Where path is the path to the directory that we want to access.

For example,

cd /                Takes us to “/”, which is the root directory in Linux systems.

cd/usr            Takes us to the “/usr” folder.

cd ..              Takes us to the upper-level parent directory.

cd ~                Takes us to the directory of the current superuser.

If we want to go to our Desktop and see what files are in it, we will execute the following commands in the terminal.

Note that after using the cd command the terminal prefixes the terminal line before the $ with the current directory, and it doesn’t display anything because we don’t have anything on our Desktop.

We can try the same with /usr.

 

Displaying the current path by using pwd

pwd displays the current path and directory without needing any other commands. It runs on its own.

An example is shown below.

 

Displaying the system information by using uname

There are three options to choose from:

-r        Displays the details regarding the current system’s core.

-s        Displays the name of the system’s core.

-o        Displays the system information.

 

Clearing the terminal interface

There are two commands to choose from:

clear              Shifts all of the current commands in the terminal up until there’s only a new line shown at the top.

                        You may choose to look at previous lines and commands by scrolling up.

reset              Deletes everything in the terminal and restarts the terminal.

 

Changing the command execution user using sudo

Like Windows, Ubuntu is an operating system that supports multiple users. The equivalent of a Windows Administrator in Ubuntu is the Superuser root. Some operations can only be carried out by using root as the identity, such as the installation of software. We can use the sudo command to temporarily grant us root permissions. Because our user does not have root permissions, we need to use the sudo command to use root to execute the adduser command.

Note that when we first tried to use the adduser command, it said we did not have the authority to do so; but when we used the sudo command before using the adduser command, it allows us to proceed because we were executing the command as root.

 

Adding a user using adduser

As shown above, this command needs to be run as root. The formatting is as follows:

adduser [option] [username] 

The most common options are as follows:

-system                      Adds a new system user

-home DIR               Sets a home directory, where DIR is the path to the user’s main directory to be set.

-uid ID                       Sets a user ID, where ID is the user’s ID to be set.

-ingroup GRP            Sets a user group, where GRP indicates the group to which the user belongs to.

The usage of the adduser command can be seen in the previous sudo demonstration.

Deleting a user using deluser

Like adduser, this command also needs to be run as root. The formatting is as follows:

deluser [option] [username]

The most common options are as follows:

-system                                 Can only delete the specified user when the current user is a system user.

-remove-home                  Deletes the home directory of the specified user.

-remove-all-files              Deletes all files related to the specified user.

-backup                                Backs up the data and information of the specified user.

As an example, we will delete the test user we previously created in the root demonstration.

 

Changing the user by using su

Using the su command, it can grant the current user root permissions, and not need to use the sudo command before every command that needs it. The formatting is as follows:

su [option] [username]

The most commonly used options are as follows:

-c -command    Executes the specified command, where command is the specified command.

-login                     Changes the user’s identity, changing the work directory and the PATH environment.

-m                         Changes the user’s identity but doesn’t change the environment.

-h                           Displays help documentation.

By entering the following command, it changes the current user to root.

sudo su

To revert back to a normal user, simply enter

sudo su [your username]

*Because of the large amount of power root has, you may accidentally delete critical system files if you’re not careful and end up breaking the system. Thus, it is highly recommended that you don’t run Ubuntu as root, and just use the sudo command when you need to use root permissions.

 

Reboot and Power Off

You can do both of these things by directly executing the following Shell commands:

reboot                       Restarts the system.

poweroff                     Shuts down the system.

 

Displaying file information using cat

gedit is the Ubuntu equivalent of Windows Notepad, and cat is the Shell equivalent. The formatting is as follows:

cat [option] [file]

The most commonly used options are as follows:

-n        Number every output line starting from 1.

-b        Number every non-empty output line starting from 1.

-s        Combines consecutive empty lines into a single empty line.

For example, if we open the file /etc/environment, it will output the following. Because it only has one line, when we use the -n command it only numbers the first line.

 

Displaying and setting internet properties using ifconfig

The formatting is as follows:

ifconfig [interface] [options] [address]

Where interface is the name of the internet connection port, which may be something like eth0.

The most commonly used options are as follows:

up                      Turns on the internet device.

down                 Shuts off the internet device.

add                    Setup the internet IP address.

netmask add   Adds a subnet mask.

An example on using ifconfig to show current internet properties is shown below.

 

System helper command using man

man serves as a help manual to all of the different commands. The formatting is as follows:

man [command name]

If we wanted to get help for the ifconfig command, we can simply execute the command

man ifconfig

Which would return the manual page for the command.

 

Installing software using install

The main way to install Ubuntu software is through terminal Shell commands. The formatting is as follows:

install [option]… [-T]  Original File     Target Location

or 

install [option]…       Original File     Directory

or

install [option]… -t    Directory   Original File

or

install [option]… -d    Directory

The install command takes the file, which is typically already compiled, and copies it to the target location. In the first three options listed above, the files are copied to an existing location; whereas the last option will create the specified directory.

The install command is commonly used with the apt-get command, which we will talk about next.

Posted in Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!