For those of you diving into Linux, whether as a beginner or a seasoned developer, understanding and mastering Linux commands can significantly enhance your experience. That’s why we’ve put together a comprehensive Linux/Unix Command Line Cheat Sheet, which serves as a solid foundation for both newcomers and experts.
Most Commonly Used Linux Commands Cheat Sheet
1. File and Directory Operations Commands
Linux provides powerful commands for managing files and directories.
Here are some of the most essential ones:
ls: Lists files and directories in the current directory: ls -l (Displays detailed information about files)
cd: Changes the current directory: cd /home/user/Documents
pwd: Prints the current working directory.
cp: Copies files or directories: cp file1.txt file2.txt (Copy file1.txt to file2.txt)
mv: Moves or renames files and directories: mv oldname.txt newname.txt
rm: Removes files or directories: rm file.txt (Remove a file)
mkdir: Creates a new directory: mkdir new_folder
rmdir: Removes an empty directory: rmdir old_folder
2. File Permission Commands
Linux has a robust permission system, and managing file permissions is essential for system security.
chmod: Changes file permissions: chmod 755 file.txt (Set read, write, and execute permissions for owner, and read and execute for others)
chown: Changes file owner and group: chown user:group file.txt
chgrp: Changes the group ownership of a file: chgrp groupname file.txt
3. File Compression and Archiving Commands
Linux provides powerful tools for compressing and archiving files.
tar: Compresses and extracts files in .tar, .tar.gz, or .tar.bz2 formats:
tar -cvf archive.tar directory/ (Create an archive)
tar -xvf archive.tar (Extract an archive)
gzip: Compresses files using the gzip algorithm: gzip file.txt (Compress a file)
gunzip: Decompresses files compressed with gzip: gunzip file.txt.gz
zip: Compresses files into a .zip archive: zip archive.zip file1.txt file2.txt
unzip: Extracts files from a .zip archive: unzip archive.zip
4. File Search and Find Commands
Linux has powerful search tools to locate files and directories.
find: Search for files and directories within a given location: find /path/to/search -name “filename”
locate: Quickly find files by name (requires updated database): locate filename
updatedb: Update the database used by locate: sudo updatedb
which: Locate a command or executable file in the system’s PATH: which python
grep: Search within files for specific patterns or text: grep “pattern” file.txt
5. Text Processing and Manipulation
Text manipulation is crucial for developers when dealing with configuration files, logs, or any other text-based data.
cat: Concatenate and display file contents: cat file.txt
less: View files page by page: less file.txt
head: View the first few lines of a file: head -n 10 file.txt_
tail: View the last few lines of a file: tail -f log.txt (Follow a log file in real-time)
grep: Search for a specific pattern in files: grep “def” script.py (Search for function definitions in Python script)
sed: Stream editor for modifying files or text: sed ‘s/old/new/g’ file.txt (Replace all occurrences of ‘old’ with ‘new’)
awk: Pattern scanning and processing language: awk ‘{print $1}’ file.txt (Print the first column of the file)
sort: Sort the contents of a file: sort file.txt
uniq: Filter out repeated lines in a file: uniq file.txt
cut: Remove sections from each line of a file: **cut -d ‘,’ -f 1 file.csv (Extract the first field of a CSV file)
wc: Count words, lines, characters, etc., in a file: wc -l file.txt (Count lines)
6. Process Management and Debugging Commands
Managing processes in Linux is crucial for system performance.
ps: Displays a snapshot of current processes: ps aux (Shows detailed process information)
top: Displays real-time system process information.
strace: Trace system calls and signals of a process: strace -p 1234 (Trace process with PID 1234)
kill: Terminates a process by its ID: kill 1234 (Kill process with PID 1234)
killall: Terminates all processes by name: killall process_name
htop: An enhanced version of top with an easier-to-read interface.
lsof: List open files by processes: lsof -i (List all open network connections)
time: Measure the execution time of commands: time python3 script.py (Measure how long the Python script takes to run)
7. Process Control Commands
These commands help manage running processes.
pstree: Show processes in a tree format.
bg: Resume a stopped job in the background: bg %1 (Resume job 1)
fg: Bring a job to the foreground: fg %1 (Bring job 1 to foreground)
jobs: List current jobs running in the background.
8. System Information Commands
To get detailed information about your system, use these commands.
uname: Displays system information: uname -a (Displays all system details)
df: Shows disk space usage: df -h (Displays human-readable disk space usage)
free: Displays memory usage: free -m (Shows memory usage in megabytes)
uptime: Shows how long the system has been running.
hostname: Displays or sets the system’s hostname: hostname (Displays the hostname)
vmstat: Report virtual memory statistics: vmstat 1 (Updates every second)
iostat: Display CPU and I/O statistics.
dmesg: Show boot and system messages, useful for debugging: dmesg | grep error
9. Networking Commands
Networking commands are essential for managing connections and troubleshooting network issues.
ping: Sends ICMP echo requests to a remote host to test connectivity: ping google.com
ifconfig: Displays or configures network interfaces (older, replaced by ip command in some distributions): ifconfig (Displays network interface information)
ip: A newer alternative to ifconfig for network management: ip addr show (Displays IP address of the system)
netstat: Displays network connections, routing tables, interface statistics: netstat -tuln (Displays listening ports)
ss: Another utility for examining network sockets: ss -tuln (Shows TCP/UDP listening ports)
curl: Transfer data from or to a server, often used for APIs: curl -X GET https://api.github.com
wget: Download files from the web: wget http://example.com/file.zip
scp: Securely copy files between systems: scp file.txt user@remote:/path/to/destination/
10. I/O Redirection Commands
Linux allows input and output redirection for streamlining commands.
> : Redirects output to a file: echo “Hello” > file.txt
>> : Appends output to a file: echo “World” >> file.txt
< : Redirects input from a file: sort < file.txt
| : Pipes the output of one command into another: ps aux | grep “nginx”
11. Disk Management Commands
These commands are great for managing and monitoring disk space and partitions.
fdisk: View and manage disk partitions: fdisk -l (List disk partitions)
lsblk: List all block devices.
du: Estimate file and directory space usage: du -sh /path/to/directory/ (Shows total size of the directory)
mount: Mount a disk or partition: mount /dev/sdb1 /mnt
umount: Unmount a disk or partition: umount /mnt
df: Show disk space usage: df -h (Displays in human-readable format)
12. Environment Variable Commands
Environment variables control the environment in which processes run.
echo $VAR_NAME: Displays the value of an environment variable: echo $HOME (Displays the home directory)
export VAR_NAME=value: Sets an environment variable: export PATH=$PATH:/new/directory
13. Backup and Synchronization Commands
These commands help with creating backups or syncing files.
rsync: Synchronize files and directories between two locations: rsync -av /source/directory /destination/
tar: Archive and compress files: tar -cvf archive.tar directory/
cpio: Copy files into or out of archives: find . | cpio -o > archive.cpio
dd: Copy and convert files (often used for creating disk images): dd if=/dev/sda of=/path/to/backup.img
14. User Management Commands
These commands are essential for creating and managing users.
useradd: Adds a new user to the system: useradd username
passwd: Changes a user’s password: passwd username
usermod: Modifies an existing user account: usermod -aG groupname username
userdel: Deletes a user from the system: userdel username
- Shortcuts Commands List** Linux supports several keyboard shortcuts that can make navigation faster.
Bash Shortcuts:
Ctrl + C: Terminate the current command.
Ctrl + D: Exit the terminal.
Ctrl + L: Clear the terminal screen.
Ctrl + A: Move cursor to the beginning of the line.
Ctrl + E: Move cursor to the end of the line.
Nano Shortcuts:
Ctrl + O: Save the file.
Ctrl + X: Exit Nano.
Ctrl + W: Search within the file.
Ctrl + K: Cut text.
Vim Shortcuts:
i : Enter insert mode.
Esc : Exit insert mode.
:w : Save the file.
:q : Quit the editor.
:wq : Save and quit.
Follow me on Medium to learn more tips: https://medium.com/@CodingAdventure
Top comments (0)