Skip to content

Instantly share code, notes, and snippets.

@ovntatar
Last active March 24, 2023 07:38
Show Gist options
  • Save ovntatar/8006069 to your computer and use it in GitHub Desktop.
Save ovntatar/8006069 to your computer and use it in GitHub Desktop.
Shell Snippets
#!/bin/bash
#run task sequential
time for i in `seq 1 5`;do sleep 1; done
#run task in parallel
time for i in `seq 1 5`;do sleep 1 & done ; wait
#http://www.commandlinefu.com/
cmdfu(){
curl -L "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext";
}
#rsync
rsync -azv USER@HOST:/<FOLDER> .
# convert epoche
date -d'Tue, 03 Nov 2009 17:36:50 CET' +%s
1257266210
# convert epoche invertse
echo "1257266210" | perl -e 'print localtime(<>) . "\n"'
Tue Nov 3 17:36:50 2009
# convert epoche - or
perl -e 'print time. "\n";'
echo "1242981512" | perl -e 'print localtime(<>) . "\n"'
#Exit Code NumberMeaningExampleComments1 Catchall for general errors let "var1 = 1/0" Miscellaneous errors, such as "divide by zero"
2 Misuse of shell builtins (according to Bash documentation) Seldom seen, usually defaults to exit code 1
126 Command invoked cannot execute Permission problem or command is not an executable
127 "command not found" Possible problem with $PATH or a typo
128 Invalid argument to exit exit 3.14159 exit takes only integer args in the range 0 - 255 (see footnote)
128+n Fatal error signal "n" kill -9 $PPID of script $? returns 137 (128 + 9)
130 Script terminated by Control-C Control-C is fatal error signal 2, (130 = 128 + 2, see above)
255* Exit status out of range exit -1 exit takes only integer args in the range 0 - 255
# encrypt decrypt file ( tested on MAC )
openssl aes-256-cbc -e -pbkdf2 -in config.yaml -out config.yaml.enc
openssl aes-256-cbc -d -pbkdf2 -in config.yaml.enc -out config.yaml
#backup
tar czvf - /destination | ssh root@<HOST> "cat > my.tgz"
tar czvf - /destination | ssh root@<HOST> "cat > mein.`date +%d.%m.%y`.tgz"
tar -cvzf Foto.tgz 101msdcf/
option " -T list.txt" create backup from all fieles in lust.txt
#option -M continue backup to other partition if the source is full
tar -cjvf bakup /FOTO
bunzip2
tar -df /backup
#check backup
#make backup from all file that was changed today
tar -cv -T /tmp/today.txt -f /dev/gft0
#make backup only in currently folder not recursive
ls | cpio -o /home/test
#make backup from all folders is recursive
find . -print | cpio -oacvB /home/test
#check backup
cpio -itvI /hone/test
#recover the backup in currently folder
cpio -id < /home/<USERNAME>/BACKUP
# to know wehere you are
cd () { builtin cd $* ; pwd ; }
# alias
alias vi=vim
alias grep="grep -i --color"
# remote csv access
export CVS_RSH="ssh"
export CVSROOT=":ext:<USERNAME>@<IP ADDRESS>:/data/cvs"
# prompt setting
PS1="\t \h-[\W]$ "
# check gmail
curl -u <USERNAME> --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | perl -pe 's/^<title>(.*)<\/title>.*<name>(.*)<\/name>.*$/$2 - $1/'
# debug
time ./test.sh
strace -c -f ./test.sh
set -x
#### == FIND == ####
#remove file if older that 5 days and ask for confirm
find / -atime -5 -name "core*" -ok rm {} \;
# find file that older then 3 hours
find . -name "*somefile*" -mmin +180
#check files with permission
find / -type f -perm -4000 -exec ls -l {} \;
#check files that was changed today
find / -mtime -1 \! -type d -print > /tmp/today.txt
#Find and rename folders
#!/bin/ksh
find . -name "Root" -print|while read obj
do
mv $obj $obj-old
done
#print access in numeric and letter format then group username and file path
find / -printf "%m %M %g %u %p\n"
# or
find / -printf "%U, %G, %m, %p\n"
#find big files
find / -xdev -size +10M
#find regular files and are not exutable by others
find / -type f ! -perm o+x
# WGET
#download web.com with 2 links
wget --mirror --convert -l 2 http://web.com
#download all sites from LIST
wget -i LIST -o serverlistelog.log -t inf -mkK
#Mirror this site
wget -mrnp http://sites.google.com/site/myunixcorner/
# MOUNT
mount -t smbfs //server/freigabe/ /ziel/
mount -t smbfs -o username=<USER> , password=<PW> //<IP>/<SOURCE> /<DEST>
#GREP
#for binary file
#Process a binary file as if it were text; this is equivalent to the --binary-files=text option
grep -a file.log
#Print NUM lines of output context.
grep -C 2 muster file.txt
#Print NUM lines of trailing context after matching lines
grep -A 2 muster file.txt
grep -B 2 muster file.txt
#AWK
#Cut the first word in a file
awk -F ";" 'NR==1 {print $1}' file.csv
#NR==1 ---- LINE
#print $1 --- Column
#replace value in the second column
awk -F\| '\
BEGIN {OFS="|"}
{gsub(" ","",$2)
print}' csv.file
# UNIQ COLUMN
sort -u -t, -k1,1 file
#-u for unique
#-t, so comma is the delimiter
#-k1,1 for the key field 1
# sort csv dacument by the delimeter ";" in the first column from the 1 char till the 5 char##########
sort -t\; -k1.1 -k1.5 -k 1
#### sort numeric in csv dacument by the delimeter ";" in the first column from the 1 char till the 5 char#
sort -n -t\; -k1.1 -k1.5 -k 1
#Create a file with the timestamp of 2010 11 09 05:01 Clock
touch -t 1011090501 somefile
#Remove carriage return in Unix
tr -d '\r' | tr -d '\n'
cat file | od -c
perl -pe 's/\r\n|\n|\r/\n/g' OLD_FILE > NEW_DATEI
#Adding NEW value of end of the line if line contain DUPLICATED IP ADDRESS (NOTES: the command run wihtout output)
sed -i '/ -- DUPLICATED IP ADDRESS/s|$|NEW|' FILE.txt
perl -pe 's/\r//g' FILE.txt
#If you want to run in a loop with a string use:
sed -i -e "/$VARIABLE;/s|$|This is the end of the Line that contain $VARIABLE|" FILE.txt
#Change value in the 3th line
sed "3s/10102007/11012007/" file1.txt
#Search a value in 6th column
awk '$6 ~ /test/' file.txt
# inversed
awk '$6 !~ /test/' file.txt
#to use a string
cn=mystring
awk -F";" '$1 ~ /'"$cn"'/' List
#Handling floats
perl -e 'printf("%.2f\n",1.23456);'
printf %0.f\\n 5.6
#Remove special chars from FILE
awk '{gsub(/[[:punct:]]/,"")}1' FILE
sed "s/[^a-z|0-9]//g;" file1
#Remove spaces from files
find . -name "* *" -exec sh -c 'echo "${0}" "${0// /_}"' {} \;
#Replace string Zwo[abcdefg.....] in column 39 with 23
awk -F";" 'BEGIN{OFS=";";} {sub("Zwo*.*","23",$39);print $0}'
#CUT file from $2Linte to $3 Line (arg $2 is the file)
head -$3 $1 | tail -`expr $3 - $2 + 1`
#diff and patch
diff -c file file2
S10-$cat patch_v1.sh
#!/bin/bash
#
# version 1
#
echo "version 1"
#
S10-$cat patch_v2.sh
#!/bin/bash
#
#
# version 2
echo "verion 2"
#
$diff -u patch_v1.sh patch_v2.sh > patch.diff
$patch < patch.diff
$patching file patch_v1.sh
S10-$cat patch_v1.sh
#!/bin/bash
#
#
# version 2
echo "verion 2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment