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
# Shows all Crontab
crontab -l

# Edit Crontab
crontab -e

# Remove all cron tab --> Be careful
crontab -r

CRON Cheatsheet

# FORMAT
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

# Display last 50 lines of a file
tail -n 50 filename.txt
tail -50 filename.txt

# Display from line number ‘n’ till the end of the file
tail +25 state.txt

How to Display a Specific Number of Bytes

# Display the last num bytes
# num --> 5000 byte or 5k byte
tail -c 500 filename.txt
tail -c 5k filename.txt

# Display all the data after skipping num bytes
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

  • Check live logs
tail -f /var/log/nginx/error.log
# ctrl + c --> to end

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
# new screen
screen -S name

# switch screen
screen -x name

# attach screen
screen -r name

# detach present screen and go to main screen
Ctrl + a  d

# kill screen
Ctrl + D
 
# kill screen from outside
screen -X -S 16253 quit

* screen -ls
* Ctrl a 1
* Ctrl a 2
  • Scroll in screen
Ctrl + A, Esc
And then use --> Arrow Key

Count lines in file

  • Count no of lines
# Line in 1 file
wc -l file_name

# All files line count
wc -l *

GREP command

  • grep [options] pattern [files]
Syntax
  -i, 	--ignore-case
  -r, 	--recursive
  -A NUM, --after-context
# Search for a given string in file
grep -i 'are' a.txt

# Print the matched line, along with the 3 lines after it.
grep -A 3 -i 'are' a.txt

# Search for a given string in a file
grep -r 'are' a.txt
# Search for a given string in all files recursively
grep -r 'are'

# Display only .txt files	
ls | grep .txt
grep -Po '"subcategory": "(.\*?)"' douglas_cate.json | sort | uniq -c
grep '/product/' product.json | sort | uniq -c | wc -l

# Get all unique urls with status 503
zcat * | grep '"status": "503"' | grep -Po '"url": "(.\*?)"' | sort | uniq -c

# Extract url from file
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

  • zip and unzip file/dir
# Folder Zip
zip -r <folder.zip> <folder>

# Folder Unzip
unzip <folder.zip>

# File Zip
gzip <filename>

# File Unzip
gunzip <filename>
# Create a new tar archive
tar cvf archive_name.tar dirname/

# Extract from existing tar archive
tar xvf archive_name.tar

Reference