Bash commands
Contents
Manual
man {CommandName}
Basics file & folder
cd change current path find ./ -name {Filename} search touch {FileName} create filename mv {FileName} {NewFileName} move a file or folder cp {FileName} {NewFileName} copy a file or folder (-R option for recursive / -V option for verbose) mkdir -P create a folder (-P option mean do not warn if already exist) rm -R {FolderName} delete a file or a folder+all subfolder+files (-R option for recursive) wget http://filetarget.ext download a file cat /etc/passwd dump file content grep {Text} {FileName} search Text in FileName tail -n {Number} {FileName} get n last lines from FileName (tail -f shows file stream) head -n {Number} {FileName} get n first lines from FileName ls -slash display folder's content -t order by date -s display size -h human readable size -l long display -a all content (include files starting with .) -d only folders (ajouter ./*/) -r reverse
Basics : disk
df -h display hard drive free space du -km --max-depth=1 | sort -nr display folder size (need to be in the folder before launch command) dd if={ImageName}.img of=/dev/sd{DriveId} apply a disk image to a disk lsblk disks details udevadm info -a -n /dev/sd{b1} more details for a selected disk fdisk partition management tool : http://www.howtogeek.com/106873/how-to-use-fdisk-to-manage-partitions-on-linux/
Basics user
su - {UserName} change current user sudo before a command ex: sudo gedit /etc/network/interfaces sudo -i switch to root access passwd {UserName} prompt change password groupadd {group} add a group gpasswd -a {user} {group} create/modify user useradd {user} create user useradd -d /data/{user} {user} create user with custom home dir useradd -u {number} {user} create user with custom ID useradd -M {user} create user without home dir useradd -s /sbin/nologin {user} create user without login shell useradd -f 45 {user} create user with password expire in 45 days useradd -e AAAA-MM-DD {user} create user with expire time useradd -c "{Custom Comment}" {user} create user with custom comment userdel {usser} -r Remove user (and home dir) mkhomedir_helper {user} create user default home dir usermod {user} -G {group} add user to group chown {User:Group} -R {Folder} change folder user (-R for recursive) chmod 777 -R {Folder} change folder rights (-R for recursive) htpasswd -b -m /Usr/passwd {User} {Pass} change/create user password pwHash=$( echo -n {pass} | openssl sha1 ) sha1 encode password
Basics misc
echo "{Text}" write text to output (option -e for \n) cat "{Text}" concatenate text to output set view en vars sync memory flush /etc/init.d/networking restart reset network service ln -s {FolderName} {NewFolderName} create a symbolic link rm -f {SymbolicLink} remove a symbolic link (option -f skip confirmation) a2enmod {ModuleName} enable module for apache2 export {VAR}="{Value}" set env variable read -p "{text}" {VAR} prompt text to user and save user typing as VAR echo "MSG" | mail -s "Subject" admin@frogg.fr send a mail c=$a"text"$b concatenate var + text
Structure
space in syntax are important /!\
a=$b assign value, without space else doesn't works $((a + b)) addition
if [ ! $a = $b ]; then with space and need instruction between "then" and "fi", else doesn't works {Script to execute} elif [ ! $a = $c ]; then {Script to execute} else {Script to execute} fi [ $a = $b ] && return 1 || return 0 conditionnal alternative format if [ $a = $b -o $b = $c ] -o mean OR if [ $a = $b -a $b = $c ] -a mean AND if [ $a = $b -o $b = $c ] || [ $a = $b -a $a = $c ] multiple conditionnal or if [ $a = $b -o $b = $c ] && [ $a = $b -a $a = $c ] multiple conditionnal and if [ -z $a ] is empty or null if [ ! -z $a ] not equal to "" if [ -n $a ] is not empty or null if [ $a -eq $b ] equal if [ $a -ne $b ] not equal if [ $a -gt $b ] greater than if [ $a -ge $b ] greater or equal than if [ $a -lt $b ] lower than if [ $a -le $b ] lower or equal than if [ -d $a ] is a directory if [ -e $a ] file exist if [ -f $a ] is a file if [ -g $a ] is group allowed for the file if [ -r $a ] is readable if [ -s $a ] file is not 0kb if [ -u $a ] is user allowed for file if [ -w $a ] is writable if [ -x $a ] is executable if [[ "$a" =~ "$b" ]] match (reg exp) if [[ $a == *$b* ]] match (glob) if [ "${a/$b}" = $a ] match (string) if [ -z "${a##*$b*}" ] match (string, work with dropbox !) if [ ${a/$b} = $a ] match (string)
for file in {Folder}/* for syntax do {Script to execute} done for i in {$START..$END} for syntax (another way) for (( i=0; i<=$END; i++ )) for syntax (another way)
plus d'infos: http://www.cyberciti.biz/faq/bash-for-loop/
while true; do while syntax {Script to execute} done
http://www.cyberciti.biz/faq/bash-while-loop/
case $1 in switch case syntax 0) {Script to execute} ;; stop matching search 7) {Script to execute} ;;& continue matching search * ) all case {Script to execute each time if not break} ;; esac
cat <<EOF >> {FileName} write to file all next string with line break include {StringToWrite} requier no space between start of line and String {StringToWrite} {StringToWrite} EOF
Function
functionName() create a function { {Script à exécuter} $1 $1 first param $2 $2 second param }
functionName "Param1" $param2 function call
function fun1() get function result via echo { echo 34 } res=$(fun1) res = returned string
function fun2() get function result via return { return "OUTPUT" } if func2;then if send a result then it mean 1 else it mean 0 echo "FOUND OUTPUT" else echo "NO OUTPUT or returned value=FALSE" fi
Compression
tar -cvzf {Archive.tgz} {TargetFolder} create tar archive tar -C {TargetFolder} -xvzf {Archive.tgz} extract tar archive zip -r {Archive.zip} {TargetFolder} create zip archive unzip {Archive.zip} -d {TargetFolder} extract zip archive unrar {Archive.rar} extract rar archive (package unrar-free) 7z x {Archive.ext} extract any kind of archive (package 7zip-full)
Special commands
$0 script name ${0##*/} = $(echo $0 | rev | cut -d/ -f1 | rev) allstring after last / ${0%/*} = $(echo $0 | sed "s/\/$Path//g") all string before last / ${0#*/} delete all before first / ${0%/*} delete all after last / ${0%%/*} delete all after first / ${1%?} remove last char ${0:0:1} first char ${0/a/b} replace first a by b ${0//a/b} replace all a by b ${0//@/ } split char @ ${0//[[:alpha:]]/X} replace alpha by X (can be [[:digit:]]) ${0//[a-zA-Z]/X} and it works with reg exp ${0^^} to uppercase ${0,,} to lowercase ${#0} string size ${!var} create a dynamic variable DATE=`date '+%Y%m%d'` display date to format AAAAMMJJ DATE=`date '+%Y/%m/%d %H:%M:%S'` display date to format AAAA/MM/JJ HH:MM:SS . {FileName} include filename in bash scripting {ScriptToExecute} > /dev/null stdout {ScriptToExecute} 2> /dev/null stderr {ScriptToExecute} 2>&1 /dev/null stderr to stdout {ScriptToExecute} 1>&2 /dev/null stdout to stderr {ScriptToExecute} >> /dev/null add to {ScriptToExecute} &> /dev/null stderr and stdout {ScriptToExecute} >> {ToFile} 2>&1 stderr to stdout to file printf '2.4.5a\n2.8b\n2.4.5zz.1\n' | sort -V sort version list (asc) $(printf "%03d\n" 7) padd a number, in this case: 007 $(expr ${a} + ${b})) force string to be add as integer echo ${a}| tr -d '\n' remove line break from a var ((i++)) incremental & at the end of a script call will start it in background
- string manipulation
http://tldp.org/LDP/abs/html/string-manipulation.html
- copy using ssh between 2 server
scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program][[user@]host1:]file1 ... [[user@]host2:]file2
- remove ^M (chr(10)chr(13) = windows line break) from a file
sed -i -e "s/\r//g" filename
- convert string to array
ARR=($0) basic case (space or : as separator)
ARR=(`echo $0 | tr " " "\n"`) using tr (\n as separator)
IFS=" " read -ra ARR <<< "$0 using IFS (space as separator)
ARR=(`echo $0 | sed -e 's/ /\n/g'`) using sed (\n as separator)
to test the array:
for k in "${ARR[@]}";do echo "[$k]";done
array size:
size=${#ARR[@]}
- copy file to remote computer with ssh
ssh <user>@<ip_du_serveur> -p <port> "echo $(cat ~/.ssh/id_rsa.pub) >> .ssh/authorized_keys"
Search
grep -R "TEXT" ./ search TEXT in current folder & childs find ./ -name "filename" -type f -exec grep mytext {} \;
Explications :
find ./ search in current folder -name "filename" a file with name matching pattern -type f a type file -exec execute for each file found grep mytext {} search "mytext" in files ( {} replace filename found). \; end exec option ( \ can add more commande after)
- the fastest way:
grep -r -n -i --include="*.htm *.php" "TEXTSTRING" . "-i" makes it case insensitlve "./" at the end means you want to start from your current directory, this could be substituted with any directory. "-r" means do this recursively, right down the directory tree "-n" prints the line number for matches. "--include" lets you add file names, extensions. Wildcards accepted
System
halt poweroff reboot shutdown
memory
free -m cat /proc/meminfo top
swap
swapoff -a swapon -a
other
pinky
Network
nslookup domaine.example.com
nc -zv 127.0.0.1 80
Send a Mail via netcat
echo "HELO esxi.frogg.fr" > mail.txt echo "AUTH LOGIN" >> mail.txt echo "BASE64encodedEMAIL" >> mail.txt echo "BASE64encodedPASS" >> mail.txt echo "MAIL FROM:esxi@frogg.fr" >> mail.txt echo "RCPT TO:admin@frogg.fr" >> mail.txt echo "DATA" >> mail.txt echo "From: admin@frogg.fr" >> mail.txt echo "To: admin@frogg.fr" >> mail.txt echo "Subject: FroggPT BackUP result" >> mail.txt echo "" >> mail.txt echo "THIS IS A TEST" >> mail.txt echo "" >> mail.txt echo "." >> mail.txt echo "QUIT" >> mail.txt # Send the mail /usr/bin/nc smtp-auth.Register.eu 25 < mail.txt
Crypting
encrypt
echo -n "password" | md5sum md5 encrypt echo -n "password" | openssl dgst -sha1 sha1 encrypt echo "password" | base64 base64 encrypt
decrypt
echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode