Linux Command Line Cheatsheet
Quick Reference Guide for Command Line Operations
2 π Viewing File Content
cat file.txtβ Print entire content
head file.txtβ Print first 10 lines
tail file.txtβ Print last 10 lines
3 π Editing Files
nano file.txtβ Open file in Nano editor- Ctrl+O to save, Ctrl+X to exit, Ctrl+K to delete a line
- Ctrl+O to save, Ctrl+X to exit, Ctrl+K to delete a line
vi file.txtβ Open file in Vi editor- Press
ito insert
- Press
Esc, then type:wqand hitEnterto save and quit
- Type
:q!and hitEnterto quit without saving
- Press
4 π Creating and Modifying Files
touch file.txtβ Create an empty file or update timestamp if the file already existecho "text" > file.txtβ Write text to file (overwrite)
echo "text" >> file.txtβ Append text to file
cat file1.txt file2.txt > file3.txtβ Combine files
5 π File Movement, Copying, and Deletion
mv oldname.txt newname.txtβ Rename a file
mv file.txt folder/β Move file to folder
cp file.txt copy.txtβ Copy file
cp -r folder1 folder2β Copy folder recursively
rm file.txtβ Delete a file
rm -r folder/β Recursively delete folder
rm -f file.txtβ Force delete file (no warning)
rm -rf folder/β Forcefully delete folder and contents
6 π Directory Management
mkdir new_folderβ Create a new folder
mkdir -p parent/childβ Create nested folders including parent folders if parent folders do not exist yetrmdir empty_folderβ Remove empty folder
7 π Permissions & Ownership
chmod 400 key.pemβ Change file permission- first digit = privillage of owner of the file; second digit = other people in the same group; third digit = everyone else in the world
- 4 = read, 2 = write, 1 = execute
- 7 = 4 + 2 + 1 = read & write & execute
- Examples:
400= read-only by owner
600= read/write by owner
756= full access for owner, read & execute for others in the same group, read & write for everyone else in the world
chown user:group folder/β Change ownership of the foldersudoβ Run command with superuser privileges, βsuper user doβ
8 π Wildcards & Patterns
*β Wildcard: matches anything- Example:
mv fish* folder/
- Example:
?β Matches a single unknown character
9 π Print and Search
echo "text"β Print text to terminal
grep pattern file.txtβ Search for pattern-iβ Ignore case
-vβ Display lines with no match-nβ precede each matching line with line number-cβ Print only total count of matched lines
10 π System & File Info
historyβ Show command history
df -hβ Show disk space in human-readable format
clearβ Clear the screen
wc file.txtβ Count number of words in this file-lβ Count number of lines in this file
sort file.txtβ Sort lines alphabetically
11 π Compression
zip file.zip file.txtβ Compress file
unzip file.zipβ Decompress file
12 π Process Management
Ctrl+Cβ Stop running command
exitβ exit the SSH connection to a server, or exit a mode of being a userCOMMAND FILE1 | COMMANDβ Connect output of commands- for instance,
cat FILE1 | sort
- for instance,