Common Linux Commands
Mastering the command line is a key skill for anyone working with Linux, whether you're managing servers, developing software, or orchestrating containers. Linux commands offer powerful ways to interact with the system, automate tasks, and manage resources efficiently. This guide covers some of the most commonly used Linux commands that form the foundation of daily tasks in a Linux environment.
Basic Navigation and File Management Commands
ls
- List Directory Contents
The ls
command lists the files and directories in the current directory. It's often the first command you'll use when navigating the filesystem. Learn more about the Linux File System.
- Options:
-l
: Displays a detailed list, including file permissions, ownership, size, and modification date.-a
: Shows all files, including hidden ones (those starting with a dot.
).-h
: Makes file sizes human-readable (e.g., KB, MB).
cd
- Change Directory
The cd
command changes the current working directory, essential for navigating the Linux file system.
- Shortcuts:
cd ~
: Goes to your home directory.cd ..
: Moves up one directory level.cd -
: Switches to the previous directory.
pwd
- Print Working Directory
The pwd
command shows the full path of the current working directory, helping you confirm your location in the directory structure.
cp
- Copy Files and Directories
The cp
command copies files or directories from one location to another.
- Options:
-r
: Recursively copies directories and their contents.-i
: Prompts before overwriting existing files.
mv
- Move or Rename Files and Directories
The mv
command moves or renames files and directories.
- Use cases:
- Renaming a file:
mv old_name new_name
- Moving a file to a different directory:
mv file_name /path/to/directory
- Renaming a file:
rm
- Remove Files or Directories
The rm
command deletes files or directories.
- Options:
-r
: Recursively removes directories and their contents.-i
: Prompts before each removal, adding a layer of safety.
Caution: rm
is irreversible, so use it carefully, especially with the -r
option.
mkdir
- Create Directories
The mkdir
command creates new directories.
- Options:
-p
: Creates parent directories as needed (e.g.,mkdir -p /parent/child/grandchild
).
rmdir
- Remove Empty Directories
The rmdir
command removes empty directories.
For removing directories with contents, use rm -r
.
File Viewing and Editing Commands
cat
- Concatenate and Display File Contents
The cat
command displays the contents of a file.
- Options:
-n
: Numbers all output lines.
less
and more
- View File Contents Page by Page
less
and more
allow you to view large files one page at a time.
- Navigation:
Space
: Move to the next page.q
: Quit the viewer.
head
and tail
- View the Beginning or End of a File
head
shows the first few lines of a file, while tail
shows the last few lines.
- Options:
-n X
: Displays the first/lastX
lines (e.g.,head -n 10 file_name
).
nano
and vim
- Text Editors
nano
and vim
are command-line text editors. nano
is more beginner-friendly, while vim
offers powerful features for advanced users.
Learn more about Shell Scripting Best Practices to enhance your editing and scripting skills in Linux.
System Monitoring and Management Commands
ps
- Display Running Processes
The ps
command displays currently running processes.
- Options:
aux
: Shows detailed information for all processes.
top
- Real-Time System Monitoring
top
provides a real-time view of system processes, including CPU and memory usage.
- Navigation:
q
: Quittop
.
htop
- Interactive Process Viewer
htop
is an enhanced version of top
with a more user-friendly interface (requires installation on some systems).
df
- Disk Space Usage
The df
command displays disk space usage for the file system.
- Options:
-h
: Displays sizes in human-readable format (e.g., GB, MB).
du
- Directory Space Usage
The du
command estimates file and directory space usage.
- Options:
-h
: Displays sizes in human-readable format.-s
: Provides a summary of the directory's total size.
free
- Display Memory Usage
The free
command shows the amount of free and used memory in the system.
- Options:
-h
: Displays sizes in human-readable format.
File Permissions and Ownership Commands
chmod
- Change File Permissions
The chmod
command changes the permissions of a file or directory.
- Common Permission Settings:
755
: Read and execute access for everyone, write access for the owner.644
: Read access for everyone, write access for the owner.
Learn more about Linux File Permissions to better understand how chmod
works.
chown
- Change File Ownership
The chown
command changes the owner and group of a file or directory.
Networking Commands
ping
- Test Network Connectivity
The ping
command tests the network connection to a host.
- Options:
-c X
: SendsX
number of packets (e.g.,ping -c 4 example.com
).
ifconfig
- Configure Network Interfaces
The ifconfig
command configures or displays network interfaces.
- Alternative:
ip a
: Modern command to display network interfaces.
Learn more about Linux Networking Commands for advanced networking tasks.
curl
- Transfer Data from or to a Server
The curl
command transfers data to or from a server, using various protocols like HTTP, FTP, and more.
- Options:
-O
: Saves the output to a file (e.g.,curl -O http://example.com/file.txt
).
wget
- Download Files from the Web
wget
is a command-line utility to download files from the web.