A complete list of essential Linux shell commands every system administrator should know. Includes file and folder operations, search techniques, disk usage checks, editing tools, recursive tasks, and SQL queries.
1. List Files and Directories
ls -l
List files with details in current directory.
ls -a
List all files including hidden files.
ls -lh
List with human-readable file sizes.
ls -R
Recursively list all files in subdirectories.
2. Search for Folder by Name
find . -type d -name "folder_name"
Find a folder named folder_name
starting from current directory.
3. Find the Largest Directories in Current Folder
du -h --max-depth=1 | sort -hr | head -n 10
Displays top 10 largest directories in current working directory.
4. Find the Largest File in a Folder
find . -type f -exec du -h {} + | sort -hr | head -n 1
Find the largest file starting from current directory.
5. Copy File and Force Overwrite
cp -f source.txt destination.txt
Copy and overwrite destination.txt
without prompt.
6. Check Disk Usage by Folder
du -sh *
Displays size of each item (file/folder) in the current directory.
7. Count Number of Files in a Folder
find . -type f | wc -l
Counts all files in current directory and subdirectories.
8. Copy One File to All Directories
find . -type d -exec cp file.txt {}/ \;
Copies file.txt
to every folder inside current directory.
9. Create Folders from List in a Text File
while read line; do mkdir -p "$line"; done < folders.txt
Reads folder names from folders.txt
and creates them.
10. Search for String in Files Recursively
grep -rnw . -e "search_term"
Find all files under current directory that contain search_term
.
11. Edit Multiple Files (Using sed)
sed -i 's/oldstring/newstring/g' *.txt
Replace all instances of oldstring
with newstring
in all .txt files in current directory.
12. Delete File from Current and Subdirectories
find . -type f -name "file-to-delete.txt" -delete
Deletes all instances of file-to-delete.txt
from current directory and its subfolders.
13. Find All Files Modified in Last 24 Hours
find . -type f -mtime -1
Lists files modified in the last 24 hours.
14. Find Files Over 100MB
find . -type f -size +100M
Lists all files larger than 100 MB.
15. Recursively Change File Permission
chmod -R 755 /path/to/directory
Changes permission recursively in a folder.
16. SQL to Update a String in a Field
UPDATE table_name SET column_name = REPLACE(column_name, 'old_value', 'new_value') WHERE column_name LIKE '%old_value%';
This SQL command replaces old_value
with new_value
in a column for all matching records.
17. Tar and Zip a Directory
tar -czvf archive-name.tar.gz /path/to/folder
Creates a compressed archive of a folder.
18. Extract a .tar.gz File
tar -xzvf archive-name.tar.gz
Extracts the contents of the compressed archive.
19. Show Top 10 Memory Consuming Processes
ps aux --sort=-%mem | head -n 10
Displays processes using most memory.
20. Find Symbolic Links
find . -type l
Lists all symbolic links in current directory and subfolders.
21. Monitor Live File Write
tail -f /path/to/logfile.log
Continuously monitor appended logs in real time.
22. Search and Replace Across Multiple Files Recursively
grep -rl 'oldtext' . | xargs sed -i 's/oldtext/newtext/g'
Recursively find and replace string across all files.
23. Find Empty Files and Folders
find . -empty
Lists all empty files and folders.
24. Delete All Empty Directories
find . -type d -empty -delete
Deletes all empty directories.
25. Set Environment Variable Temporarily
export VAR_NAME=value
Sets an environment variable for current session.
Conclusion
This comprehensive Linux command reference is essential for system administrators and DevOps engineers. Bookmark or integrate this guide into your KB system for quick daily access.