Basic Commands
-
Show current directory
pwd
-
List files in a directory
ls ls -l # long format ls -a # include hidden files
-
Change directory
cd <directory>
-
Create a new directory
mkdir <directory>
-
Remove a directory
rmdir <directory>
-
Create an empty file or update the access and modification times of a file
touch <filename>
-
Remove a file
rm <filename> rm -r <directory> # remove directory and its contents
Viewing and Editing Files
-
Display the contents of a file
cat <filename>
-
Display the first few lines of a file
head <filename>
-
Display the last few lines of a file
tail <filename>
-
Open a file for editing in nano
nano <filename>
-
Open a file for editing in vim
vim <filename>
File and Directory Operations
-
Copy files and directories
cp <source> <destination> cp -r <source> <destination> # copy directories recursively
-
Move/rename files and directories
mv <source> <destination>
Permissions and Ownership
-
Change file permissions
chmod <permissions> <filename> chmod 755 <filename> # common permission set
-
Change file owner and group
chown <owner>:<group> <filename>
-
View file permissions
ls -l <filename>
System Information
-
Show current date and time
date
-
Show system uptime
uptime
-
Show who is logged in
who
-
Show current user
whoami
-
Display disk usage of a directory
du -sh <directory>
-
Display free and used memory
free -h
-
Show system information
uname -a
Networking
-
Display network interfaces and IP addresses
ifconfig
-
Check connectivity to a host
ping <hostname>
-
Display routing table
route -n
-
Display open network connections
netstat
Process Management
-
List running processes
ps aux
-
Display process tree
pstree
-
Terminate a process by PID
kill <pid>
-
Terminate a process by name
killall <process-name>
-
Show process usage and system load
top
Searching and Filtering
-
Search for a pattern in files
grep <pattern> <file>
-
Find files and directories
find <directory> -name <pattern>
-
Search for commands in the command history
history | grep <pattern>
-
Display command history
history
Archiving and Compression
-
Create a tar archive
tar -cvf <archive.tar> <files>
-
Extract a tar archive
tar -xvf <archive.tar>
-
Create a compressed archive (gzip)
tar -czvf <archive.tar.gz> <files>
-
Extract a compressed archive (gzip)
tar -xzvf <archive.tar.gz>
-
Compress a file with gzip
gzip <file>
-
Decompress a file with gzip
gunzip <file.gz>
-
Compress a file with bzip2
bzip2 <file>
-
Decompress a file with bzip2
bunzip2 <file.bz2>
Disk Usage and Management
-
Check disk space usage
df -h
-
Check inode usage
df -i
-
Check space usage of files and directories
du -sh <directory>
Environment Variables
-
Set an environment variable
export VAR_NAME=value
-
Show environment variables
printenv
-
Unset an environment variable
unset VAR_NAME
Miscellaneous
-
Echo text to the terminal
echo "Hello, World!"
-
Redirect output to a file
command > <file>
-
Append output to a file
command >> <file>
-
Chain multiple commands
command1 && command2 command1 || command2