Linux - keshavbaweja-git/guides GitHub Wiki
# Force df output to one line per file system mapping
df -Ph
# Linux Distribution Information
lsb_release -a
# Linux Kernel Version
uname -a
# Linux CPU build
uname -m
# Delete directories recursively under current folder modified more than 1 day ago
find . -type d -ctime +1 -exec rm -rf {} \;
# Delete files older than 5 days
find /path/to/files -mtime +5 -exec rm {} \;
# Search and list sizes
find . -name *.json -exec du -sh {} \;
# Search and list cumulative size
find . -type f -name *.json -exec du -ch {} + | grep total$
# Search for a pattern in all files under current directory recursively
grep -r <pattern> *
# Find oldest file under a folder
ls -lth <folder> | tail -1
# Find files which do not contain a particular text
grep -v <text>
# Find the directory where current executing script is hosted
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# List contents of a zip file
less <zipfile>
unzip -l <zipfile>
# Zip a folder recursively
zip -r <archive_name> <path_to_folder>
# Untar a tar.gz archive
tar -xvzf <archive_name.tar.gz>
# Create a tarball
tar -cvzf <archive_name.tar.gz> <folder>
# Untar a tar.gz archive
tar -xvzf <archive_name.tar.gz>
# Untar a tar.gz archive to a folder
tar -xvzf <archive_name.tar.gz> -C <path-to-folder>
# List processes of a users sorted by start time
ps -u <uid> -o pid,ppid,lstart,cmd --sort=cmd
# List pid based on filter condition
ps -ef | grep <searchText> | grep -v grep | awk '{print $2}'
# Find pid listening on a port
netstat -tulpn | grep ":<port>"
# Run a process in background and redirect outputs
./scriptName > fileName 2>&1 &
# Transfer file from local machine to remote Unix box
pscp <local_file_path> <user_name>@<server>:<remote_file_path>
# Copy a folder recursively
cp -R <path_to_source> <path_to_destination/>
# Replace text in place
sed -i.bak 's/<match-text>/<replacement-text>/' <path-to-file>
# Delete last line in file
sed -i '$ d' <file>`
# Create alias
set aliasName='command'
# Create a symbolic link to a folder
ln -s <target> <link>
# Which shell I am using
echo $0
du -h --max-depth=1 | sort -hr
du -sh * | sort -hr | head -n10
du -a . | sort -n -r | head -n 20
https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
Use curly braces to set them off:
echo "${10}"
10 or more bash function arguments
# Produce per CPU utilization stats for all CPU cores with a frequency of 1 sec
mpstat -P ALL 1
# Produce per process utilization stats for all processes with a frequency of 1 sec
pidstat 1
# CPU Architecture
lscpu
#!/usr/bin/env bash
sudo yum update -y
sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd
mkdir -p /var/www/html
sudo chmod 777 /var/www/html
echo "<body>Hello from "$HOSTNAME"<body>" > /var/www/html/index.html
# List keys added to ssh-agent
ssh-add -l
# Add key to ssh-agent
ssh-add -K <path-to-private-key-pem>