Linux Command Line Essentials: Mastering the Basics

What is a Linux Command?

A Linux command is a piece of a software program or a utility, that is executed at the user space using a CLI (Command Line Interface). The Linux commands will simply be executed in a Terminal, However the terminal application may vary depending on the Linux distribution you’re using. If you’re still unsure using the CLI program, we suggests to read this “CLI – Introduction”

Here are some of the basic and most fundamental Linux commands:

1. sudo command:

Short for SuperUser do, sudo is the most powerful and is a basic linux command that lets users to perform tasks that require administrative or root permissions. Sudo enables the programs to execute with highest privileges that are required to perform administrative tasks.

Below is the general syntax for executing sudo:

$ sudo <command/program_to_execute>

2. ls command:

The ls commands lists out the files and directories that are present in the current working directory, the ls command would simply put-out the files available, without much information such as file types, permissions, and modified date & time stamp.

$ ls [flags]

Below are some of the suggests flags that are most commonly preferred to work, while using ls command:

  • ls -a : Lists all hidden files along with the visible files.
  • ls -r : Lists the files but in the descending order.
  • ls -R : Lists all the files, directories along with the files within the sub-directories.
  • ls -l : Lists the files, and directories along with its permissions.

3. pwd command:

pwd – stands for Present Working Directory, when executed it returns the full path of your current working directory.

The $PWD is an environmental variable that stores the path of the exact current directory

$ pwd [option]

pwd command accepts two flags, which are suggested as below:

  • pwd -P : Where -P is referred to as Physical, which prints the actual path of the directory.
  • pwd -L : Where -L is referred to as Logical, which prints the Logical path of the directory.

4. cat command:

cat – or concatenate is a multi-purpose command in linux systems, It can be used to perform multiple operations such: create a file, display the contents of the file.

$ cat file_name

Note: The format of the file_name has to be considered while performing cat operation.

To view multiple files, simply add multiple files to the cat command.

$ cat file_name1 file_name2 file_name3

5. cp command:

cp – stands for copy, This command is used to copy files or directories from one location to another location. cp command expects to arguments where the first argument is that of a source file and the second argument is the destination file path to where it needs to be copied.

$ cp src_file dest_file 

Note: The destination directory path should exist when performing the copy, cp command won’t create a directory path but can create a file if it don’t exist.

Additionally, you can use the below flags with cp command for performing various ways of copy.

  • -r or -R: perform copy operation in recursive way for coping all the directory along with its sub-files.
  • -i : for interactively copying the files, and allows users to authorise if the user needs to replace the file or no during the copy.
  • -b : back-up the file in destination file in the same destination file with a different filename.
  • -f : initiates force copy even when there is no write permission for the user to perform for the file copy.

6. rm command:

rm – stands for remove, rm commands used to perform removal or deletion of a file in a filesystem. User should be careful when trying to perform rm operation, since the command works in the silent mode and often time the files removed cannot be recovered.

$ rm file_name

To perform deletion of multiple files you can simply phrase multiple file names for the rm command.

$ rm file_name1 file_name2 file_name3

rmdir – stands for removing empty directory from the filesystem. multiple files or subfiles can be removed by phrasing ‘-r’ argument for recursively removing/deleting files and subfiles in the directory.

$ rmdir -r dir_name

Note: checking the ‘rmdir –help’ usage flags to list down all the available options for rmdir command.

7. grep command:

grep – global regular expression print – is the most useful command for searching strings, keywords, or a specific pattern in a file, or from a command output.

$ grep "search_string" file_name

grep has several usage options that can be used to allow users to search strings.

$ grep [OPTION] file_name

below are some of the most commonly used options:

  • -i : ignore case – ignores the exact same cases and outputs all the matching subset of string patterns.
  • -n : line number – Outputs the exact line number of each string that found with appropriate input string pattern.
  • -v : invert match – Output the non-matching lines of the input string pattern.
  • -w : word match – Outputs the exact match of the word for the input string pattern.
  • -c : count match – provides the total number of occurrences of the input string pattern.

8. mv command:

mv – move command – as the name suggests this command, generally performs two operations:

  • rename: Performs renaming a file
  • move: Performs move operation of a file from one location to other within a filesystem.
$ mv src_filename dest_filename

In the above command, the mv performs rename operation where the user needs to choose the source file for which the file had to be renamed, and provide the new file name as the destination file name argument.

$ mv src_filename dest_/_file_/_path

In the above command, the mv performs moving source file from one location of filesystem to a different file location for the destination path provided as the secondary argument.

9. chmod command:

chmod – Change Mode – Every file or directories in Linux has a set of permissions where the user can control the user permissions for who has to read (r), write (w), and execute (x) the file. These permission form together a set of permissions for specific user groups. The chmod command lets users to modify or provide certain permission so that the user can grant or restrict access to directories and files.

$ chmod [mode] [file_name]

chmod mode:

  • 4 : Read Permission
  • 2 : Write Permission
  • 1 : Execute Permission
$ chmod 764 [file_name]

Here:

  • 7 – refers to permission for Owner (rwx)
  • 6 – refers to permission for Group (rw)
  • 4 – refers to permission for Others (r)

Please find the detailed documentation of chmod.

10. kill command:

kill – is a command used to send signal to a process, but by default the kill send a termination signal which requests the process to exit manually, unless if the user decides to specify a certain kill signal for the process.

Options:

  • SIGUP – 1 – It send a hangup signal for the ongoing controlling process or kill of controlling process.
  • SIGINT – 2 – sends a interrupt signal to a process from a keyboard.
  • SIGKILL – 9 – sends a kill signal to a process.
  • SIGTERM – 15 – sends a terminal signal to a process.
  • SIGSTOP – 19 – sends a signal to stop the process temporarily. which does enables to resume the process later.

Note: Several other signal names can be look up with the below command:

$ kill -l 

Example: kill command syntax.

$ kill {-s singal} PID

Example: Kill command usage.

$ kill -9 2124

We’ll provide a detailed article of usage of kill command on linux. It’s a very useful tutorials and we recommend you to check it out.

Share
OpenLib .

OpenLib .

The Founder - OpenLib.io

You may also like...