Linux - keshavbaweja-git/guides GitHub Wiki

System Information

# 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

Search

# 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 )"

zip, tar

# 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>

Process

# 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 &

Copy

# 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/>

Misc

# 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

List sizes of folders

du -h --max-depth=1 | sort -hr
du -sh * | sort -hr | head -n10
du -a . | sort -n -r | head -n 20

Parse command line arguments in bash

https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash

Handle 10 or more arguments in bash function

Use curly braces to set them off: echo "${10}" 10 or more bash function arguments

Monitor system performance

# 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

setuid, setgid, sticky bit

setuid, setgid, sticky bit

Parse yaml in bash

Parse yaml file in bash

Amazon Linux 2 - install httpd

#!/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

ssh agent

# List keys added to ssh-agent
ssh-add -l

# Add key to ssh-agent
ssh-add -K <path-to-private-key-pem>
⚠️ **GitHub.com Fallback** ⚠️