Crontab
crontab
is a useful tool, I really regret that I don’t familar it earlier.
It’s usage really simple, and these two are my frequent using:
crontab -e #edit crontab file that reside in /var/spool/cron
crontab -l #list current crontab job
It basic syntax as follow, also really simple
example of usage:
- delete file foo every minute
* * * * * rm foo
- delete file foo every 15 minutes
15 * * * * rm foo
- delete file foo every beginning of hour
0 * * * * rm foo
- delete file foo every minute after 3 hours
* 3 * * * rm foo
- delete file foo every day at 18:30
30 18 * * * rm foo
- delete file foo every beginning of month
0 0 0 * * rm foo
- delete file foo on beginning of 1st,10th of month
0 0 1,10 * * rm foo
The usage really simple, I now use it to renew my SSL certification and daily update bt-tracker.
tr
tr
is really useful when encountered situation that needs struggle with string.
Three frequency ways of using tr
-
shrink multiple characters into single one
tr -s '[string]'
e.g.echo "ssssssspaaaaace" | tr -s 'sa'
would convert “ssssssspace” to “space” -
delete specific character. I usually use it to delete white space or delimiter
tr -d '[string]'
e.g.echo "blog.garhve.com" | tr -d '.o'
would convert url to “blggarhvecm” -
convert specific character to another one.
tr '[string1]' '[string2]'
e.g.echo "woopwon | tr "wo" "fe"
would result “feepfen”
cut
I use cut
mostly to get word from a string, especially get relative path from absolute path. Because I always want to loop to get same sub-directories file from different main directory, cut
helps a lot.
for now, I only use it one way
echo string | cut -d '[character]' -f position
e.g. echo path/to/most/inner/file | cut -d '/' -f1
this will give me word before first ‘/’, which is ‘path’
Often, rev
will co-work with cut
to get last one word
e.g. echo blog.garhve.com | rev | cut -d '.' -f 1 | rev
this will give word after last ‘/’, which is com.
a worth noting here is that the
rev
command needs to appear twice because it usage is not so intuitive, it reverse whole string
echo "hello world" | rev
will get ‘dlrow olleh`
echo "hello world" | cut -d ' ' -f1 | rev
will give result of ‘dlrow’
echo "hello world" | rev | cut -d ' ' -f1
will give same result, as ‘dlrow’
echo "hello world" | rev | cut -d ' ' -f1 | rev
will give expected result, as ‘world’
Daily update bt tracker
I already learn shell script for a while.. so I wrote a simple script to test whether I really got used to it, but result is obviously, I need more and more practice to memorize commands.
#! /bin/sh
#bt-tracker.txt
site=https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt
file=/path_to_aria_directory/aria2.conf
Addr=user@addr
# Get bt-tracker and format it to fulfill aria needs, then store in variable
bt=$(curl $site | tr -s '[:space:]' | tr '[:space:]' ',')
# sshpass is a software, that can allow me pass ssh password as argument
# so that I don't need to wait prompt
# 410 is the line of bt tracker resides, I now no other way to replace it.
$(sshpass -p 'password' ssh -T $Addr "sed -i '410d' $file | echo $bt >> $file")
Ways to find files or specific string in files
In order to look up C definitions, I need to know where linux stores header files or which files store definitions I need. So that here comes up some methods to fulfill this need:
find 'path' -name 'file_name'
e.g.
find / -name stdio.h
will return multiple location that stdio.h resides. this could expand to look for others
more info could see
man find
grep -rnw 'path' -e 'pattern'
e.g. `grep –include=*.{h,c} -rnw / -e “from_kuid” will return string and filename that contains string.
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the searchAlong with these,
--exclude
,--include
,--exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it’s possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
more info could see man grep.