DEV Community

Cover image for Basic Linux Commands to Know: A Beginner's Guide
benny fmo
benny fmo

Posted on • Updated on

Basic Linux Commands to Know: A Beginner's Guide

Introduction

Linux is a powerful and flexible operating system used by developers, system administrators, cloud engineers, and IT professionals worldwide. Understanding basic Linux commands is essential for navigating the system, managing files, and performing administrative tasks. In this guide, we'll cover 15 essential Linux commands, explaining what each does and showing their outcomes.

For this exercise, I'll be using Windows Subsystem for Linux (WSL) running Ubuntu 22.04.3 LTS. If you're not familiar with WSL or need help setting it up, you can find a detailed guide here.

Basic Command Structure in Linux

Before diving into specific commands, it's important to understand the basic structure of a Linux command. A typical Linux command follows this format:

command [options] [arguments]
Enter fullscreen mode Exit fullscreen mode
  • command: This is the actual command you want to execute (e.g., ls, cd, cp).
  • options: These modify the behavior of the command (e.g., -l for ls to list in long format).
  • arguments: These specify the targets for the command, like files or directories.

For example, the command ls -l /home lists the contents of the /home directory in long format.


Using the man Command

Each

Linux

command comes with a manual page (man page) that provides detailed information on how to use the command, including available options and examples. To access the man page for any command, use:

man command_name
Enter fullscreen mode Exit fullscreen mode

For example, to explore the options available for the ls command, you would use:

man ls
Enter fullscreen mode Exit fullscreen mode

This will open a detailed manual where you can explore all the functionalities of the ls command.


Table of Contents

  1. pwd
  2. ls
  3. cd
  4. mkdir
  5. touch
  6. cp
  7. mv
  8. rm
  9. cd
  10. sudo
  11. cat
  12. grep
  13. echo
  14. find
  15. top

1. pwd (Print Working Directory)

The pwd command displays the current working directory you're in. It's useful to know your location in the filesystem.

Example:

pwd
Enter fullscreen mode Exit fullscreen mode

Output:

/home/username

screenshot of pwd command in use


2. ls (List Directory Contents)

The ls command lists the contents of a directory. It can display files, directories, and various details like permissions, ownership, and size.

Example:

ls -l
Enter fullscreen mode Exit fullscreen mode

Output:

drwxr-xr-x 2 username username 4096 Aug 13 15:32 folder_name
-rw-r--r-- 1 username username 220 Aug 13 15:32 file_name.txt

screenshot of ls command in use


3. cd (Change Directory)

The cd command is used to change your current directory. You can navigate to any directory using the absolute or relative path.

Example:

cd /home/username/Documents
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just changes the directory to /home/username/Documents.

screenshot of cd command in use


4. mkdir (Make Directory)

The mkdir command creates a new directory. You can specify the name of the directory you want to create.

Example:

mkdir new_folder_name
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just creates a directory named new_folder_name.

screenshot of mkdir command in use


5. touch (Create a File)

The touch command is used to create an empty file or update the timestamp of an existing file.

Example:

touch new_file_name
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just creates an empty file named new_file_name.

screenshot showing how to use the move command


6. cp (Copy Files/Directories)

The cp command copies files or directories from one location to another.

Example:

cp file1.txt /home/username/Documents/
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just copies file1.txt to the /home/username/Documents/ directory.

screenshot showing copy command in use


7. mv (Move/Rename Files)

The mv command moves or renames files or directories.

Example:

mv file1.txt new_file1.txt
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just renames file1.txt to new_file1.txt.

screenshot showing how the mv command works in linux


8. rm (Remove Files/Directories)

The rm command removes files or directories. Be cautious as this can permanently delete files.

Example:

rm unwantedfile.txt
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just deletes unwantedfile.txt.

screeenshot showing how to use rm command


9. chmod (Change File Permissions)

The chmod command changes the permissions of a file or directory. It’s useful for controlling who can read, write, or execute a file.

Example:

chmod 400 script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

No output, just changes the permissions of the file script.sh to read only. You can type ll or ls -l to verify the permissions as shown below

screenshot showing the use of the chmod command


10. sudo (Admin privileges)

The sudo command in Linux allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It stands for "Superuser Do" and is commonly used to perform tasks that require administrative or root privileges.

Example:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Output:

sudo apt update refreshes the list of available packages and versions. sudo is needed because this command updates system files that require administrative privileges without which permission will be denied as shown on the screenshot below.

screenshot showing the use of the sudo command


11. cat (Concatenate and Display File Content)

The cat command displays the content of a file. It can also concatenate multiple files and display them together.

Example:

cat file_name.txt
Enter fullscreen mode Exit fullscreen mode

Output:

Displays the contents of file_name.txt.

screenshot showing how to use the cat command on linux


12. grep (Search Text)

The grep command searches for a specific pattern of text within files.

Example:

grep "search_term" file2.txt
Enter fullscreen mode Exit fullscreen mode

Output:

Displays the lines containing the string search_term in file2.txt.

screenshot showing how to use the grep command on linux


13. echo (Display a Line of Text)

The echo command outputs the specified text or variables to the terminal.

Example:

echo "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Output:

Displays the text Hello, World! in the terminal.

screen shot showing the echo command in use


14. find (Search for Files and Directories)

The find command searches for files and directories within a specified directory.

Example:

find /home/username/Documents -name "*.txt"
Enter fullscreen mode Exit fullscreen mode

Output:

Lists all .txt files in /home/username/Documents and its subdirectories. A clear example is shown on the image below.


screenshot showing how to use the find command on the linux terminal


15. top (Display System Processes)

The top command provides a real-time view of running processes and system resource usage.

Example:

top
Enter fullscreen mode Exit fullscreen mode

Output:

Displays a dynamic view of system processes.

screenshot showing how to use the top command on linux


Conclusion

These basic Linux commands are essential tools for anyone working in a Linux environment. Mastering them will make navigating and managing your system more efficient. As you become more comfortable with these commands, you'll be able to perform more complex tasks with ease. Remember to use the man command to explore the full capabilities of each command.


Resources

1.How to write a high quality post on DEV

2.Youtube Linux Operating System - Crash Course for Beginners by freecodecamp

  1. Dev post formatting markdown cheat sheet on github

Top comments (1)

Collapse
 
nora_b profile image
Nora Bassey

Wow, Simplified and well put together. Great Job