Introduction
Working in Linux without knowing basic commands is like reading a book without knowing the alphabet. The terminal (command line) is the primary tool for administration, configuration, and everyday tasks in Linux. By mastering a few dozen key commands, you'll be able to navigate the file system quickly, manage files and processes, and get system information. This guide is your starting point: we've collected the most important commands with explanations and examples that will be useful for anyone starting with Linux.
Requirements
Before you begin, make sure you have:
- Access to a Linux terminal (locally, via a virtual machine, or remotely through SSH).
- Basic understanding of the file system structure (what directories and files are).
- Permissions to perform operations (for some commands, like
rmorsudo, administrator rights may be required).
Step 1: Navigating the File System
The first thing to learn is understanding where you are and moving between directories.
pwd β Where am I?
The pwd (print working directory) command shows the current full path to the directory.
pwd
# Example output: /home/username
cd β Change Directory
cd (change directory) switches you to the specified directory.
cd /var/log # navigate to /var/log
cd ~ # go to the home directory
cd .. # move up one level
cd - # return to the previous directory
ls β List Contents
ls (list) outputs a list of files and subdirectories in the current directory.
ls # simple list
ls -l # detailed list (permissions, size, date)
ls -a # show hidden files (starting with .)
ls -lh # sizes in a readable format (KB, MB)
Step 2: Managing Files and Directories
Now let's learn how to create, copy, move, and delete file system objects.
Creation
touch filenameβ create an empty file.mkdir dirnameβ create a new directory.mkdir -p path/to/dirβ create nested directories (if intermediates don't exist).
touch newfile.txt
mkdir documents
mkdir -p projects/2024/plan
Copying and Moving
cp source destinationβ copy a file or directory.mv source destinationβ move or rename a file/directory.
cp file.txt backup/
cp -r dir1 dir2 # recursive directory copy
mv old.txt new.txt
mv file.txt /home/user/Documents/
Deletion
rm fileβ delete a file.rm -r dirβ delete a directory and all its contents.rm -f fileβ force deletion without prompts.
β οΈ Caution! Files deleted via
rmusually don't go to the trash. Make sure you specify the correct path.
rm oldfile.txt
rm -r old_dir/
::in-article-ad
::
Step 3: Viewing and Editing Files
Sometimes you need to quickly view a file's contents or make edits.
Outputting Contents
cat fileβ output the entire file to the terminal (suitable for small files).less fileβ page-by-page viewing (scroll with arrows, exit withq).head fileβ first 10 lines of a file.tail fileβ last 10 lines (useful for logs).
cat /etc/os-release
less /var/log/syslog
tail -f /var/log/auth.log # monitor changes in real time
Editing
nano fileβ simple text editor with prompts (ideal for beginners).vim fileβ powerful modal editor (requires learning but very efficient).
nano myconfig.conf
# In nano: Ctrl+O β save, Ctrl+X β exit
Step 4: Managing Processes
Processes are running programs. Knowing how to manage them is critical for system stability.
Viewing Processes
psβ snapshot of processes (usually current user).ps auxβ all system processes (columns: USER, PID, %CPU, %MEM, COMMAND).toporhtopβ interactive monitoring (real-time updates, sorting, termination).
ps aux | grep nginx # find nginx processes
top # to exit β q
Terminating Processes
kill PIDβ gently terminate a process by its ID (PID).kill -9 PIDβ force terminate (SIGKILL) if the process is unresponsive.
kill 1234 # graceful termination
kill -9 1234 # forceful
pkill firefox # terminate all processes named firefox
Step 5: Searching and Filtering
When you have many files, search by name or content.
Finding Files
find /path -name "filename"β search for files by name (supports wildcards*).find /path -type fβ only files (-type dβ directories).find /path -size +1Mβ files larger than 1 MB.
find /home -name "*.txt"
find /var/log -type f -mtime -7 # modified in the last 7 days
Searching Text Inside Files
grep "pattern" fileβ find lines containing the pattern.grep -r "pattern" /dirβ recursive search in a directory.grep -i "pattern"β case-insensitive.
grep "error" /var/log/syslog
grep -r "TODO" ~/projects/
Step 6: Getting Help
Don't memorize everything β Linux has excellent built-in documentation.
man β Manual Pages
man command shows the full manual page with description, options, and examples.
man ls
# Navigation: arrows, PageUp/Down, /text to search, q to exit
--help or -h
Quick help directly in the terminal.
ls --help
grep -h
whatis β Short Description
whatis ls
# Output: ls (1) - list directory contents
Checking Your Results
After completing this guide, you should be able to confidently perform the following actions:
- Open a terminal and run
pwdβ you'll see the current path. - Navigate to another directory with
cdand verify withls. - Create a file
touch test.txtand a directorymkdir test_dir. - Copy a file
cp test.txt backup/and move itmv test.txt new.txt. - Output a file's contents
cat /etc/os-releaseand find a processps aux | grep bash. - Get help
man cdand find a commandwhatis grep.
If all commands work without errors β you've mastered the basics!
Possible Issues
"Permission denied" Error
Cause: Insufficient permissions for the operation (e.g., writing to a system directory).
Solution: Use sudo before the command (if you have administrator rights) or change to a directory where you have permissions (e.g., ~/).
sudo rm /etc/important.conf # requires administrator password
"No such file or directory"
Cause: An incorrect path or filename was specified.
Solution: Check the current directory (pwd) and file list (ls). Use absolute paths (/home/user/file) instead of relative ones.
"Command not found"
Cause: The command is not installed or there's a typo in the name.
Solution: Check the spelling. Install the package via the package manager (e.g., sudo apt install package for Ubuntu/Debian).
Infinite Output in cat for Large Files
Cause: You're outputting a huge file (e.g., a binary or a multi-hundred MB log).
Solution: Use less or limit the output (head, tail).
Unable to Terminate a Process
Cause: The process is ignoring the gentle SIGTERM signal.
Solution: Use kill -9 PID (SIGKILL), but only if other methods fail β this is an emergency termination.