Linux Commands

Linux commands are case-sensitive and are written in lowercase. Let’s take a look at some basic information about Linux commands, and then we will dive deep into the individual commands.

We issue the commands using a shell. Most Linux distributions come with bash, which is an enhanced version of shell(sh).

Bash = bourne-again shell

Or we can use a terminal emulator when we use GUI. The terminal emulator will communicate with the shell and execute the commands.

Terminal Initial Status

When we open a terminal we see there is already some text written and we can enter our command after that.

We can see one of the 2 cases-

Case #1: Normal User

If the user accessing the terminal is a normal user then we see the following things initially-

  • Mention of user name and machine name in the format: username@machine_name
  • Then a colon(:).
  • The current directory, like- the home directory(~) or any other directory path that the terminal is accessing.
  • Dollar sign($) at the end.
username@machine_name:~$

# Or we can see something like below
username@machine_name:/etc/nginx$
Bash

Case #2: Super User

If the user has superuser privileges, then-

  • Mention of user name and machine name in the format: username@machine_name
  • Then a colon(:).
  • The current directory, like- the home directory(~) or any other directory path that the terminal is accessing.
  • Hash (#) at the end.
root@machine_name:~#

# Or we can see something like below
admin_username@machine_name:/etc/nginx#
Bash

Check Shell

We can check which shell the current user is using. Use the following commands to check the current shell-

To check the shell of the current user, which is set in the ‘etc/passwd‘ file, use the command below

$ echo $SHELL

/bin/bash
Bash

To check which shell the current user is using right now, use the following command-

$ echo $0

-bash
Bash

We can also check the process of the current shell for the process list-

$ ps -p $$
  PID TTY          TIME CMD
55782 pts/10   00:00:00 bash
Bash

Virtual Terminals

Virtual terminal is an interface that the Linux kernel provides to run multiple command line sessions at the same time.

We can access the virtual terminals using shortcut CTRL + ALT + Function keys(F1 to F6), like below-

CTRL + ALT + F1 , CTLR + ALT + F2 , CTLR + ALT + F3 , and this goes upto CTRL + ALT + F6

There is also CTRL + ALT + F7 , which is used to go to the graphical user interface.

We can use shortcut ALT + Function keys(F1 to F6) to alter virtual consoles. Like, ALT + F1, ALT + F2 , upto ALT + F6

On top of the terminal you can see the terminal number in the format – tt[number here].

Here is how it looks-

Linux Virtual Terminals
Linux Virtual Terminals

Get Suggestion

To get suggestions to complete a command type part of the command and then press “tab” twice (“tab” + “tab”). This will give suggestions to complete the command.

$ chm
# Press tab + tab, and you will see suggestions like below

chmem  chmod

$ chm
Bash

Check Current Directory

Use the “pwd” command to print the current working directory.

$ pwd

/home/bigboxcode
Bash

When you login to the terminal, the working directory is set to the home directory of the user. Which is by default, a directory inside the ‘/home‘ directory.

Search Commands

We can search the command manual page name and description using “apropos” command. This way we can find commands that we want to use

# Search for commands that has "chm" in name or description
$ apropos chm

ab (1)               - Apache HTTP server benchmarking tool
chmem (8)            - configure memory
chmod (1)            - change file mode bits
chmod (2)            - change permissions of a file
fchmod (2)           - change permissions of a file
fchmodat (2)         - change permissions of a file
redis-benchmark (1)  - Benechmark a Redis instance
Bash

Let’s search for commands that have “his”, as we want to see what case we use for history or other related functions-

$ apropos his

byobu-ugraph (1)     - helper script for notification history graphs
docker-history (1)   - Show the history of an image
docker-image-history (1) - Show the history of an image
git-merge (1)        - Join two or more development histories together
history (3readline)  - GNU History Library
lcf (1)              - Determine which of the historical versions of a config is installed
pam_pwhistory (8)    - PAM module to remember last passwords
run-this-one (1)     - run just one instance at a time of some command and unique set of arguments (useful for cronjobs, eg)
tracepath (8)        - traces path to a network host discovering MTU along this path
Bash

Command Details

Get a detailed description of a command. It also gives us list of all parameters and options.

Let’s see how can we get the details of the “cd” command.

$ help cd
Bash

Alternatively we can use the “–help” flag of a command to get the same information.

$ cd --help

# Same out as "help cd"
Bash

Command Short Description

If you don’t want to go through the full command details in the manual, then you can check the short description using “what’s”-

$ whatis
whatis what?


$ whatis your_command_here
your_command_here (1)               - single line command description
Bash

Here are some examples of “whatis” command-

$ whatis ls
ls (1)               - list directory contents

$ whatis clear
clear (1)            - clear the terminal screen

$ whatis rm
rm (1)               - remove files or directories

$ whatis ls rm clear
ls (1)               - list directory contents
rm (1)               - remove files or directories
clear (1)            - clear the terminal screen

$ whatis history
history (3readline)  - GNU History Library

$ whatis man
man (7)              - macros to format man pages
man (1)  
Bash

Navigate Through History

We can use the Up and Down arrow keys to navigate through the commands that were previously executed.

Linux remembers the last 1000 commands in history. Just press the up arrow key to get the previous command. We will see all the previous commands cycling backward.

Close Terminal

Use the “exit” command to close the terminal-

$ exit

# This will close the terminal
Bash

Command Structure

Commands in Linux follow a similar pattern-

command_name options args
Bash

1) We have the command in the beginning.
2) Then we define some options
3) Then we have some arguments defined by the command.

NOTE

Options and arguments are optional, and can or can not be present for a command.

Binary Path

When we type a command like “echo”, “find”, “ls” or any other command, Linux executes a binary execution file with the same name. If we want to check where those binaries are present then we have to check the value of “$PATH”.

$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
Bash

In the output we can see a bunch of text separated by a colon(:). These are separated paths/locations where Linux tries to find the binary files.

Search for the binary files starting from the leftmost location mentioned in the “$PATH“.

So, in our case, Linux will look into “/usr/local/sbin” first. If not found, then it will look into “/usr/local/bin“, then if required it will look into “/usr/sbin“, “/usr/bin“, “/sbin“, “/bin“, “/snap/bin” afterward.

Note: If binary with the same name is present in multiple directories, then the first one found in the leftmost directory is used.

You can check which binary file is being used for certain commands by using the “which” command.

It will give use the path of the binary file-

$ which echo

/usr/bin/echo

$ which find

/usr/bin/find

$ which ls

/usr/bin/ls
Bash

If the binary for the command is not found in those directories, then we get an error-

$ non_existing_command

non_existing_command: command not found
Bash

Commands

Here are the most used commands that we use while using Linux.

Command Management

Commands

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.