FA

Essential Linux Commands

Comprehensive Practical Guide for Beginners

May 25, 2023  |  20 min read

Linux commands are the primary tools for interacting with the operating system. By learning these commands, you can manage files, configure the system, gather information, and perform everyday tasks quickly. This article is designed for people who are new to Linux and want to learn the most important commands in a simple way.

Prerequisite: Basic familiarity with the terminal environment is sufficient. If you've ever opened a terminal, this article is a great starting point for you.

1. Navigation Commands

These commands help you navigate through the Linux directory structure and see where you are.

pwd – Display Current Path

The pwd (Print Working Directory) command shows the full path of the current directory:

pwd
# Output: /home/username

ls – List Directory Contents

With ls, you can view the list of files and folders in a directory. Common options:

  • ls -l : Show detailed information (permissions, owner, size, date).
  • ls -a : Show all files including hidden ones that start with a dot.
  • ls -lh : Show file sizes in human-readable format (e.g., KB, MB).
ls -l
# Output: drwxr-xr-x 2 user user 4096 May 25 10:00 Documents

cd – Change Directory

Use cd (Change Directory) to move between folders:

cd /home/user/Documents   # Go to the Documents folder
cd ..                     # Go one level up
cd ~                      # Go to the user's home directory
cd /                      # Go to the system root

2. File and Directory Management

These commands are used for creating, copying, moving, and deleting files and directories.

mkdir – Create a New Directory

mkdir my_folder           # Create a folder
mkdir -p parent/child     # Create a folder with nested subfolders

touch – Create an Empty File

touch file.txt            # Create an empty text file

cp – Copy Files and Directories

cp file.txt backup.txt    # Copy a file
cp -r folder1 folder2     # Copy an entire folder (with -r option)

mv – Move or Rename

mv file.txt /tmp/         # Move file to the tmp folder
mv old_name.txt new_name.txt  # Rename a file

rm – Remove Files and Directories

Warning: The rm command permanently deletes files and they cannot be recovered. Use with caution.
rm file.txt               # Delete a file
rm -r folder/             # Delete a folder and all its contents
rm -f file.txt            # Force delete without confirmation

rmdir – Remove Empty Directory

rmdir empty_folder        # Only deletes empty directories

3. Viewing File Contents

Use these commands to see the contents of text files.

cat – Display Full File

cat file.txt              # Shows the entire content

less – Page-by-Page Display

For large files, less displays content page by page. Use Space to go forward and b to go backward.

less long_file.log

head and tail – Show Beginning or End of File

head -n 20 file.txt       # Show first 20 lines
tail -n 10 file.txt       # Show last 10 lines
tail -f logfile.log       # Follow live changes to a file (useful for logs)

4. Permission Management

In Linux, every file and directory has permissions for read (r), write (w), and execute (x).

chmod – Change Permissions

You can change permissions using numbers (e.g., 755) or letters (e.g., u+x):

chmod 755 script.sh       # Owner: read, write, execute | Group and others: read, execute
chmod u+x script.sh       # Add execute permission for the owner

chown – Change Owner

chown user:group file.txt # Change file owner and group

5. System Information

These commands provide useful information about the system and its resources.

  • uname -a : Display system kernel information.
  • df -h : Show used and available disk space (human-readable).
  • du -sh folder/ : Show total size of a folder.
  • free -h : Show RAM and Swap memory usage.
  • ps aux : Display list of running processes.
  • top or htop : Show system resources in real-time.
df -h
# Output: Filesystem      Size  Used Avail Use% Mounted on
#         /dev/sda1       100G   30G   70G  30% /

6. Search and Filter

grep – Search Text in Files

grep is one of the most powerful tools for searching text patterns:

grep "error" logfile.log          # Find lines containing "error"
grep -r "TODO" src/                # Recursively search in all files in the src folder
grep -i "warning" file.txt         # Case-insensitive search

find – Search for Files and Directories

find /home -name "*.txt"           # Find all .txt files in the home folder
find /var/log -size +10M           # Find files larger than 10 MB

7. Networking Commands

  • ping google.com : Check connectivity to a server.
  • curl https://api.example.com : Send HTTP request and receive response.
  • wget https://example.com/file.zip : Download a file from the internet.
  • ssh user@server.com : Securely connect to a remote server.
ping -c 4 8.8.8.8
# Send 4 packets to Google's DNS server and check response

8. Compression and Archiving

tar – Create and Extract Archives

tar -czf archive.tar.gz folder/    # Compress a folder using gzip format
tar -xzf archive.tar.gz            # Extract a compressed file

gzip and gunzip

gzip file.txt               # Compress a file (converts to file.txt.gz)
gunzip file.txt.gz          # Decompress a file

9. Process Management

  • ps aux | grep firefox : Find processes related to Firefox.
  • kill -9 PID : Forcefully terminate a process by its PID.
  • killall firefox : Close all Firefox processes.
  • bg : Resume a stopped process in the background.
  • fg : Bring a background process to the foreground.
# Run a command in the background
long_running_task &
# View background processes
jobs

10. Getting Help

man – Display Manual Pages

man ls                     # Show the complete manual for the ls command

--help – Quick Help

Most commands provide a brief description with the --help option:

ls --help

Conclusion and Next Steps

In this article, you learned the most important Linux commands. You can now perform everyday tasks in the terminal environment. To progress further:

  • Practice a few new commands every day and learn about their options.
  • Use man and --help to learn about new commands.
  • Read «The Linux Command Line» (available at Linux Library) for a deeper dive.
  • Work in the terminal often so that the commands become second nature.
Remember: Working with the terminal requires practice. Each time you use a command, you get one step closer to mastery.