Mid Prep Linux
Mid Prep Linux
Filesystem Commands
File Editing
File Operations
Bash Configuration
vim ~/.bashrc : Edit user's bash configuration file using vim editor
vim /etc/skel/.bashrc : Edit default bash configuration file using vim editor
Command Alias
Permissions
Resource Usage
Viewing Logs
Buffer Management
File Navigation
gg : Go to beginning of file
Shift+G : Go to end of file
Split Windows
Line Numbers
VIM Configuration
cut
awk
find
find /path -name "*.txt" : Find all .txt files in the specified path
find . -name name : Search by name in the current directory
find . -iname nAme : Case-insensitive name search
find . -name name -type f : Search for files by name (use -type d for directory)
find . -name name -type f -exec rm {} + : Find and remove matching files
find /path -name "*.log" -type f -exec truncate -s 0 {} + : Truncate all log files to size 0
find . -mmin -10 : Find files modified less than 10 minutes ago
find . -mtime -10 : Find files modified less than 10 days ago
find . -size +5M : Find files larger than 5 MB (can use-/+ and G:GB, M: MG, k: KB)
find . -empty : Find empty files and directories
find . -perm 777 : Find files with specific permissions
find /path -exec chown user1:grp {} + : Change owner and group for all found files
find . -name name -type f -maxdepth 1 : Search only in the current directory (not recursively)
Basics
Variables
Environment Variables
Usually in uppercase
env : Display all environment variables
Basic Math
If Statements
if [ -f /path/to/file ]
then
echo "The file exists"
else
echo "The file does not exist"
fi
Exit Codes
While Loops
while [ condition ]
do
# commands
sleep 0.5
done
sleep 0.5 (It waits 0.5 sec before the next execution of the loop)
#!/bin/bash
release_file=/etc/os-release
if grep -q "Arch" $release_file
then
# Arch-based system
sudo pacman -Syu
elif grep -q "Ubuntu" $release_file || grep -q "Debian" $release_file
then
# Ubuntu/Debian-based system
sudo apt update
sudo apt dist-upgrade
fi
For Loops
for item in 1 2 3 4 5
do
echo $item
done
# Alternate syntax
for item in {1..5}
do
echo $item
done
Store scripts in /usr/local/bin for system-wide access (you'll be able to execute scripts from everywhere)
Make root the owner of system-wide scripts
Add custom directories to PATH: export PATH=/path/to/scripts:$PATH
Remember to make your scripts executable with chmod +x script_name.sh before running them.