Skip to content

Instantly share code, notes, and snippets.

@bbrother92
Last active September 2, 2019 15:54
Show Gist options
  • Save bbrother92/6de6e09b0bbb979465b4b53031a98ea7 to your computer and use it in GitHub Desktop.
Save bbrother92/6de6e09b0bbb979465b4b53031a98ea7 to your computer and use it in GitHub Desktop.
#logs #cat #find #xargs #mkdir #sed

Tail

by default tail display only first 10 lines

tail -n2 access.log 
**last 2 lines  

tail -f /var/log/mail.log 
**fresh updating  

tail -n 1000 /var/log/mail.log | more 
**if too many lines  

tail -h /var/log/mail.log 
**headers On  

tail -q file1.txt file2.txt 
**two files without headers  

Cat

cat test1.md test.md | uniq 
**find unique lines only 

-b, numer non-blank output lines
-n, number all output lines
-s, squeeze multiple adjacent blank lines
-v, display nonprinting characters, except for tabs and the end of line character

TR

tr “[a-z]” “[A-Z]”  
**all to caps

tr [:space:] '\t' 
**all the white-space to tabs

echo "Wwelcome" | tr -d -c 'w|W'
**delete and invert options

echo "Wwelcome w" | tr -c 'w|W' '1'
**change includes every character except for w


echo "Welwcome to  the   place of  lonely heart" | tr -s  '[:space:]'
**Squeeze multiple occurrences into a single instance of the character

Find

find . -type f -maxdepth 1
**without recursion 

find /path/to/search -iname filename
**case-insensitive

find /path/to/search -type f -executable

find ./ -size +100M

find /path/to/search | wc -l                   
**how many things you find

find \(-not -name "*.js" -type f \) -or -type d 
** grouping

find . -path './vendor' -prune -o -name "*.json" -print 
**prevent find from going into a directory that matches some tests

find . -name "*.php" -path "./tests/*"  -exec grep -l "foreach" {} \;
**execute command for each path found by find, {} - placeholder of that path.

Xargs

ll | xargs -L 1 -p  echo                   
**L invoke a command for each line of input 

ls [0-3]-test.xml | xargs -I replaceme find ./ -name <strong>replaceme</strong> -type f            
**Placement of arguments

echo {0..9} | xargs -n 2                   
**subset of arguments at a time

echo "1 2 3" | xargs -n 1 -I {} touch {}.txt
**create files: 1.txt 2.txt 3.txt

php

On lamp
/var/log/httpd/error_log Or
/var/log/apache2/error.log
To find error log location php --info | grep error

linux

/var/log/messages or /var/log/syslog : General message and system related stuff
/var/log/auth.log : Authenication logs
/var/log/kern.log : Kernel logs
/var/log/cron.log : Crond logs (cron job)
/var/log/maillog : Mail server logs
/var/log/qmail/ : Qmail log directory (more files inside this directory)
/var/log/httpd/ : Apache access and error logs directory
/var/log/lighttpd/ : Lighttpd access and error logs directory
/var/log/boot.log : System boot log
/var/log/mysqld.log : MySQL database server log file
/var/log/secure or /var/log/auth.log : Authentication log
/var/log/utmp or /var/log/wtmp : Login records file
/var/log/yum.log : Yum command log file.

mkdir

mkdir -p project/{lib/ext,doc/{html,info,pdf},demo/a}  
** creating directories using pattern

mkdir {1,2}{a,b}

lsof -p <process id>
** List all the files that a specific process opens

sed

sed '2,3s/test/another test/' myfile
sed '2,$s/test/another test/'
** диапазон строк 

sed '3d' myfile
** удалить строку нумер 3

sed '/second/,/fourth/d' myfile
** удалить по шаблону от первого до второго  шаблона

sed '2i\This is the inserted line.' myfile
** вставить в линию 

sed 's/root/& &/g'
** вставить в линию 

echo "abcd qwer 123"|  sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'
**  поменять местами первое и второе слово используя regexp

file:
CSE - Count
EEE - Count
Civil - Count

sed  -e '/CSE/ s/Count/100/; /EEE/ s/Count/70/;' test.txt
**  Two replace commands

sed -n -e '5,7p' -e '10,13p' myfile.txt
**  Viewing non-consecutive lines and ranges

echo "5098673" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
** 5,098,673
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment