#CLI
#Unix
#Dev
Some Advance unix commands
crontab
, tail
, Screen
, grep
, compress
, wc
CRONTAB
- The crontab is a list of commands that you want to
run on a regular schedule
crontab -l
crontab -e
crontab -r

min hour date month dayofweek
0-59 0-23 1-31 1-12 or JAN-DEC 0-7 or SUN-SAT
EXAMPLE
* * * * * ---> every min 24X7
12,46 1,2,3 7,9 MAR,AUG 3,5 ---> for 12 and 46 min
34-56 6-12 7-14 3-8 MON-FRI ---> from 34 to 56 every min
34-56/2 6-12 7-14 3-8 MON-FRI ---> from 34 to 56 every 2 min
*/5 * * * * ---> Every 5 min, STARTS from 0 min
10 11 12-17 * WED ---> At 11.30 b/w(12-17 & WED)
Linux Tail Command
- One of the most common uses of the tail command is to watch and analyze logs and other files that change over time
tail [OPTION]... [FILE]...
Default
- The tail command displays the last part (
10 lines
by default)
tail filename.txt
How to Display a Specific Number of Lines
tail -n 50 filename.txt
tail -50 filename.txt
tail +25 state.txt
How to Display a Specific Number of Bytes
tail -c 500 filename.txt
tail -c 5k filename.txt
tail -c +263 state.txt
How to Display Multiple Files
- For multiple files it will display the
last 10 lines from each file
.
tail filename1.txt filename2.txt
tail -n 20 filename1.txt filename2.txt
How to Watch a File for Changes
tail -f /var/log/nginx/error.log
How to Use Tail with Other Commands
tail -f /var/log/apache2/access.log | grep 192.168.42.12
tail -n 7 state.txt | sort -r
cat state.txt | head -n 20 | tail -n 5 > list.txt
SCREEN
- Screen command in Linux provides the ability to launch and use
multiple shell sessions
from a single ssh session
- Screen session can
run in background
- NOTE:
- Be carefully while using screen
- It may use more RAM for some process
- So, it’s better to kill screen after use
- Install may be needed
sudo apt-get install screen
screen -S name
screen -x name
screen -r name
Ctrl + a d
Ctrl + D
screen -X -S 16253 quit
* screen -ls
* Ctrl a 1
* Ctrl a 2
Ctrl + A, Esc
And then use --> Arrow Key
Count lines in file
wc -l file_name
wc -l *
GREP command
grep [options] pattern [files]
Syntax
-i,
-r,
-A NUM,
grep -i 'are' a.txt
grep -A 3 -i 'are' a.txt
grep -r 'are' a.txt
grep -r 'are'
ls | grep .txt
grep -Po '"subcategory": "(.\*?)"' douglas_cate.json | sort | uniq -c
grep '/product/' product.json | sort | uniq -c | wc -l
zcat * | grep '"status": "503"' | grep -Po '"url": "(.\*?)"' | sort | uniq -c
grep -Po '"url": "(.*?)"' half_file_nane* | wc -l
zcat * | grep '"http_status": "200"' > http_200.json
grep -l "Bindi" * ---> files name
grep -i "Bindi" * ---> data , ignore case
grep -c "Bindi" * ---> count
grep -il "bindi" * ---> ignorecase, filename
grep -w "bindi" * ---> Exact-case
grep -n "bindi" * ---> line number, data
ls –l |grep “^d”
ls –l |grep “^-”
Compress
# Folder Zip
zip -r <folder.zip> <folder>
# Folder Unzip
unzip <folder.zip>
# File Zip
gzip <filename>
# File Unzip
gunzip <filename>
tar cvf archive_name.tar dirname/
tar xvf archive_name.tar
Reference