1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
+++
title = "Mess with bash(1)"
date = 2022-08-25
[taxonomies]
categories = ["code"]
tags = ["bash"]
[extra]
math = false
+++
### 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:
```bash
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:
1. delete file *foo* every minute
`* * * * * rm foo`
2. delete file *foo* every 15 minutes
`15 * * * * rm foo`
3. delete file *foo* every beginning of hour
`0 * * * * rm foo`
4. delete file *foo* every minute after 3 hours
`* 3 * * * rm foo`
5. delete file *foo* every day at 18:30
`30 18 * * * rm foo`
6. delete file *foo* every beginning of month
`0 0 0 * * rm foo`
7. 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`
1. **shrink** multiple characters into single one
`tr -s '[string]'`
e.g. `echo "ssssssspaaaaace" | tr -s 'sa'` would convert "ssssssspace" to "space"
2. **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"
3. **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.
```bash
#! /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")
```
> Usage of [tr](https://blog.garhve.com/index.php/archives/23/#tr) and [cut](https://blog.garhve.com/index.php/archives/23/#cut), expansion of 'command tips' is needed
### 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:
1. `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`
---
2. `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 search
>
> Along 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:
>
> ```bash
> grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
> ```
>
> * This will exclude searching all the files ending with .o extension:
>
> ```bash
> 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/:
>
> ```bash
> grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
> ```
>
> more info could see [man grep](https://ss64.com/bash/grep.html).
|