Basic CLI and Linux Commands

Command Line Interface

The Command Line Interface (CLI) is a text-based interface for interacting with a computer's operating system or software. The CLI allows users to execute commands by typing text commands into a terminal or command prompt. CLI commands can perform a wide range of functions, from navigating file systems and manipulating files to installing software and managing system settings.

In this guide, we'll explore some fundamental commands that every Linux user should know.

1. pwd

The pwd command in Linux is used to display the path of the current working directory.

Example:

$ pwd

2. ls

The ls command is used to display the contents of a directory.

Example:

$ ls

There are various flags we can use with ls to get more information about the files and directories listed by the ls command.

  • ls -l : -l flag gives the file and directory information with permission string and the owner of the file.

  • ls -a : -a flag is used for listing all the hidden files and directories.

  • ls -lh : -lh flag gives us file and directory information just like the -l flag and it also shows the size of the files or directories.

3. cd

The cd command is used for changing the current working directory.

Example:

$ cd <directory_path>

There are many other ways of using cd command.

  • cd .. : It is used to move one folder back.

  • cd ~ : It is used to move directly to the Home directory.

  • cd / : It is used to move directly to the Root directory.

4. mkdir

The mkdir command is used for creating a new directory.

Example:

$ mkdir <directory_name>

5. touch

The touch command in Linux is used to create a new file.

$ touch <file_name>

6. cat

The cat command is used to show the contents of a file.

Example:

$ cat <file_name>

7. rm

The rm command is used for removing files or directories.

  • rm <file_name> : Used for deleting a file.

  • rmdir <folder_name> : Used for deleting an empty folder.

  • rm -r <folder_name> : Used for deleting a non-empty folder.

  • rm -rf <folder_name> : Used for forcefully deleting a folder.

Example:

$ rm file.txt

8. less

The less command is used to read the contents of a file in the terminal. It allows users to navigate through large files quickly and efficiently without loading the entire file into the memory.

To get out of the terminal window, press q.

Example:

$ less <file_name>

9. head

The head command in Linux is used for displaying the beginning part of a file.

Example:

$ head <file_name>

We can also specify the number of lines to show using the -n option followed by the desired number of lines.

Example:

$ head -n 10 <file_name>

10. tail

The tail command in Linux is used for displaying the last part of a file.

Example:

$ tail <file_name>

We can also specify the number of lines to show using the -n option followed by the desired number of lines.

Example:

$ tail -n 10 <file_name>

11. man

The man command in Linux is used to display the user manual of any command that we run on the terminal. It provides a detailed view of a command.

Example:

$ man ls

To move out of the information window, press q.

12. echo

The echo command in Linux is used to display a line of text or to input values of variables or expressions to the terminal or standard output.

Example:

$ echo "Hello World!"

Example:

$ my_variable = 10
$ echo $my_variable

13. pipe ( | )

In Linux, a pipe is represented by | character. It is a powerful feature that allows us to provide the output of one command as an input to the other command.

Example:

$ command1 | command2

14. grep

The grep command in Linux is used for searching text patterns within files or input streams.

$ grep <string> <file_name>

Example:

$ grep "line" data.txt

15. cp

The cp command in Linux is used for copying files and directories from one location to another.

$ cp <source_file> <destination_directory>

Example:

$ cp file.txt ./documents/

16. mv

The mv command in Linux is used for moving or renaming files and directories from one location to another.

$ mv <source_file> <destination_directory>

Example:

$ mv file.txt ./documents/

17. ps

The ps command in Linux is used to list all the information about currently running processes.

Example:

$ ps

18. kill

The kill command is used for terminating running processes.

Example:

$ kill <PID>

The kill -9 command is used for the forceful termination of a process.

Example:

$ kill -9 <PID>

Going through these essential Linux commands will empower you to navigate your system efficiently and perform various tasks from the command line. Experiment with these commands in your terminal to become more proficient and unlock the full potential of the command line interface.

Stay tuned for more blogs.