You’re here to learn Linux commands. Linux commands are instructions given to the Linux operating system through the command line interface (CLI) to perform various tasks.
The command-line interface (CLI) is where you get direct, unfiltered access to the core of the system. It’s faster, more powerful, and once you get the hang of it, more efficient for many tasks.
This guide is a breakdown of the Linux commands you need to know. We’ll cover everything from navigating the file system to managing processes and networks.
Free Linux Course for Beginners
Comprehend Linux basics with this beginner's free Linux tutorial. Get familiar with Linux operating system and understand its history, benefits, architecture, components, and basic commands.
A command has three parts: the command itself, options (also called flags or switches), and arguments.
- Command: The program you want to run (e.g., ls, cd).
- Options: Modify the command’s behavior (e.g.,
-l
,-a
). They usually start with a hyphen. - Arguments: The target of the command, like a file or directory path.
Let’s get started.
File System Navigation Linux Commands
Before you can do anything, you need to know how to move. The Linux file system is a hierarchical structure, starting from the root directory, represented by a single forward slash (/
). Everything on your system is located under this root. Unlike Windows with its C: or D: drives, Linux has one unified tree.
1. pwd
(Print Working Directory)
Lost? Type pwd. It tells you exactly where you are in the file system by printing the full, or “absolute,” path of your current directory. An absolute path starts from the root (/
) and shows every directory in the path to your current location.
Example:
pwd
Output:
/home/username/documents
2. ls
(List)
This is one of the most frequently used commands. ls lists the files and directories in your current location.
Common ls
Options:
ls -l
: Lists files in a long format, showing permissions, owner, size, and modification date.ls -a
: Shows all files, including hidden files (those starting with a.
).ls -al
: Combines the two to show detailed information for all files.ls -R
: Lists the contents of sub-directories as well.
Example:
ls -al
3. cd
(Change Directory)
Use cd to move between directories. You can use absolute paths or relative paths. A relative path is based on your current location.
Key cd
Shortcuts:
cd ..
: Moves one directory up. The..
represents the parent directory.cd ~
or justcd
: Takes you straight to your home directory.cd -
: Switches you back to the previous directory you were in.
Examples:
- Absolute Path:
cd /var/log
(moves to the log directory, no matter where you currently are). - Relative Path: If you are in
/home/username
and want to go to/home/username/documents
, you can just typecd documents
.
File and Directory Management Linux Commands
Now that you can navigate, you need to manage files and directories.
4. mkdir
(Make Directory)
Creates a new directory.
Example:
mkdir new_folder
Tip: Use the -p
option to create parent directories if they don’t exist: mkdir -p project/assets/images
will create project
and assets
if needed.
5. touch
(Create File)
The touch command creates a new, empty file. If the file already exists, it updates its timestamp.
Example:
touch my_script.py
6. cp
(Copy)
Copies files or directories.
Syntax: cp [destination]
Examples:
- Copy a file:
cp old_file.txt new_file.txt
- Copy a file to another directory:
cp my_file.txt /home/username/documents/
- Copy a directory: You need the
-r
(recursive) option to copy a directory and its contents.
cp -r /source/directory /destination/
7. mv
(Move)
Moves or renames files and directories. In Linux, renaming a file is the same as moving it to the same location with a new name.
Syntax: mv [destination]
Examples:
- Rename a file:
mv old_name.txt new_name.txt
- Move a file:
mv my_file.txt /new/location/
- Move multiple files to a directory: If the last argument is a directory, mv will move all specified files into it.
mv file1.txt file2.txt /some/directory/
8. rm
(Remove)
Deletes files and directories. Use this command with caution, as deleted files are permanently removed.
Examples:
- Delete a file:
rm my_file.txt
- Delete a directory: You must use the
-r
(recursive) option to delete a directory and everything inside it.
rm -r old_directory
Force deletion: The -f
(force) option will remove files without prompting. Combining -r
and -f
as rm -rf
is powerful and can be destructive if you’re not careful.
9. find
Searches for files and directories based on various criteria.
Examples:
- Find a file by name in the current directory and subdirectories:
find . -name "my_file.txt"
- Find all .js files larger than 1MB modified in the last 7 days:
find /path/to/search -type f -name "*.js" -size +1M -mtime -7
Viewing and Editing Files
10. cat
(Concatenate)
The cat command reads a file and prints its contents to the standard output (your terminal). It’s good for quickly viewing small files.
Example:
cat /etc/hosts
You can also use cat to create a new file or join multiple files.
11. less
For larger files, less is better than cat because it displays the content one page at a time. You can scroll up and down. Press q
to exit.
Example:
less /var/log/syslog
12. head
and tail
head
: Shows the first 10 lines of a file by default.tail
: Shows the last 10 lines of a file by default.
Tip: tail -f
is extremely useful for watching log files in real-time. It continuously displays new lines as they are added to the file.
Users and Permissions Commands: Controlling Access
Linux is a multi-user system, and permissions are crucial for security.
13. Understanding Permissions
When you run ls -l
, you see a string like -rwxr-xr--
.
- The first character indicates the file type (
-
for a file,d
for a directory). - The next nine characters are three sets of permissions for the user (owner), the group, and others.
- Each set has three permission types:
r
(read): View the contents.w
(write): Modify the contents.x
(execute): Run the file (for scripts) or enter the directory.
14. chmod
(Change Mode)
Changes the permissions of a file or directory. You can use symbolic notation (u, g, o, a and +, -, =) or numeric notation.
Numeric Notation:
r
= 4w
= 2x
= 1
You add the numbers for the permissions you want. For example, rwx
is 4+2+1=7, r-x
is 4+0+1=5, and r--
is 4+0+0=4.
Examples:
- Make a script executable for the owner:
chmod u+x my_script.sh
- Set permissions to rwxr-xr– (user can read/write/execute, group can read/execute, others can only read):
chmod 754 my_file.txt
15. chown
(Change Owner)
Changes the owner and group of a file or directory. You typically need sudo to run this.
Syntax: sudo chown [user]:[group] [file]
Example:
sudo chown newuser:newgroup my_file.txt
16. sudo
(Superuser Do)
Executes a command with root (administrator) privileges. Many administrative tasks require sudo
.
Example:
sudo apt-get update
Process Management: Keeping an Eye on Your System
A process is a running instance of a program.
17. ps
(Process Status)
Shows a snapshot of the current processes.
Common ps
Options:
ps aux
: Displays all running processes from all users in a detailed, user-friendly format.
18. top
Provides a real-time, interactive view of the running processes. It shows CPU and memory usage and updates continuously. Press q
to exit.
19. kill
and pkill
Terminates a process.
kill
: Requires the Process ID (PID) of the process you want to stop. You can find the PID usingps
ortop
.
kill 12345
If a process is stuck, you may need to use kill -9 12345
to force it to terminate.
pkill
: Kills a process based on its name.
pkill firefox
Networking Linux Commands
20. ping
Sends a small packet to a host to check for a response. It’s the standard way to test if a machine is reachable over the network.
Example:
ping google.com
Use Ctrl+C
to stop it.
21. ip
The modern tool for displaying and configuring network interfaces.
ip addr
orip a
: Shows IP addresses assigned to your network interfaces.ip route
: Displays the routing table.
22. netstat
and ss
netstat
: Shows network connections, routing tables, and interface statistics.netstat -tuln
will display listening TCP and UDP ports.ss
: The modern replacement fornetstat
.ss -tuln
does the same as thenetstat
command above but is generally faster.
23. ssh
(Secure Shell)
Connects securely to a remote machine.
Syntax: ssh [username]@[hostname_or_ip]
Example:
ssh user@192.168.1.100
Useful Shortcuts and Tricks
- Tab Completion: Start typing a command, file, or directory name and press
Tab
. The shell will attempt to autocomplete it for you. This saves a lot of typing and avoids typos. - History: Press the up and down arrow keys to cycle through your previously used commands. You can also type
history
to see a list of recent commands. - Ctrl+R: Performs a reverse search through your command history. Start typing a part of a command you remember, and it will show you the most recent match.
man
(Manual): Displays the manual page for a command. This is your best friend for learning about command options and usage.
man ls
clear
: Clears your terminal screen. The keyboard shortcut is oftenCtrl+L
.
Also Read: