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]
-
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
forls
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
For example, to explore the options available for the ls
command, you would use:
man ls
This will open a detailed manual where you can explore all the functionalities of the ls
command.
Table of Contents
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
Output:
/home/username
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
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
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
Output:
No output, just changes the directory to
/home/username/Documents
.
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
Output:
No output, just creates a directory named
new_folder_name
.
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
Output:
No output, just creates an empty file named
new_file_name
.
6. cp
(Copy Files/Directories)
The cp
command copies files or directories from one location to another.
Example:
cp file1.txt /home/username/Documents/
Output:
No output, just copies
file1.txt
to the/home/username/Documents/
directory.
7. mv
(Move/Rename Files)
The mv
command moves or renames files or directories.
Example:
mv file1.txt new_file1.txt
Output:
No output, just renames
file1.txt
tonew_file1.txt
.
8. rm
(Remove Files/Directories)
The rm
command removes files or directories. Be cautious as this can permanently delete files.
Example:
rm unwantedfile.txt
Output:
No output, just deletes
unwantedfile.txt
.
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
Output:
No output, just changes the permissions of the file
script.sh
to read only. You can typell
orls -l
to verify the permissions as shown below
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
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.
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
Output:
Displays the contents of
file_name.txt
.
12. grep
(Search Text)
The grep
command searches for a specific pattern of text within files.
Example:
grep "search_term" file2.txt
Output:
Displays the lines containing the string
search_term
infile2.txt
.
13. echo
(Display a Line of Text)
The echo
command outputs the specified text or variables to the terminal.
Example:
echo "Hello, World!"
Output:
Displays the text
Hello, World!
in the terminal.
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"
Output:
Lists all
.txt
files in/home/username/Documents
and its subdirectories. A clear example is shown on the image below.
15. top
(Display System Processes)
The top
command provides a real-time view of running processes and system resource usage.
Example:
top
Output:
Displays a dynamic view of system processes.
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
Top comments (1)
Wow, Simplified and well put together. Great Job