Basic Linux Commands

Sai kumaresh
BeyondX
Published in
15 min readNov 10, 2020

Linux CLI, 50 Most Used Linux Commands, Tips and Tricks

The Linux command line is a text interface to your computer. Also known as shell, terminal, console, command prompts and many others is a computer program intended to interpret commands. It allows users to execute commands by manually typing at the terminal.

There are a few important things to keep in mind when using a Linux shell:

  • It is case sensitive
  • The / (forward-slash) is a special character used as directory separator
  • File extensions don’t matter

50 Most used Commands

When using the CLI there are some basic commands which are used constantly even by the most expert users. Those commands enable us to perform basic operations such as moving around the folders structure, gather information, perform changes.

1.echo

This command is used to print the values you provide it with

Syntax: echo “value”

It can also be used to print variables like HOME with a $ in front of it.

2.whoami

This command reveals the user who is currently logged in.

3.id

This command prints the user(UID) and groups (GID) of the current user.

With a username provided as an argument, information about that user will be printed.

4.uname

It is used to determine the processor architecture, the system hostname and the version of the kernel running on the system.

Syntax: uname -option

Usually, the uname command is used with the “-a” option to print all available information:

The output includes the following information:

  • Linux — Kernel name.
  • kali — Hostname.
  • 5.7.0-kali1-amd64 — Kernel release.
  • x86_64 — Machine hardware name.
  • GNU/Linux — Operating system name.

5.sudo

This command is required when performing actions that require root or superuser permissions, such as changing the password for another user. You need to type “sudo” before the code which needs to be executed as the root user. It requires the login password to become a superuser.

“sudo -s” can be used to run every command as a superuser. This way we need not enter the password for each line. You can use “ctrl+d” or type exit or simply close the terminal to come out of the root mode.

Syntax: sudo command

6.date

This command displays the current date.

7.cal

This displays the calendar for the current month and year.

8.clear

This clears all the previous commands entered and displays a clean terminal.

Syntax: clear

9.bc

bc command is used for a command-line calculator. It is similar to a basic calculator by using which we can do basic mathematical calculations. You can use this command in bash or shell script for evaluating arithmetic expressions.

10.man

This command provides thorough information about the command.

Syntax: man command_name

11.help

Syntax: command_name — help

With almost every command, ‘ — help’ option shows a usage summary for that command.

12.whatis

This command gives a one-line description of the command. It can be used as a quick reference for any command.

13.alias

alias command instructs the shell to replace one string with another string while executing the commands.

When we often have to use a single big command multiple times, in those cases, we create something called as an alias for that command. Alias is like a shortcut command which will have the same functionality as if we are writing the whole command.

Syntax: alias name=”value”

Removing an existing alias is known as unaliasing.

Syntax: unalias [alias name]

alias -p prints all the defined aliases in a reusable format

14.history

The history command is used to view the previously executed commands.

15.chmod

The chmod command is used to change the access mode of a file.

The name is an abbreviation of change mode.

The 3 modes for a file are:

r : Permission to read the file.

w : Permission to write (or delete) the file.

x : Permission to execute the file, or, in the case of a directory, search it.

— : means no permissions have been granted at all.

The first three characters show the permissions for the user who owns the file

The middle three characters show the permissions for members of the file’s group

The last three characters show the permissions for anyone not in the first two categories

  • u: User, meaning the owner of the file.
  • g: Group, meaning members of the group the file belongs to.
  • o: Others, meaning people not governed by u and g permissions.
  • a: All, meaning all of the above.
  • : Minus sign. Removes the permission.
  • +: Plus sign. Grants permission. The permission is added to the existing permissions. If you want to have this permission and only this permission set, use the = option, described below.
  • =: Equals sign. Set permission and remove others.

The digits you can use and what they represent are listed here:

  • 0: (000) No permission.
  • 1: (001) Execute permission.
  • 2: (010) Write permission.
  • 3: (011) Write and execute permissions.
  • 4: (100) Read permission.
  • 5: (101) Read and execute permissions.
  • 6: (110) Read and write permissions.
  • 7: (111) Read, write and execute permissions.

16.pwd

This prints the Present Working Directory(folder)

17.ls

This lists the files in the working directory

ls -a →List all files including hidden file starting with ‘.‘.

ls -l →shows file or directory, size, modified date and time, file or folder name and owner of the file and its permission.

ls -lh →shows sizes in a human-readable format

18.cd

It is used to change the directory. “cd location” — moves to the location directory. While entering the location folder you can use TAB to autocomplete the name of the directory or file. If you need to go to the Downloads folder, we can type “cd Down” and hit the TAB button to automatically fill the rest.

Syntax: cd location

Simply entering “cd” return to the home directory from anywhere.

cd .. → moves back a folder

cd / → moves to the root directory. This is different from the root user

19.mkdir

This is used to create new directories.

  • “mkdir” followed by the folder name creates a new folder in the present working directory.
  • “mkdir location/filename” creates the folder in the given location

Syntax: mkdir filename

20.touch

To create an empty file, use the touch command. If a file already exists, touch will update its timestamp. We use touch to create plain files and mkdir to create directories.

Syntax: touch -option filename

Options:

a → change the access time only

c → if the file does not exist, do not create it

d → update the access and modification times

m → change the modification time only

r → use the access and modification times of file

t → creates a file using a specified time

21.mv

mv stands for move. It has two distinct functions:

(i) It renames a file or folder.

(ii) It moves a group of files to a different directory.

22.cp

cp stands for copy. This command is used to copy files or a group of files or directory to the same or different directory. It creates an exact image of a file on a disk with a different file name. cp command requires at least two filenames in its arguments.

23.rm

rm stands for remove. It is used to remove objects such as files, directories, symbolic links and so on from the file system. By default, it does not remove directories.

This command normally works silently and you should be very careful while running rm command because once you delete the files then you are not able to recover the contents of files and directories.

Syntax: rm -option filename

Options:

r → Delete a folder recursively

i → Interactive Deletion (Confirms the action)

f → force deletion

24.find

The find command in Linux is a command-line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions.

25.grep

The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expressions and print out).

Syntax: grep “word to be searched” filename

Options:

-c: This prints only a count of the lines that match a pattern

-h: Display the matched lines, but do not display the filenames.

-i: Case Insensitive search

-l: Displays a list of filenames only.

-n: Display the matched lines and their line numbers.

-o: Print only the matched parts of a matching line

26.cat

The Cat(concatenate) command is very frequently used to read data from a file and gives its content as output. It helps us to create, view, concatenate files.

Syntax: cat filename — displays the content of the file

Options:

  • cat file1 file2 — displays the contents of all the files listed
  • cat -n file — used to number the lines of the file’s content
  • cat >filename — creates a new file named filename, Type In the content and use “Ctrl+c” to save and exit
  • cat file1 > file2 — copies content of file1 into file2

27.head

Head is used to print the first few lines of a very long file. By default, it prints the first 10 lines of the specified files.

Syntax: head filename

Options:

“-n number” is used to print the first “number” lines of the file

“-c number” is used to print the first “number” characters of the file

“-v” is used to print the filename with its contents

28.tail

The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.

Syntax: tail filename

Options:

“-n number” is used to print the last “number” lines of the file

“-c number” is used to print the last “number” characters of the file

“-v” is used to print the filename with its contents

29.less

Less command is a Linux utility which can be used to read contents of a text file one page per time. If you open a large file using any text editor, then the complete file will be loaded to the main memory, but less command won’t load the entire file but loads it part by part, which makes it faster.

Syntax: less filename

Press “q” to exit the screen.

30.wc

wc stands for word count. As the name implies, it is mainly used for counting purposes.

  • It is used to find out the number of lines, word count, byte and characters count in the files specified in the file arguments.
  • By default, it displays the four-columnar output.
  • The first column shows the number of lines present in a file specified, the second column shows the number of words present in the file, the third column shows the number of characters present in the file and the fourth column itself is the file name which is given as argument.

Syntax: wc -option filename

Options:

-l: This option prints the number of lines present in a file.

-w: This option prints the number of words present in a file.

-c: This option displays the count of bytes present in a file.

-m: Using -m option ‘wc’ command displays the count of characters from a file.

-L: it can be used to print out the length of the longest (number of characters) line in a file.

31.vi

  • The vi editor is the most popular and commonly used text editor in all Linux Distributions.
  • It works in two modes, Command and Insert
  • Command mode takes the user commands, and the Insert mode is for editing text

Syntax:

  • vi filename: Creates a new file if it already does not exist, otherwise opens the existing file.
  • vi -R filename: Opens an existing file in read-only mode.

32.pico

Pico (available on modern Linux systems as nano) is a simple, display-oriented text editor. Commands and their Control key shortcuts are displayed at the bottom of the screen. As characters are typed they are immediately inserted into the text.

Syntax: pico filename

33.nano

GNU nano is an easy to use command line text editor for Unix and Linux operating systems. It includes all the basic functionality you’d expect from a regular text editor, like syntax highlighting, multiple buffers, search and replace with regular expression support, spell checking, UTF-8 encoding, and more.

To install nano editor: sudo apt install nano

Syntax: nano filename

34.zip, unzip

ZIP is a compression and file packaging utility in Linux. Each file is stored in a single file with the extension .zip.

Syntax : zip [options] zipfile files_list

Syntax: unzip filename.zip

35.w

w command in Linux is used to show who is logged on and what they are doing. This command shows the information about the users currently on the machine and their processes.

36.free

The free command displays the total amount of free space available along with the amount of memory used and swap memory in the system, and also the buffers used by the kernel.

37.env

env allows you to set or print the environment variables. During troubleshooting, you may find it useful for checking if the wrong environment variable prevents your application from starting.

38.uptime

It is used to find out how long the system is active. This command returns a set of values that involve the current time, and the amount of time system is in running state, the number of users currently logged in to, and the load time for the past 1, 5 and 15 minutes respectively.

39.top

top displays and updates sorted process information. Use this monitoring tool to determine which processes are running and how much memory and CPU they consume.

40.ps

The ps command, part of the procps-ng package which provides useful commands for investigating process IDs, shows the status of a running process. Use this command to determine a running application or confirm an expected process.

41.netstat

Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc.,

42.ip address

ip command in Linux is present in the net-tools which is used for performing several network administration tasks. IP stands for Internet Protocol. This command is used to show or manipulate routing, devices, and tunnels. It is similar to ifconfig command but it is much more powerful with more functions and facilities attached to it.

43.ping

PING (Packet Internet Groper) command is used to check the network connectivity between host and server/host. This command takes as input the IP address or the URL and sends a data packet to the specified address with the message “PING” and get a response from the server/host this time is recorded which is called latency. Fast ping low latency means a faster connection. Ping uses 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. Ping is generally measured in millisecond every modern operating system has this ping pre-installed.

44.traceroute

traceroute command in Linux prints the route that a packet takes to reach the host. This command is useful when you want to know about the route and about all the hops that a packet takes.

-I option is to use ICMP Echo for tracerouting instead of UDP as it might be blocked by some firewalls.

45.apt / apt-get

apt-get is a command-line tool which helps in handling packages in Linux. Its main task is to retrieve the information and packages from the authenticated sources for installation, upgrade and removal of packages along with their dependencies. Here APT stands for the Advanced Packaging Tool.

Syntax: apt-get install package-name

46.git-clone

git clone is primarily used to point to an existing repo and make a clone or copy of that repo in a new directory, at another location.

Syntax: git clone URL

47.wget/curl

Wget is the non-interactive network downloader which is used to download files from the server even when the user has not logged on to the system and it can work in the background without hindering the current process.

Syntax: wget URL

48.dpkg

dpkg is the software at the base of the package management system in the free operating system Debian and its numerous derivatives. dpkg is used to install, remove, and provide information about .deb packages. dpkg itself is a low-level tool.

Syntax: dpkg package-name

49.curl

curl is a command-line tool to transfer data to or from a server

Syntax: curl URL

50.youtube-dl

youtube-dl is a Python-based small command-line tool that allows downloading videos from YouTube, Dailymotion, Photobucket, Facebook, Yahoo, Metacafe, Depositfiles and few more similar sites.

Installation:

  1. sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
  2. sudo chmod a+rx /usr/local/bin/youtube-dl
  3. youtube-dl -U

Syntax: youtube-dl <video_url>

Tips and Tricks

  • Try the TAB button to autofill what you are typing. For example, if you need to type Documents, begin to type a command (let’s go with cd Docu, then hit the TAB key) and the terminal will fill in the rest, showing you cd Documents.
  • Ctrl+C and Ctrl+Z are used to stop any command that is currently working. Ctrl+C will stop and terminate the command, while Ctrl+Z will simply pause the command.
  • Ctrl+A moves you to the beginning of the line while Ctrl+E moves you to the end.
  • You can run multiple commands in one single command by using the “;” to separate them. For example Command1; Command2; Command3.

--

--

Sai kumaresh
BeyondX
Editor for

It's not who you are underneath but whatI do that defines me