Snippets Flashcards
How to compress using tar
tar -zcvf archive-name.tar.gz directory-name
Where,
- <kbd>-z<kbd> : Compress archive using gzip program</kbd></kbd>
- <kbd>-c</kbd>: Create archive
- <kbd>-v</kbd>: Verbose i.e display progress while creating archive
- <kbd>-f</kbd>: Archive File name
How to uncompress using tar
$ tar -zxvf prog-1-jan-2005.tar.gz
Where,
- <kbd>-x</kbd>: Extract files
Checking the size of a folder
$ du -sh
Tmux start a new session
tmux new -s [name of session]
Check tmux sessions
tmux ls
Detach from tmux session
ctrl+b d
In tmux, to split a pane horizontally
ctrl+b "
In tmux, To split pane vertically
ctrl+b %
In tmux to move from pane to pane
ctrl+b [arrow key]
In tmux, expand the pane down a few lines
ctrl+b :
Tmux, Kill named session
tmux kill-session -t [name of session]
tmux, Kill current pane
ctrl+b x
Kill tmux server, along with all sessions:
tmux kill-server
A very simple backup script
#!/bin/bash OF=/var/my-backup-$(date +%Y%m%d).tgz tar -czvf $OF /home/me/
Functions and Local variables in bash.
HELLO=Hello function hello { local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO
Check the size of a specific folder?
du -sch Dir1 File1
If statement in bash
#!/bin/bash T1="foo" T2="bar" if ["$T1" = "$T2"]; then echo expression evaluated as true else echo expression evaluated as false fi
For in bash
#!/bin/bash for i in `seq 1 10`; do echo $i done
bash for loop ten times
#!/bin/bash for i in `seq 1 10`; do echo $i done
bash while statement
#!/bin/bash COUNTER=0 while [$COUNTER -lt 10]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done
Bash Until Statement
#!/bin/bash COUNTER=20 until [$COUNTER -lt 10]; do echo COUNTER $COUNTER let COUNTER-=1 done
Bash function with parameters
#!/bin/bash function quit { exit } function e { echo $1 echo $2 } e Hello Bye
Using select to make simple menus
#!/bin/bash OPTIONS="Hello Quit" select opt in $OPTIONS; do if ["$opt" = "Quit"]; then echo done exit elif ["$opt" = "Hello"]; then echo Hello World else clear echo bad option fi done
BAsh script to do backup for a specific folder. The folder to be backed up given in argument to the script.
#!/bin/bash if [-z "$1"]; then echo usage: $0 directory exit fi SRCD=$1 TGTD="/home/alhaol/" OF=\_BackUp\_$(date +\_%Y\_%\_m\_%d).tgz tar -czvf $TGTD$SRCD$OF $SRCD echo ====================================== echo =====================Done =========== echo check $TGTD$SRCD$OF
Use cw to find how many lines in a file?
wc -l file
Use wc to count the number of words?
wc -w fileName
use ls and wc to find number of files in a folder.
ls FolderName | wc -l
Use head to show first 2 line in a file:
head -n 2 FileName
How to add header line to a file using echo, >, and cat
echo “A, B, C”> header.txt
cat header.csv file.csv > Newfile.csv
How to use sed to change the content of a file?
sed “s/<string>/<string>/g" <source_file> > <target_file></target_file></source_file></string></string>
use grep and wc to find how many times something happened in a file.
grep “Something” File | wc -l
use sed and > to find a pattern in a file and change it to something else
sed “s/somthing/somethingelse/g” adult.csv > adult.csv
Using head and tail to sub set large file
head -n 120 adult.csv | tail -n 20 > adult_sample.csv
Finding number of duplicate lines is a file using uniq, sort and wc.
sort file.csv | uniq -d | wc -l
how to use for loop to replace spaces with _
replace_source=’ ‘
replace_target=’_’
for filename in ./*.csv; do new_filename=${filename//$replace_source/$replace_target}
mv “$filename” “$new_filename”
done