Conserver la configuration d'iptables lors des dรฉmarrages
# Sauvegarde de la configuration d'iptables dans un fichier
iptables-save > /etc/iptables.conf
# Crรฉation d'un fichier sh dans le dossier ...
echo "#!/bin/sh" > /etc/network/if-up.d/iptables
# ... qui restaurera les rรจgles sauvegardรฉ dans le fichier /etc/iptables.conf
echo "iptables-restore < /etc/iptables.conf" >> /etc/network/if-up.d/iptables
# Rendre executable le script bash
chmod +x /etc/network/if-up.d/iptables
Il est nรฉcรฉssaire de relancer ce script ร chaque modifications d'iptables
Gestion des services
(must be executable: chmod +x /etc/init.d/blah)
#! /bin/sh
# /etc/init.d/blah
## BEGIN INIT INFO
# Provides: blah
# Required-Start: $local_fs $syslog $remote_fs dbus
# Required-Stop: $local_fs $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start blah
## END INIT INFO
# Some things that run always
touch /var/lock/blah
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting script blah"
echo "Could do more here"
;;
stop)
echo "Stopping script blah"
echo "Could do more here"
;;
*)
echo "Usage: /etc/init.d/blah {start|stop}"
exit 1
;;
esac
exit 0
# Set deadline for user password
chage -E 2005-12-31 user1
# Create a new group
groupadd [group]
# Delete a group
groupdel [group]
# Rename a group from moon to sun
groupmod -n moon sun
# Check correct syntax and file format of `/etc/group` and groups existence
grpck
# Log into a new group to change default group of newly created files
newgrp - [group]
# Change password
passwd
# Change a user password (only by root)
passwd user1
# Check correct syntax and file format of `/etc/passwd` and users existence
pwck
# Create a new user `user1` belongs `admin` group
useradd -c "User Linux" -g admin -d /home/user1 -s /bin/bash user1
# Create a new user
useradd user1
# Delete a user (`-r` eliminates home directory)
userdel -r user1
# Change user attributes as description, group and other
usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1
# Show user rights on files
ls -l
# or, but show folders "." and ".."
ls -all
# User informations
id USERNAME
# Show last logs of users
lastlog
# Show last logged users
last
# Show current user
who -a
# Command history
~/.bash_history
~/.history
# for root user
/root/.bash_history
# Be sure no user can hide his command history (a = append only, i = immutable)
chattr +a /home/user/.bash_history
chattr +i /home/user/.profile
# See also
# http://www.akyl.net/securing-bashhistory-file-make-sure-your-linux-system-users-won%E2%80%99t-hide-or-delete-their-bashhistory
# http://zero202.free.fr/bash/html/ar01s01.html#id336074
# https://en.wikipedia.org/wiki/Chattr
chattr
chwon
chmod
Network
Note: ifconfig, route, mii-tool, nslookup commands are obsolete
# Show status of ethernet interface eth0
ethtool eth0
#Manually set ethernet interface speed
ethtool --change eth0 autoneg off speed 100 duplex full
# Show link status of `eth0`
mii-tool eth0
# Show configuration of an ethernet network card
ifconfig eth0
# Configure IP Address
ifconfig eth0 192.168.1.1 netmask 255.255.255.0
# Configure `eth0` in promiscuous mode to gather packets (sniffing)
ifconfig eth0 promisc
# Show status of wireless interface eth1
iwconfig eth1
# Manually set wireless interface speed
iwconfig eth1 rate 1Mb/s fixed
# List wireless networks in range
iwlist scan
# Disable an interface `eth0`
ifdown eth0
# Activate an interface `eth0`
ifup eth0
# List network interfaces and their status
ip link show
# Rename interface eth0 to wan
ip link set dev eth0 name wan
# Bring interface eth0 up (or down)
ip link set dev eth0 up
# List addresses for interfaces
ip addr show
# Add (or del) ip and mask (255.255.255.0)
ip addr add 1.2.3.4/24 brd + dev eth0
# List routing table
ip route show
# Set default gateway to 1.2.3.254
ip route add default via 1.2.3.254
# Show routing table
route -n
# Configure default gateway
route add -net 0/0 gw IP_Gateway
# Configure static route to reach network `192.168.0.0/16`
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
# Remove static route
route del 0/0 gw IP_gateway
# Lookup DNS ip address for name or vice versa
host example.com
# Machine name
hostname
# Lookup local ip address (equivalent to host `hostname`)
hostname -i
# Active interface `eth0` in dhcp mode
dhclient eth0
# List network services on a system and their PID
netstat -tupl
# List active network connections to/from system and their PID
netstat -tup
# Show routing table alike "route -n"
netstat -rn
# Show all HTTP traffic
tcpdump tcp port 80
# DNS record
dig example.com
# Lookup whois info for hostname or ip address
whois example.com
# Activate ip routing temporarily
echo "1" > /proc/sys/net/ipv4/ip_forward
# Reverse DNS
ping -a 8.8.8.8
nslookup -type=ptr 8.8.8.8
nslookup www.example.com
# Test if remote port is open
nc -vz example.com 80
nmap -p 80 example.com
telnet example.com 80
timeout 1 bash -c "</dev/tcp/example.com/80" && echo Port open || echo Port closed
Note: samba is the package that provides all this windows specific networking support
# Find windows machines. See also findsmb
smbtree
# Find the windows (netbios) name associated with ip address
nmblookup -A 1.2.3.4
# List shares on windows machine or samba server
smbclient -L windows_box
# Show remote shares of a windows host
smbclient -L ip_addr/hostname
# Mount a windows share
mount -t smbfs -o fmask=666,guest //windows_box/share /mnt/share
# Send popup to windows machine (off by default in XP sp2)
echo 'message' | smbclient -M windows_box
# Mount a windows network share
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share
# Netbios name resolution
nbtscan ip_addr
# Netbios name resolution
nmblookup -A ip_addr
# Like wget can download files from a host windows via smb
smbget -Rr smb://ip_addr/share
HTTP
# Get headers
curl -s -D - https://example.com -o /dev/null
# Raw body
curl -s --ignore-content-length --raw https://example.com
# Get HTTP response
echo -en "GET /feed/ HTTP/1.1\r\nHost:example.com\r\nUser-Agent:Firefox\r\n\r\n" | nc infoheap.com 80
# Get the header Content-Type
echo -en "GET /feed/ HTTP/1.1\r\nHost:example.com\r\nUser-Agent:Firefox\r\n\r\n" | nc localhost 80 | head -n 30 | grep -a Content-Type
# Search if test is in first 1000 bytes (include header)
echo -en "GET /feed/ HTTP/1.1\r\nHost:example.com\r\nUser-Agent:Firefox\r\n\r\n" | nc localhost 80 | head -c1K | grep -a test
# Get body length
echo -e "GET / HTTP/1.1\r\nHost: example.com\r\nAccept-Encoding: gzip, deflate\r\n\r\n" | nc localhost 80 | sed ':a;N;$!ba;s/^.*\r\n\r\n//g' | wc -c
# Get body uncompressed length (should be gzipped)
echo -e "GET / HTTP/1.1\r\nHost: example.com\r\nAccept-Encoding: gzip, deflate\r\n\r\n" | nc 93.184.216.34 80 | sed ':a;N;$!ba;s/^.*\r\n\r\n//g' | gzip -d | wc -c
# Simple HTTP server
echo -e "GET / HTTP/1.1\r\nHost:example.com\r\nUser-Agent:Firefox\r\n\r\n" | nc example.com 80 > response.http
while true; do nc -l 8000 < response.http; done
# Create `.htpasswd` file
htpasswd -c /path/.htpasswd USERNAME
# Store local browsable version of a page to the current dir
(cd dir/ && wget -nd -pHEKk http://www.pixelbeat.org/cmdline.html)
# Continue downloading a partially downloaded file (with the ability to stop the download and resume later)
wget -c http://www.example.com/large.file
# Download a set of files to the current directory
wget -r -nd -np -l1 -A '*.jpg' http://www.example.com/dir/
# FTP supports globbing directly
wget ftp://remote/file[1-9].iso/
# Process output directly
wget -q -O- http://www.pixelbeat.org/timeline.html | grep 'a href' | head
# Download url at 1AM to current dir
echo 'wget url' | at 01:00
# Do a low priority download (limit to 20KB/s in this case)
wget --limit-rate=20k url
# Check links in a file
wget -nv --spider --force-html -i bookmarks.html
# Efficiently update a local copy of a site (handy from cron)
wget -m http://www.example.com/
wget -m -U "" http://example.com/path/dir
SSH Tunnel
# Redirect local port to a remote machine
ssh -NfL 8080:127.0.0.1:0808 user@host
# Copy content of a directory on remote directory via ssh
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p'
# Copy a local directory on remote directory via ssh
( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p'
Copy files with sftp
sftp -i /path/to/private/keyfile remote.host.tld
Copy files with lftp
/dst/dir.lftp:
set ftp:list-options -a
#set cmd:fail-exit true
# To use keyfile
# http://www.adminschoice.com/how-to-configure-ssh-without-password
# ssh-keygen -t rsa -f keyfile -N ""
#set sftp:connect-program "ssh -a -x -i <keyfile>"
open -u user,pass sftp://sftp.dc0.gpaas.net
mirror --verbose --delete --parallel=4 --only-newer --exclude ^snapshots/ --exclude ^vhosts/ --exclude ^lamp0/\.config --exclude ^lamp0/var/admin/gandi.cnf$ --exclude ^lamp0/var/admin/logrotate.conf$ --exclude ^lamp0/var/cron/admin/logrotate$ --exclude ^lamp0/var/cron/admin/phpsess$ --exclude ^lamp0/db/ --exclude ^lamp0/var/log/.+ --exclude ^lamp0/var/php/www/.+ "${SRC_DIR}" "${DST_DIR}"
exit
# For paths in variables used in sed, see https://stackoverflow.com/a/29613573/470117
SRC_DIR=/src/dir; DST_DIR=/dst/dir; sed -e "s#\${SRC_DIR}#$SRC_DIR#" -e "s#\${DST_DIR}#$DST_DIR#" /dst/dir.lftp | lftp -f /dev/stdin
# Quick math (Calculate ฯ). See also bc
echo '(1 + sqrt(5))/2' | bc -l
# Calculate ฯ the unix way
seq -f '4/%g' 1 2 99999 | paste -sd-+ | bc -l
# More complex (int) e.g. This shows max FastE packet rate
echo 'pad=20; min=64; (100*10^6)/((pad+min)*8)' | bc
# Python handles scientific notation
echo 'pad=20; min=64; print (100E6)/((pad+min)*8)' | python
# Plot FastE packet rate vs packet size
echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | gnuplot -persist
# Base conversion (decimal to hexadecimal)
echo 'obase=16; ibase=10; 64206' | bc
# Base conversion (hex to dec) ((shell arithmetic expansion))
echo $((0x2dec))
# Unit conversion (metric to imperial)
units -t '100m/9.58s' 'miles/hour'
# Unit conversion (SI to IEC prefixes)
units -t '500GB' 'GiB'
# Definition lookup
units -t '1 googol'
# Add a column of numbers. See also add and funcpy
seq 100 | (tr '\n' +; echo 0) | bc
Date and clock
# Display a calendar
cal -3
# Display a calendar for a particular month year
cal 9 1752
# System date
date
# Set system date and time `MonthDayhoursMinutesYear.Seconds`
date 041217002020.00
# save date changes on BIOS
clock -w
# What date is it this friday. See also day
date -d fri
# Exit a script unless it's the last day of the month
[ $(date -d '12:00 +1 day' +%d) = '01' ] || exit
# What day does xmas fall on, this year
date --date='25 Dec' +%A
# Convert seconds since the epoch (1970-01-01 UTC) to date
date --date='@2147483647'
# What time is it on west coast of US (use tzselect to find TZ)
TZ='America/Los_Angeles' date
# What's the local time for 9AM next Friday on west coast US
date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Locale
# Print number with thousands grouping appropriate to locale
printf "%'d\n" 1234
# Use locale thousands grouping in ls. See also l
BLOCK_SIZE=\'1 ls -l
# Extract info from locale database
echo "I live in $(locale territory)"
# Lookup locale info for specific country. See also ccodes
LANG=en_IE.utf8 locale int_prefix
# List fields available in locale database
locale -kc $(locale | sed -n 's/\(LC_.\{4,\}\)=.*/\1/p') | less
Packages
RPM Packages ( Fedora, Red Hat and like):
# install a rpm package
rpm -ivh [package.rpm]
# install a rpm package ignoring dependencies requests
rpm -ivh --nodeeps [package.rpm]
# upgrade a rpm package without changing configuration files
rpm -U [package.rpm]
# upgrade a rpm package only if it is already installed
rpm -F [package.rpm]
# remove a rpm package
rpm -e [package]
# show all rpm packages installed on the system
rpm -qa
# show all rpm packages with the name "httpd"
rpm -qa | grep httpd
# obtain information on a specific package installed
rpm -qi [package]
# show rpm packages of a group software
rpm -qg "System Environment/Daemons"
# show list of files provided by a rpm package installed
rpm -ql [package]
# show list of configuration files provided by a rpm package installed
rpm -qc [package]
# show list of dependencies required for a rpm packet
rpm -q [package] --whatrequires
# show capability provided by a rpm package
rpm -q [package] --whatprovides
# show scripts started during installation / removal
rpm -q [package] --scripts
# show history of revisions of a rpm package
rpm -q [package] --changelog
# verify which rpm package belongs to a given file
rpm -qf /etc/httpd/conf/httpd.conf
# show list of files provided by a rpm package not yet installed
rpm -qp [package.rpm] -l
# import public-key digital signature
rpm --import /media/cdrom/RPM-GPG-KEY
# verify the integrity of a rpm package
rpm --checksig [package.rpm]
# verify integrity of all rpm packages installed
rpm -qa gpg-pubkey
# check file size, permissions, type, owner, group, MD5 checksum and last modification
rpm -V [package]
# check all rpm packages installed on the system - use with caution
rpm -Va
# verify a rpm package not yet installed
rpm -Vp [package.rpm]
- `rpm -ivh /usr/src/redhat/RPMS/`arch`/[package.rpm]`: install a package built from a rpm source
# extract executable file from a rpm package
rpm2cpio [package.rpm] | cpio --extract --make-directories *bin*
# build a rpm package from a rpm source
rpmbuild --rebuild [package.src.rpm]
# List all packages by installed size (Bytes) on rpm distros
rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n
YUM packages tool (Fedora, RedHat and alike):
# download and install a rpm package
yum -y install [package]
# That will install an RPM, and try to resolve all the dependencies for you using your repositories.
yum localinstall [package.rpm]
# update all rpm packages installed on the system
yum -y update
# upgrade a rpm package
yum update [package]
# remove a rpm package
yum remove [package]
# list all packages installed on the system
yum list
# find a package on rpm repository
yum search [package]
# clean up rpm cache erasing downloaded packages
yum clean [package]
# remove all files headers that the system uses to resolve dependency
yum clean headers
# remove from the cache packages and headers files
yum clean all
DEB packages (Debian, Ubuntu and like):
# install / upgrade a deb package
dpkg -i [package.deb]
# remove a deb package from the system
dpkg -r [package]
# show all deb packages installed on the system
dpkg -l
# show all deb packages with the name "httpd"
dpkg -l | grep httpd
# obtain information on a specific package installed on system
dpkg -s [package]
# show list of files provided by a package installed on system
dpkg -L [package]
# show list of files provided by a package not yet installed
dpkg --contents [package.deb]
# verify which package belongs to a given file
dpkg -S /bin/ping
# List all packages by installed size (KBytes) on deb distros
dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | sort -k1,1n
APT packages tool (Debian, Ubuntu and alike):
# returns list of packages which corresponds string "searched-packages"
apt-cache search [package]
# install / upgrade a deb package from cdrom
apt-cdrom install [package]
# install / upgrade a deb package
apt-get install [package]
# update the package list
apt-get update
# upgrade all of the installed packages
apt-get upgrade
# remove a deb package from system
apt-get remove [package]
# verify correct resolution of dependencies
apt-get check
# clean up cache from packages downloaded
apt-get clean
Pacman packages tool (Arch, Frugalware and alike):
# Install package `name` with dependencies
pacman -S name
# Delete package `name` and all files of it
pacman -R name
Monitoring and debugging
fuser
lsof
ls -l /proc/[process id]/fd
ls -l /proc/*/fd
for p in [0-9]*; do ls -l /proc/$p/fd ;done
strace ls
someexe > /dev/null & sudo dtruss -f -t open -p $!
inotifywait -m -r -e OPEN /path/to/traced/directory
# Displays status of RAM in megabytes
free -m
# Force closure of the process and finish it
kill -9 process_id
# force a process to reload configuration
kill -1 process_id
# Show history reboot
last reboot
# Display kernel loaded
lsmod
# Display a list of files opened by processes
lsof -p process_id
# Displays a list of open files in a given path system
lsof /home/user1
# Displays linux tasks
ps -eafw
# Displays linux tasks in a hierarchical mode
ps -e -o pid,args --forest
# Shows a tree system processes
pstree
# Monitoring reliability of a hard-disk through SMART
smartctl -A /dev/hda
# Check if SMART is active on a hard-disk
smartctl -i /dev/hda
# Display system calls made and received by a process
strace -c ls >/dev/null
# Display library calls
strace -f -e open ls >/dev/null
# Show events inherent to the process of booting kernel
tail /var/log/dmesg
# Show system events
tail /var/log/messages
# Display linux tasks using most cpu
top
# Display interrupts in real-time
watch -n1 'cat /proc/interrupts'
# Show shared libraries required by ssh program
ldd /usr/bin/ssh
# See also
auditctl
inotify
# Get process details for process ID 3117
/proc/3117
# Monitor messages in a log file
tail -f /var/log/messages
# Summarise/profile system calls made by command
strace -c ls >/dev/null
# Show file opens
echo exit | strace bash -li |& grep '^open'
# List system calls made by command
strace -f -e open ls >/dev/null
# Monitor what's written to stdout and stderr
strace -f -e trace=write -e write=1,2 ls >/dev/null
# List library calls made by command
ltrace -f -e getenv ls >/dev/null
# List opened files and ports
# http://en.wikipedia.org/wiki/Lsof
lsof
# List paths that process id has open
lsof -p $$
# List processes that have specified path open
lsof ~
# Show network traffic except ssh. See also tcpdump_not_me
tcpdump not port 22
# List processes in a hierarchy
ps -e -o pid,args --forest
# List processes by % cpu usage
ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'
# List processes by mem (KB) usage. See also ps_mem.py
ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS
# List all threads for a particular process
ps -C firefox-bin -L -o pid,tid,pcpu,state
# List elapsed wall time for particular process IDs
ps -p 1,$$ -o etime=
# Show system reboot history
last reboot
# Show amount of (remaining) RAM (-m displays in MB)
free -m
# Watch changeable data continuously
watch -n.1 'cat /proc/interrupts'
# Monitor udev events to help configure rules
udevadm monitor
# Active processes
ps -A
# Active processes - Tree processes
/bin/ps acxfwwwe
# Active processes - Live display
top
# List active network connections (TCP) with corresponding processes
netstat -ape
System
System information
See also sysinfo.
# Show name and version of distribution
head -n1 /etc/issue
# Show kernel version and system architecture
uname -a
# show architecture of machine(2)
uname -m
# show used kernel version
uname -r
# show architecture of machine
arch
# show information CPU info
cat /proc/cpuinfo
# show interrupts
cat /proc/interrupts
# verify memory use
cat /proc/meminfo
# show file(s) swap
cat /proc/swaps
# show version of the kernel
cat /proc/version
# show network adpters and statistics
cat /proc/net/dev
# show mounted file system(s)
cat /proc/mounts
# Show RAM total seen by the system
grep MemTotal /proc/meminfo
# Show CPU(s) info
grep "model name" /proc/cpuinfo
# Show PCI info
lspci -tv
# Show USB info
lsusb -tv
# Show state of cells in laptop battery
grep -F capacity: /proc/acpi/battery/BAT0/info
# Display SMBIOS/DMI information
dmidecode -q | less
Power management
Aka shutdown, restart and logout of a system
# shutdown system(2)
init 0
# leaving session
logout
# reboot(2)
reboot
# shutdown system(1)
shutdown -h now
# planned shutdown of the system at 16:30
shutdown -h 16:30 &
# cancel a planned shutdown of the system
shutdown -c
# reboot
shutdown -r now
# shutdown system
telinit 0
Interactive
See also linux keyboard shortcuts
# Line editor used by bash, python, bc, gnuplot, ...
readline
# Virtual terminals with detach capability, ...
screen
# Visual file manager that can browse rpm, tar, ftp, ssh, ...
mc
# Interactive/scriptable graphing
gnuplot
# Web browser
links
# open a file or url with the registered desktop application
xdg-open .
Shell
Command-line interface (CLI), not for graphical user interface (GUI)
The default shell is defined for a specific user by the file /etc/passwd
# List available shells
cat /etc/shells
# Clear shell cache
hash -r
# Change shell
chsh
chsh --list-shells
In /etc/bashrc or ~/.bashrc
# If id command returns zero, youโve root access.
if [ $(id -u) -eq 0 ];
then you are root, set red colour prompt
PS1="\\[$(tput setaf 1)\\]\\u@\\h:\\w\\[$(tput sgr0)\\]"
else normal
PS1="[\\u@\\h:\\w] $"
fi
Profile files, defined by (in order, but also if the shell is interactive and/or a login):
(global system) /etc/profile
(global system) /etc/bashrc
(user) ~/.bash_profile
(user) ~/.bash_login
(user) ~/.profile
(user) ~/.bashrc (not used by some OS like macOS for login shell, it's recommended to if [ -s ~/.bashrc ]; then source ~/.bashrc; fi in ~/.profile for that case)
(user) ~/.bash_logout au logout
(and /etc/bash.bashrc, /etc/bash.bashrc.local?)
To reload a configuration file (here the file ~/.bashrc)
source ~/.bashrc
# or use dot command:
. ~/.bashrc
initialisation at the session level (.bash_profile) and initialisation at each shell level (.bashrc)
By convention, the prompt ends with $ for users and by # for root
# History auto complete with "start with" filter
"\e[5~": history-search-backward
"\e[6~": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on
#
# Dont forget to call ~/.bashrc in your ~/.profile script
#
export CLICOLOR=1
alias ll='ls -l'
alias la='ls -A'
alias vi='vim'
alias l='ls -CF'
function cyan_red_prompt {
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local RED="\[\033[0;31m\]"
PS1="${CYAN}[\u@\h ${RED}\w${CYAN}]${GRAY} "
}
cyan_red_prompt
# Relative to execution path:
#source /path/to/script.sh
# So use this instead:
source $(dirname $0)/script.sh
# Or:
CURRENT_DIR=`dirname $0`
$CURRENT_DIR/script.sh
Avoid using UPPERCASE variable names. That namespace is generally reserved by the shell for special purposes (like PATH), so using it for your own variables is a bad idea.
Escape special char in argument:
command $'\t'
Wildcard files:
for file in *.ext1; do echo "${file} ${file/.ext1/.ext2}"; done
echo "The current user (\$USER) \"$USER\" use the shell (\$SHELL) \"$SHELL\""
echo 'The current user ($USER) "'"$USER"'" use the shell ($SHELL) "'"$SHELL"'"'
Shell parameter expansion:
# Remove trailing slash for all arguments `a/ b/c/ d /` output `a b/c d`
echo ${@%/}
# myvar=a/b/// output `a/b`
shopt extglob
echo ${myvar%%+(/)}
somevar=$(echo "$value" | sed 's:/*$::')
echo $somebar
dir=/a/b///
echo $(realpath -s --canonicalize-missing $dir)
# Construct command parts as an array
mycommand=(/some/command "$PATH_NAME")
# Print the command
printf '%s\n' "${mycommand[*]}"
# Execute command
"${mycommand[@]}"
# Enter to directory `/home`
cd /home
# Go back one level
cd ..
# Go back two levels
cd ../..
# Go to $HOME directory
cd
# Go to home directory
cd ~user1
# Go to previous directory
cd -
# Get the current working directory
pwd
# Go to dir, execute command and return to current dir
(cd dir && command)
# Put current dir on stack so you can popd back to it
pushd .
Files and folders operations
File name:
Using sed or other external processes to do simple string operations like stripping extensions and prefixes is inefficient. Instead, use parameter expansions which are part of the shell (no external process means it will be faster). Some helpful articles on the subject are listed below:
# Modify timestamp of a file or directory - (YYMMDDhhmm)
touch -t 0712250000 file1
# Create directories `dir1` and `dir2` (in same parent dir)
mkdir dir1 dir2
# Try to create a directory tree
mkdir -p /tmp/dir1/subdir1
# Extract music from m3u (ignore lines start with #, whitespace or are empty)
# Playlists entries could be relative
cd "/path/to/playlists"
cat "playlist1.m3u" "playlist1.m3u" "playlist3.m3u" | grep "^[^# \t]" | tr -s '\n' | while read -r line; do cp --parent -v "$line" /destination; done
# Get extensions of all files
find . -type f -printf "%f\n" | grep -o -E '(\.[^\.]*)$' | sort | uniq
# File checksum SHA256, used in chef cookbooks
openssl dgst -sha256 path/to/myfile
# File checksumMD5
openssl dgst -md5 path/to/myfile
# Read or write BSD-style checksum file
# https://unix.stackexchange.com/a/476480/60668
sha256sum -c SHA256SUMS path/to/myfile
sha256sum --tag path/to/myfile
# Encrypt a file with GNU Privacy Guard
gpg -c file1
# Decrypt a file with GNU Privacy Guard
gpg file1.gpg
# Create hardlink aka hardware link
# Note: It's not possible to create hardlink with a directory (due to the risk of loops in tree)
ln /path/to/source_file /path/to/target_file
ln -T /etc/apache2/sites-available/example.com /etc/apache2/sites-enabled/example.com
# Remove file link
unlink /etc/apache2/sites-enabled/websiteA.exemple
# Create a symbolic link to file or directory
ln -s file1 lnk1
File listing and searching
Find support shell pattern for -path and -ipath parameters:
# Find all files that have been modified in the past 7 days
find . -type f -mtime -7
# Search binary files are not used in the last 100 days
find /usr/bin -type f -atime +100
# Find all JPEGs that have been modified more than 30 days ago
find . -name \*.jpg -mtime +30
# Move all JPEGs from the current folder (recursively) that are greater than 40k into the folder /tmp/2
find . -name \*.jpg -size +40k -exec mv {} /tmp/2 +
# search files and directories belonging to `user1`
find / -user user1
# List files by date. See also newest and find_mm_yyyy
ls -lrt
# Print in 9 columns to width of terminal
ls /usr/bin | pr -T9 -W$COLUMNS
# Search 'expr' in this dir and below. See also findrepo
find -name '*.[ch]' | xargs grep -E 'expr'
# Search all regular files for 'example' in this dir and below
find -type f -print0 | xargs -r0 grep -F 'example'
# Search all regular files for 'example' in this dir
find -maxdepth 1 -type f | xargs grep -F 'example'
# Process each item with multiple commands (in while loop)
find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done
# Get filename (without the extension)
find ./ -name "tile_*.png" -exec bash -c 'filename="$1";echo "${filename%.*}"' _ {} \;
# search files with `.bin` extension within directory `/home/user1`
find /home/user1 -name \*.bin
# Search files with `.rpm` extension and modify permits
find / -name *.rpm -exec chmod 755 '{}' \;
# Search files with `.rpm` extension ignoring removable partitions as cdrom, pen-drive, etc.โฆ
find / -xdev -name \*.rpm
# Find files with the `.ps` extension - first run `updatedb` command
locate \*.ps
#Search cached index for names. This re is like glob *file*.txt
locate -r 'file[^/]*\.txt'
# Find files not readable by all (useful for web site)
find -type f ! -perm -444
# Find dirs not accessible by all (useful for web site)
find -type d ! -perm -111
# Search for empty files and folder (or file with only whitespaces)
find -empty
# List all folders and files, ordered by size
du -ak . | sort -nr | less
# List top 10 largest files and directories
du -a /var | sort -n -r | head -n 10
# Quickly search (sorted) dictionary for prefix
look reference
# Append to each files
find -type f -exec sh -c 'echo "something" >> "$1"' _ {} \;
find -type f -print0 | while IFS= read -r -d '' file; do echo "something" >> "$file"; done
find -type f -exec bash -c 'for file; do echo "something" >> "$file"; done' _ {} \+
# See also https://serverfault.com/questions/343705/how-do-i-append-a-specific-number-of-null-bytes-to-a-file/
# Use a simple shell loop, to process each of the images.
mkdir thumbnails
for f in *.jpg
do convert $f -thumbnail 200x90 thumbnails/$f.gif
done
# Use find to substitute filenames into a 'convert' command.
# This also provides the ability to recurse though directories by removing
# the -prune option, as well as doing other file checks (like image type,
# or the disk space used by an image).
find * -prune -name '*.jpg' \
-exec convert '{}' -thumbnail 200x90 thumbnails/'{}'.gif \;
# Use xargs -- with a shell wrapper to put the argument into a variable
# This can be combined with either "find" or "ls" to list filenames.
ls *.jpg | xargs -n1 sh -c 'convert $0 -thumbnail 200x90 thumbnails/$0.gif'
# An alternative method on Linux (rather than plain Unix)
# This does not need a shell to handle the argument.
ls *.jpg | xargs -r -I FILE convert FILE -thumbnail 200x90 FILE_thumb.gif
# Find multiple exec a executed only if the previous one exit with 0 https://stackoverflow.com/questions/5119946/find-exec-with-multiple-commands#comment34296391_6043896
find -type f -iname "*.properties" -exec grep -q -i -E '^abtest=false$' {} \; -print
find -type f -iname "*.properties" -exec grep -q -i -E '^abtest=' {} \; -exec sed -i 's/^abtest=.*/abtest=true/i' {} \; -print
# Use test to inverse the grep exit value https://stackoverflow.com/a/30495279/470117 (-v/--invert-match option is not useful for that case), for that we need also sh interpreter to use builtin test command
# For append mode (redirection), need to use sh interpreter directly, else {} will be interpreted directly by Bash. See https://superuser.com/questions/1327969/appending-new-lines-to-multiple-files/1327980#1327980
# -exec sh -c 'echo "command name: $0, first arg: $1"' test {} \;
find -iname "*.properties" -exec sh -c 'grep -q -i -E "^abtest=" $1; test $? -eq 1' match {} \; -exec sh -c 'echo -e "\n\nabtest=true" >> $1' append {} \; -print
find -iname "*.properties" -type f -print0 | xargs -0 grep -EHi '^abtest='
# Same as (flexibity of find vs glob include/exclude filters):
grep -EHir --include="*.properties" '^abtest=' /path/to/dir
grep -ir --include=\*.{php,js,css} "/api/v1/"
# Example search in all .less files that contains `url("<url>")` or `url('<url>')` or `url(<url>)`
find "$wd" -iname "*.less" \( -not -ipath "*/node_modules/*" \) -type f -print0 | xargs -0 grep -EliZ 'url\(('"'"'|"|)(.*?)\1\)' | xargs -0 -n1 echo "Do something with that file:"
# Rename (move) a file or directory
mv dir1 new_dir
# Change file extension
for j in /path/dir/*.html
do
n=${j/.html}
mv "$j" "$n.php"
done
# Change file extension
for f in *; do mv "$f" "$f.bin"; done;
# Rename `Ref_00001.jpg` to `anim001.jpg`
find . -name 'Ref_00*.jpg' -print0 | awk 'BEGIN {RS = "\0"; ORS = "\0"} {print; gsub(/\/Ref_00/,"/anim"); print }' | xargs -0 -n2 mv
# Or use bash+sed directly, but less performant
for i in 'Ref_00'*.jpg; do mv "$i" "$(echo $i | sed 's/^Ref_00/anim/')"; done;
# To test, replace find by: `echo -en "Ref_00001.jpg\0Ref_00002.jpg\0Ref_00002.jpg"` and replace xargs with `xargs -0 -n2 echo`
# Rename `sprite1.png` to `sprite0.png`, `sprite2.png` to `sprite1.png`, etc.
find . -maxdepth 1 -type f -exec mv "{}" "{}.tmp" \; -print0 | awk 'BEGIN {RS = "\0"; ORS = "\0"} {match($0,/^(.*sprite)([0-9]+)(.*)$/,a); print a[1]a[2]a[3]".tmp"; print a[1]a[2]-1a[3] }' | xargs -0 -n2 mv
# To test, replace find by: `echo -en "sprite1.png\0sprite2.png\0sprite3.png"` and replace xargs with `xargs -0 -n2 echo`
# Use Perl rename (util-linux rename.ul, perl prename, p5-file-rename)
# Rename *.JPG to *.jpg
rename 's/\.JPG/\.jpg/' *.JPG
# Strip spaces
rename 's/ //' *.jpg
# Lower case
rename 'y/A-Z/a-z/' *
# Copying a file
cp file1 file2
# Copy all files in dir1 to dir2 (recursively)
cp -R dir1/. dir2/
# Copy all files of a directory within the current work directory
cp dir/* .
# Copy a directory within the current work directory
cp -a /tmp/dir1 .
# Copy a directory
cp -a dir1 dir2
# Find and copy all files with `.txt` extension from a directory to another
find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents
# local copy preserving permissions and links from a directory to another
tar cf - . | (cd /tmp/backup ; tar xf - )
On macOS install it with port port install p5-file-rename (but should use the cmd rename-5.22 where 22 is installed version via port instead of rename) or brew brew install rename
# Decompress a file called `file1.bz2`
bunzip2 file1.bz2
# Compress a file called `file1`
bzip2 file1
# Decompress a file called `file1.gz`
gunzip file1.gz
# Compress a file called `file1`
gzip file1
# Compress with maximum compression
gzip -9 file1
# Create an archive rar called `file1.rar`
rar a file1.rar test_file
# Compress `file1`, `file2` and `dir1` simultaneously
rar a file1.rar file1 file2 dir1
# Decompress rar archive
rar x file1.rar
# Create a uncompressed tarball
tar -cvf archive.tar file1
# Create an archive containing `file1`, `file2` and `dir1`
tar -cvf archive.tar file1 file2 dir1
# Show contents of an archive
tar -tf archive.tar
# Extract a tarball
tar -xvf archive.tar
# Extract a tarball into / tmp
tar -xvf archive.tar -C /tmp
# Create a tarball compressed into bzip2
tar -cvfj archive.tar.bz2 dir1
# Create a tarball compressed into gzip
tar -cvfz archive.tar.gz dir1
# Decompress a compressed tar archive in bzip2
tar -xvfj archive.tar.bz2
# Decompress a compressed tar archive in gzip
tar -xvfz archive.tar.gz
# Make a incremental backup of directory `/home/user`
tar -Puf backup.tar /home/user
# Decompress rar archive
unrar x file1.rar
# Decompress a zip archive
unzip file1.zip
# Create an archive compressed in zip
zip file1.zip file1
# Compress in zip several files and directories simultaneously
zip -r file1.zip file1 file2 dir1
Create archive from file list:
#!/bin/bash
# Create temp list
list=$(mktemp "/tmp/archive_list.XXXXXXXXXX")
# Write stdin file / folder list
cat > "$list"
# Get destination folder based on first file / folder
first=$(head -n 1 "$list")
folder=$(dirname "$first")
# Temporary file and final file
final="$folder/Archive.zip"
file="$final.tmp"
# Create archive file (zip, ultra)
/opt/local/bin/7z a -tzip -mx=9 "$file" @"$list"
# Move to final destination
mv "$file" "$final"
# Remove temp list
rm "$list"
Test if dirB/file.ext exist else choose dest file name as dirB/file-1.ext, if already exist choose dirB/file-2.ext and so one
source=dirA/file.ext
dest_dir=dirB
file=$(basename file.ext)
basename=${file%.*}
ext=${file##*.}
if [[ ! -e "$dest_dir/$basename.$ext" ]]; then
# file does not exist in the destination directory
mv "$source" "$dest_dir"
else
num=2
while [[ -e "$dest_dir/$basename-$num.$ext" ]]; do
(( num++ ))
done
mv "$source" "$dest_dir/$basename-$num.$ext"
fi
File attributes
# Allows write opening of a file only append mode
chattr +a file1
# Allows that a file is compressed / decompressed automatically by the kernel
chattr +c file1
# Makes it an immutable file, which can not be removed, altered, renamed or linked
chattr +i file1
# All attributes (see details in the manpage of `chattr`):
# - append only (a)
# - compressed (c)
# - no dump (d)
# - extent format (e)
# - immutable (i)
# - data journalling (j)
# - secure deletion (s)
# - no tail-merging (t)
# - undeletable (u)
# - no atime updates (A)
# - synchronous directory updates (D)
# - synchronous updates (S)
# - top of directory hierarchy (T)
# show specials attributes
lsattr
File type
# Get file type if known by libmagic
file --mime /path/file
# Get information about files in the current folder
find . -maxdepth 1 -type f -exec file "{}" \;
Supported formats are listed by:
files in /etc/magic
/etc/magic.mime
/usr/share/misc/magic.mgc
/usr/share/file/magic.mgc
/opt/local/share/misc/magic.mgc
files in /opt/local/share/misc/magic
~/.magic.mgc
~/.magic
The extension of compiled magic: *.mgc
# Test if a file exist
filename=$1
if [ -f $filename ]
then
echo "$filename exists"
else
echo "$filename does NOT exist"
fi
Files permissions
# Change group of files
chgrp group1 file1
# Set permissions reading (r), write (w) and (x) access to users owner (u) group (g) and others (o)
chmod ugo+rwx directory1
# Remove permits reading (r), write (w) and (x) access to users group (g) and others (or
chmod go-rwx directory1
# Set SUID bit on a binary file - the user that running that file gets same privileges as owner
chmod u+s /bin/file1
# Disable SUID bit on a binary file
chmod u-s /bin/file1
# Set SGID bit on a directory - similar to SUID but for directory
chmod g+s /home/public
# Disable SGID bit on a directory
chmod g-s /home/public
# Set STIKY bit on a directory - allows files deletion only to legitimate owners
chmod o+t /home/public
# Disable STIKY bit on a directory
chmod o-t /home/public
# Change owner of a file
chown user1 file1
# Change user owner of a directory and all the files and directories contained inside
chown -R user1 directory1
# Change user and group ownership of a file
chown user1:group1 file1
# View all files on the system with SUID configured
find / -perm -u+s
Name, extension and parent folder
Note: Don't use parameter substitution, it's not work with all cases
# View the contents of a file starting from the first row
cat file1
# View first two lines of a file
head -2 file1
# Pagers (moving down the file content one screen at a time)
# View content of a file along, allowing only forward navigation through the file
more file1
# View content of a file along, allowing forward and backward navigation through the file
less file1
# View the contents of a file starting from the last line
tac file1
# View last two lines of a file
tail -2 file1
# View in real time what is added to a file
tail -f /var/log/messages
# View from line 20 to 30 of a file
sed -n '20,30 p' file1
# View from line 20 to end of a file
sed -n '20,$ p' file1
# View from the begining to line 20 of a file
sed -n '1,20 p' file1
# Divide into 5 columns
ls /tmp | pr -T5 -W$COLUMNS
# Create or tuncate a file
> /var/log/apache2/error.log
# Create an empty file or change the modification date
touch file.ext
# Create a large test file (taking no space)
dd bs=1 seek=2TB if=/dev/null of=file.ext
File comparaisons
Note: you can export LANG=C for speed. Also these assume no duplicate lines within a file
# Union of unsorted files
sort file1 file2 | uniq
# Intersection of unsorted files
sort file1 file2 | uniq -d
# Difference of unsorted files
sort file1 file1 file2 | uniq -u
# Symmetric Difference of unsorted files
sort file1 file2 | uniq -u
# Union of sorted files
join -t'\0' -a1 -a2 file1 file2
# Intersection of sorted files
join -t'\0' file1 file2
# Difference of sorted files
join -t'\0' -v2 file1 file2
# Symmetric Difference of sorted files
join -t'\0' -v1 -v2 file1 file2
Use diff and patch
Compare 2 folders:
Sans -q mais avec -u pour faire un patch
diff -rq /path/dirA /path/dirB
Et sans prendre en compte les fichiers metadonnรฉes d'OSX et de Windows, le tout ordonnรฉ :
Aka folder link, like symlink but for folder (standart implementation don't allow to link folder)
# Mount a folder at a specific point (here `/path/destdir`)
mount --bind /path/srcdir /path/destdir
# Unmount
umount /path/destdir
The mount point is registerd in /etc/fstab as fs-type: bind
Archives and compression
# Encrypt file
gpg -c file
# Decrypt file
gpg file.gpg
# Make compressed archive of dir/
tar -c dir/ | bzip2 > dir.tar.bz2
# Find all files with `.log` extension and make an bzip archive
find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2
# Extract archive (use gzip instead of bzip2 for tar.gz files)
bzip2 -dc dir.tar.bz2 | tar -x
# Make encrypted archive of dir/ on remote machine
tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'
# Make archive of subset of dir/ and below
find dir/ -name '*.txt' | tar -c --files-from=- | bzip2 > dir_txt.tar.bz2
# Make copy of subset of dir/ and below
find dir/ -name '*.txt' | xargs cp -a --target-directory=dir_txt/ --parents
# Copy (with permissions) copy/ dir to /where/to/ dir
( tar -c /dir/to/copy ) | ( cd /where/to/ && tar -x -p )
# Copy (with permissions) contents of copy/ dir to /where/to/
( cd /dir/to/copy && tar -c . ) | ( cd /where/to/ && tar -x -p )
# Copy (with permissions) copy/ dir to remote:/where/to/ dir
( tar -c /dir/to/copy ) | ssh -C user@remote 'cd /where/to/ && tar -x -p'
# Backup harddrive (raw) to remote machine
dd bs=1M if=/dev/sda | gzip | ssh user@remote 'dd of=sda.gz'
Incremental listing
Incrementaly add/changed/delete files (list made from cron or similar)
find . -type f -mtime -7 -exec ls -l {} \; > list.txt
find . -type f -exec ls -i {} + | sort -k2 > list.txt
tar --create --file=archive.1.tar --listed-incremental=/var/log/usr.snar .
tar -cf /dev/null -g /var/log/usr.snar .
date +%s
/backupdevice/folder
date +%s%N > /backupdevice/folder.index
Use recode that obsoletes iconv, dos2unix, unix2dos
# Show available conversions (aliases on each line)
recode -l
#iconv -l
# Windows "ansi" to local charset (auto does CRLF conversion)
recode windows-1252.. file_to_change.txt
# Windows utf8 to local charset
recode utf-8/CRLF.. file_to_change.txt
# Latin9 (western europe) to utf8
recode iso-8859-15..utf8 file_to_change.txt
#iconv -f iso-8859-15 -t utf-8 <infile> -o <outfile>
# Base64 encode / decode
recode ../b64 < file.png
# Quoted printable decode
recode /qp.. < file.qp > file.txt
# Text to HTML
recode ..HTML < file.txt > file.html
# Lookup table of characters
recode -lf windows-1252 | grep euro
# Convert a text file format from MSDOS to UNIX (line-ending CRLF to LF)
recode ibmpc..latin1 file file2
#dos2unix file file2
#cat file | tr -d '\r' > file2 && mv -v file2 file
#sed -i s/\r//g file
# convert a text file format from UNIX to MSDOS (line-ending LF to CRLF)
recode latin1..ibmpc file file2
#unix2dos file file2
#sed -i 's/$/\r/' file
# Show what a code represents in latin-9 charmap
echo -n 0x80 | recode latin-9/x1..dump
# Show latin-9 encoding
echo -n 0x20AC | recode ucs-2/x2..latin-9/x
# Show utf-8 encoding
echo -n 0x20AC | recode ucs-2/x2..utf-8/x
Temporary file
temp_file=$(mktemp)
# Remove the file when the script exit
trap "rm -f $temp_file" 0
# do something with the file
echo "Hello World!" > $temp_file
cat $temp_file
# Remove the file now (optional)
rm -f "$temp_file"
# Do something else
Do not use echo "$( jq '.address = "abcde"' test.json )" > test.json, this will not always work. Large files it will cause issues, also with whitespaces, non-printable and escapment sequences. Never redirect a file to itself, it is always a bad idea.
function relative_path_from_to() {
# strip trailing slashes
path1=${1%\/}
path2=${2%\/}
# common part of both paths
common=$(printf '%s\x0%s' "${path1}" "${path2}" | sed 's/\(.*\/\).*\x0\1.*/\1/')
# how many directories we have to go up to the common part
up=$(grep -o "/" <<< ${path1#$common} | wc -l)
# create a prefix in the form of ../../ ...
prefix=""; for ((i=0; i<=$up; i++)); do prefix="$prefix../"; done
# return prefix plus second path without common
printf "$prefix${2#$common}"
}
relative_path_from_to /path/test1/file /path/test2/file
# > ../../test2/file
# require the path of both file and dir to exist, resolve symlink
realpath --relative-to=DIR FILE
# Echo multiline string (use the newline separator `\n`)
echo -e "a\nb"
# Add line to begin of file, aka prepend text
echo "firstline" | cat - file.txt > file.txt.tmp && mv file.txt.tmp file.txt
# Convert from lower case in upper case
tr '[:lower:]' '[:upper:]'
# Sort contents of two files
sort file1 file2
# Sort contents of two files omitting lines repeated
sort file1 file2 | uniq
# Sort contents of two files by viewing only unique line
sort file1 file2 | uniq -u
# Sort contents of two files by viewing only duplicate line
sort file1 file2 | uniq -d
# Sort IPV4 ip addresses
sort -t. -k1,1n -k2,2n -k3,3n -k4,4n
# Filter non printable characters
tr -dc '[:print:]'
# cut fields separated by blanks
tr -s '[:blank:]' '\t'
# Count lines
wc -l
# Number row of a file
cat -n file1
# Compare contents of two files by deleting only unique lines from `file1`
comm -1 file1 file2
# Compare contents of two files by deleting only unique lines from `file2`
comm -2 file1 file2
# Compare contents of two files by deleting only the lines that appear on both files
comm -3 file1 file2
# Compare with awk: https://stackoverflow.com/questions/15065818/compare-files-with-awk
# Find differences between two files
diff file1 file2
# Look up words "Aug" on file `/var/log/messages`
grep Aug /var/log/messages
# Look up words that begin with "Aug" on file `/var/log/messages`
grep ^Aug /var/log/messages
# Select from file `/var/log/messages` all lines that contain numbers
grep [0-9] /var/log/messages
# Search string "Aug" at directory `/var/log` and below
grep Aug -R /var/log/*
# Find text in files
grep -rn "texttofind" *
grep "string text to find in all files" . -R
# Highlight occurances of regular expression in dictionary
grep --color reference /usr/share/dict/words
# Merging contents of two files for columns
paste file1 file2
# Merging contents of two files for columns with `+` delimiter on the center
paste -d '+' file1 file2
# Find differences between two files and merge interactively alike `diff`
sdiff file1 file2
# Find in files with a multile regex
# See https://stackoverflow.com/a/7167115/470117
find ./path/to/files -type f -exec grep -Pazo '(?s)<control>.*?</control>' {} \+
# Split string into an array
mapfile -d '' array < <(echo -en "a\0b\0c")
# Join array with single char separator
echo "$(IFS=, ; echo "${array[*]}")"
# Join array with multi chars separator
printf "\"%s\"," "${array[@]}"
Note: sed uses stdin and stdout. Newer versions support inplace editing with the -i option
Remove multiline comment:
# Remove in place all multiline comments
# Note: see also m regex flag
sed -Ei -e '1h;2,$H;$!d;g' -e 's|/\*.*?\*/||g' file.ext
# Single line only (sed match by default on line basis)
sed -i '/<!--.*-->/ d' file
# Note: the , implied the multiple lines.
sed -i '/<!--/,/-->/ d' file
#/bin/bash
# Search files that match some pre conditions then apply remplacements
# find . -iname "*.aspx" -type f -exec grep -q -i -E "Page Language=\"VB\"" {} \; -exec grep -q -i -E "<Fnac:HtmlFooter" {} \; -exec ./substitute.sh {} \; -exec unix2dos -q {} \; -print
REGEXP='<Fnac:HtmlFooter\s+ID="HtmlFooter"\s+runat="server"(\s+Omniture="Default")?\s+OmnitureEVar2="([^"]*)"(\s+OmniturePageName="([^"]*)")?(\s+TagCommander="Default")?\s+TagCommanderIdentifier="1"\s+TagCommanderTemplateType="([^"]*)"\s+TagCommanderTemplateName="([^"]*)"\s+/>'
# All chars must be escaped, only newlines are kept for readability
REPLACEMENT=$(cat <<EOF | sed ':a;N;$!ba;s/\n/\\n/g'
<%
' Html.SetTrackingValues(Tracker.Omniture, new {...});
TrackingHelpers.SetTrackingValues(Tracker.TagCommander, New With
{
Key .template_type = "\6",
Key .template_name = "\7"
})
TrackingHelpers.SetTrackingValues(Tracker.Omniture, New With
{
Key .eVar2 = "\2",
Key .pageName = "\4"
})
%>
<%=WebFormsMvcUtilities.Partial("~/Shared/_Tracking.cshtml", New List(Of Tracker)({ Tracker.TagCommander, Tracker.Omniture }))%>
</body>
</html>
EOF
)
sed -Ei 's|<%@ Register TagPrefix="fnac" TagName="HtmlFooter" Src="~/Nav/Core/Common/HtmlControls/HtmlFooter.ascx" %>||' "$@"
sed -Ei -e '1h;2,$H;$!d;g' -e "s|$REGEXP|$REPLACEMENT|" "$@"
# Replace text in file, need a temp file
# Note: if `old-text` contains slashes `/`, back slash them: `../..` give `..\/..`
sed "s/old-text/new-text/g" file.txt > file.txt.tmp && mv file.txt.tmp file.txt
# Replace string1 with string2
sed 's/string1/string2/g'
# Modify anystring1 to anystring2
sed 's/\(.*\)1/\12/g'
# Remove comments and blank lines
sed '/^ *#/d; /^ *$/d'
# Concatenate lines with trailing \
sed ':a; /\\$/N; s/\\\n//; ta'
# Remove trailing spaces from lines
sed 's/[ \t]*$//'
# Escape shell metacharacters active within double quotes
sed 's/\([`"$\]\)/\\\1/g'
# Right align numbers
seq 10 | sed "s/^/ /; s/ *\(.\{7,\}\)/\1/"
# Duplicate a column
seq 10 | sed p | paste - -
# Print 1000th line
sed -n '1000{p;q}'
# Print lines 10 to 20
sed -n '10,20p;20q'
# Extract title from HTML web page
sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'
# Delete a particular line
sed -i 42d ~/.ssh/known_hosts
# Replace "string1" with "string2"
sed 's/string1/string2/g'
# Remove all blank lines
sed '/^$/d'
# Remove comments and blank lines
sed '/ *#/d; /^$/d'
# Eliminates the first line
sed -e '1d'
# View only lines that contain the word `string1`
sed -n '/string1/p'
# Remove empty characters at the end of each row
sed -e 's/ *$//'
# Remove only the word "string1" from text and leave intact all
sed -e 's/string1//g'
# Print from 1th to 5th row of example.txt
sed -n '1,5p'
# Print row number 5 of example.txt
sed -n '5p;5q'
# Replace more zeros with a single zero
sed -e 's/00*/0/g'
AWK
AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.
# Print all fields
awk '{print NR": "$0; for(i=1;i<=NF;++i) print "\t"i": "$i}'
# Remove all even lines
awk 'NR%2==1'
# View the first column of a line
echo a b c | awk '{print $1}'
# View the first and third column of a line
echo a b c | awk '{print $1,$3}'
# Mounting a Filesystem:
# force umount when the device is busy
fuser -km /mnt/hda2
# mount disk called hda2 - verify existence of the directory `/mnt/hda2`
mount /dev/hda2 /mnt/hda2
# mount a floppy disk
mount /dev/fd0 /mnt/floppy
# mount a cdrom / dvdrom
mount /dev/cdrom /mnt/cdrom
# mount a cdrw / dvdrom
mount /dev/hdc /mnt/cdrecorder
# mount a cdrw / dvdrom
mount /dev/hdb /mnt/cdrecorder
# mount a file or iso image
mount -o loop file.iso /mnt/cdrom
# mount a Windows FAT32 file system
mount -t vfat /dev/hda5 /mnt/hda5
# mount a usb pen-drive or flash-drive
mount /dev/sda1 /mnt/usbdisk
# mount a windows network share
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share
# unmount disk called hda2 - exit from mount point `/mnt/hda2` first
umount /dev/hda2
# run umount without writing the file /etc/mtab - useful when the file is read-only or the hard disk is full
umount -n /mnt/hda2
# Show all partitions registered on the system
cat /proc/partitions
# List mounted filesystems on the system (and align output)
mount | column -t
# Show info about disk sda
hdparm -i /dev/sda
# displays the characteristics of a hard-disk
hdparm -i /dev/hda
# perform test reading on a hard-disk
hdparm -tT /dev/sda
# Do a read speed test on disk sda
hdparm -tT /dev/sda
# Test for unreadable blocks on disk sda
badblocks -s /dev/sda
# How long has this disk (system) been powered on in total
smartctl -A /dev/sda | grep Power_On_Hours
# Check bad blocks on disk hda1
badblocks -v /dev/hda1
# Repair / check integrity of dos filesystems on disk hda1
dosfsck /dev/hda1
# Repair / check integrity of ext2 filesystem on disk hda1
e2fsck /dev/hda1
# Repair / check integrity of ext3 filesystem on disk hda1
e2fsck -j /dev/hda1
# Repair / check integrity of linux filesystem on disk hda1
fsck /dev/hda1
# Repair / check integrity of ext2 filesystem on disk hda1
fsck.ext2 /dev/hda1
# Repair / check integrity of ext3 filesystem on disk hda1
fsck.ext3 /dev/hda1
# Repair / check integrity of fat filesystem on disk hda1
fsck.vfat /dev/hda1
# Repair / check integrity of dos filesystem on disk hda1
fsck.msdos /dev/hda1
Backup the filesystem
# Note: only supported by ext2/ext3 filesystems
# Make a full backup of directory `/home`
dump -0aj -f /tmp/home0.bak /home
# Make a incremental backup of directory `/home`
dump -1aj -f /tmp/home0.bak /home
# Restoring a backup interactively
restore -if /tmp/home0.bak
# Format a floppy disk
fdformat -n /dev/fd0
# Create a filesystem type linux ext2 on hda1 partition
mke2fs /dev/hda1
# Create a filesystem type linux ext3 (journal) on hda1 partition
mke2fs -j /dev/hda1
# Create a filesystem type linux on hda1 partition
mkfs /dev/hda1
# Create a FAT32 filesystem
mkfs -t vfat 32 -F /dev/hda1
Filesystem swap:
# Create a swap filesystem
mkswap /dev/hda3
# Activating a new swap partition
swapon /dev/hda3
# Activate two swap partitions
swapon /dev/hda2 /dev/hdb3
Disk space
See also FSlint
# Show files by size, biggest last
ls -lSr | more
# Show top disk users in current dir. See also dutop
du -s * | sort -k1,1rn | head
# Sort paths by easy to interpret disk usage
du -hs /home/* | sort -k1,1h
# Show size of the files and directories sorted by size
du -sk * | sort -rn
# Estimate space used by directory `dir1`
du -sh dir1
# Show free space on mounted filesystems aks disk usage
df -h
# Show free inodes on mounted filesystems
df -i
# Show disks partitions sizes and types (run as root)
fdisk -l
Clone disk
# Make a copy of a local hard disk on remote host via ssh
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz'
# Copy content of the harddrive to a file
dd if=/dev/sda of=/tmp/file1
# Make a copy of MBR (Master Boot Record) to floppy
dd if=/dev/hda of=/dev/fd0 bs=512 count=1
# Restore MBR from a copy saved to floppy
dd if=/dev/fd0 of=/dev/hda bs=512 count=1
Aka duplicate disks
For supported partitions (on source drive), use Disk Utility
For not supported partitions
Clone exact disk (boot sector, all partitions & data, etc.) to an other drive (to a file, use a path to a file instead of /dev/sdX, see this page for other options like split the image and dd man page for partial copy etc.)
Change bs to find the optimal value. 64K seem a good value, but it's related to hardware and software (USB2.0 to USB2.0 RAID). Try to use the same value as the disk cache 8388608 size, or the block size 4096.
With bs=64K:
USB 2.0 to USB 2.0: 20 to 11 MB/s
SATA 3.0 (3.0 Gb/s) to SATA 3.0 (3.0 Gb/s): 130 MB/s
# Save copy of data cdrom
gzip < /dev/cdrom > cdrom.iso.gz
# Create cdrom image from contents of dir
mkisofs -V LABEL -r dir | gzip > cdrom.iso.gz
# Mount the cdrom image at /mnt/dir (read only)
mount -o loop cdrom.iso /mnt/dir
# Clear a CDRW
cdrecord -v dev=/dev/cdrom blank=fast
# Burn cdrom image (use dev=ATAPI -scanbus to confirm dev)
gzip -dc cdrom.iso.gz | cdrecord -v dev=/dev/cdrom -
# Rip audio tracks from CD to wav files in current dir
cdparanoia -B
# Make audio CD from all wavs in current dir (see also cdrdao)
cdrecord -v dev=/dev/cdrom -audio -pad *.wav
# perform an md5sum on a device, like a CD
dd if=/dev/hdc | md5sum
# mount an ISO image
mount -o loop cd.iso /mnt/iso
Command operations
# Show full path to a binary / executable
which command
# Show location of a binary file, source or man
whereis command
# See how long a command takes (stopwatch). Ctrl-d to stop. See also sw
time command
# Show commands pertinent to string. See also threadsafe
# Display a list of commands that pertain to keywords of a program, useful when you know what your program does, but you don't know the name of the command
apropos somekeyword
# Find the location of a command
command -v git 2> /dev/null
# displays description of what a program does
whatis somekeyword
man mv
# find any related commands
man -k mv
# make a pdf of a manual page
man -t ascii | ps2pdf - > ascii.pdf
Section numbers of the man manual:
executable programs or shell commands
system calls (functions provided by the kernel)
library calls (functions within program libraries)
special files (usually found in /dev)
file formats and conventions eg /etc/passwd
games
miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
system administration commands (usually only for root)
Some commands support also (or instead) an option that show the help, could be:
--help
-h
help
-?
/?
SSH
Aka Secure SHell
# Run command on $HOST as $USER (default command=shell)
ssh $USER@$HOST command
# Run GUI command on $HOSTNAME as $USER
ssh -f -Y $USER@$HOSTNAME xeyes
# Copy with permissions to $USER's home directory on $HOST
scp -p -r $USER@$HOST: file dir/
# Use faster crypto for local LAN. This might saturate GigE
scp -c arcfour $USER@$LANHOST: bigfile
# Forward connections to $HOSTNAME:8080 out to $HOST:80
ssh -g -L 8080:localhost:80 root@$HOST
# Forward connections from $HOST:1434 in to imap:143
ssh -R 1434:imap:143 root@$HOST
# Install public key for $USER@$HOST for password-less log in
ssh-copy-id $USER@$HOST
To disallow SSH remote root login, in /etc/ssh/sshd_config update/add: