Skip to content

Commit b372c1c

Browse files
committed
docs: update notes
1 parent 09d8d51 commit b372c1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1468
-351
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[![Netlify Status](https://api.netlify.com/api/v1/badges/8e7bba5d-546f-4672-9b3b-a6efbecf7fa4/deploy-status)](https://app.netlify.com/sites/rsapkf-notes/deploys)
66

7-
These are companion notes to my [dotfiles](https://github.com/rsapkf/config) and [links](https://github.com/rsapkf/42/) where I keep short snippets of code, shell scripts, tricks and tips to remember stuff.
7+
These are companion notes to my [dotfiles](https://github.com/rsapkf/config) and [links](https://github.com/rsapkf/42/) where I keep short snippets of code, shell scripts, tricks and tips related to common Linux utilities/concepts, mathematical topics, Martin Gardner's mathemagic tricks, poems that I like, notes from books I've read, etc.
88

99
Notes are in the [`docs/`](https://github.com/rsapkf/73/tree/main/docs) directory.
1010

docs/linux/awk.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ awk -F: '{print $1;}' /etc/passwd # Equivalent to above
4747
awk 'BEGIN { FS=":"; print "User\t\tUID\t\tGID\t\tHome\t\tShell\n--------------"; }
4848
{print $1,"\t\t",$3,"\t\t",$4,"\t\t",$6,"\t\t",$7;}
4949
END { print "---------\nFile Complete" }' /etc/passwd
50-
5150
```
5251

5352
## Field searching
5453

5554
```shell
5655
awk '$2 ~ /^bin/' <file> # Print lines where the second field starts with `bin`
57-
awk '$2 !~ /^sa/' <file> # The second field does _not_ start with `bin`
58-
awk '$2 !~ /^sa/ && $1 < 5' <file> # The second field does not start with `bin` _and_ the value of the first column is less than 5
56+
awk '$2 !~ /^bin/' <file> # The second field does _not_ start with `bin`
57+
awk '$2 !~ /^bin/ && $1 < 5' <file> # The second field does not start with `bin` _and_ the value of the first column is less than 5
5958
```
6059

6160
## Scripting

docs/linux/curl.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ curl -L https://github.com/robots.txt # Follow redirects
1515
curl https://api.github.com/repos/3b1b/manim # Basic GET request to the GitHub API
1616
curl -i https://github.com/robots.txt # Include HTTP-header in the output
1717
curl -I https://github.com/robots.txt # Fetch HTTP-header only
18+
curl -C - -O http://example.org/archive.tar.gz # Resume interrrupted download
1819

19-
# -u, --user: Specify the user name and password to use for server authentication
20+
# -u, --user: Specify the user name and password for server authentication
2021
curl -u <username:password> https://api.example.com/endpoint # Basic authentication
2122
curl -u <username> https://api.example.com/endpoint # Prompt for password; Avoid having password in history
2223

@@ -34,18 +35,18 @@ curl -d @data.json <URL> # Read data to POST from file
3435

3536
# -H, --header: Specify headers
3637
curl -H "Content-Type: application/json" -H "Authorization: <token>" -d @data.json <URL>
37-
# Can be used multiple times to add/replace/remove multiple headers.
38+
# Can be used multiple times to add/replace/remove multiple headers
3839

3940
# -b, --cookie: Pass the data to the HTTP server as a cookie
4041
curl -b "name=Fry" <URL>
4142
curl --dump-header <file> <URL> # Save cookies received from the server to file
4243
curl -b <file> <URL> # Use cookies from file
43-
curl -c <file> <URL> # Save the incoming cookies using netscape cookie format
44+
curl -c <file> <URL> # Save the incoming cookies using Netscape cookie format
4445

4546
# -x, --proxy: Use the specified HTTP proxy and port number (default: 1080)
4647
curl -u <username:password> -x <proxyhost[:port]> <URL>
4748

48-
# -U, --proxy-user: Specify the user name and password to use for proxy authentication
49+
# -U, --proxy-user: Specify the user name and password for proxy authentication
4950
curl -U <username:password> -x <proxyhost[:port]> <URL>
5051

5152
# -v, --verbose: Make the fetching more verbose/talkative

docs/linux/find.md

Lines changed: 141 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,31 @@ find \! -name "query_to_avoid"
1919
```shell
2020
find -type <type_descriptor> <query> # Basic usage
2121
# Common type descriptors:
22-
# f: file
23-
# d: directory
24-
# l: symlink
25-
# c: character device
26-
# b: block device
22+
# f: File
23+
# d: Directory
24+
# l: Symlink
25+
# c: Character device
26+
# b: Block device
2727

2828
find /usr -type f -name "*.conf" # Search for files ending in `.conf` in `/usr` directory
2929
find /usr -type f -and -name "*.conf" # Equivalent to above; `-and` combines two queries
3030
find -name query_1 -or -name query_2 # `-or` returns results that match either expression
31-
3231
```
3332

3433
### Find by size
3534

3635
```shell
3736
find -size <number><suffix> # Basic usage
3837
# Common suffixes
39-
# c: bytes
40-
# k: kilobytes
41-
# M: megabytes
42-
# G: gigabytes
38+
# c: Bytes
39+
# k: Kilobytes
40+
# M: Megabytes
41+
# G: Gigabytes
4342
# b: 512-byte blocks
4443

4544
find /usr -size 50c # Find all files in `/usr` exactly 50 bytes in size
4645
find /usr -size -50c # Less than 50 bytes in size
4746
find /usr -size +600M # More than 600 megabytes in size
48-
4947
```
5048

5149
### Find by time
@@ -80,7 +78,7 @@ find / -perm 644
8078
find / -perm -644 # Files with at least these permisions
8179
```
8280

83-
### Filter by depth
81+
### Find by depth
8482

8583
```shell
8684
find -mindepth <num>
@@ -89,9 +87,25 @@ find -maxdepth <num>
8987

9088
### Execute commands on `find` results
9189

90+
Other commands can be executed on results returned by `find` using the `-exec`/`-delete` options or by piping the output to the [`xargs`](./xargs) command or [GNU Parallel](<https://en.wikipedia.org/wiki/Parallel_(software)>).
91+
9292
```shell
9393
find <find_parameters> -exec <command_and_options> {} \;
9494
find . -type d -perm 755 -exec chmod 700 {} \; # Change directory permissions from 755 to 700
95+
find . -name "*.json" -delete # Delete files ending in `.json`
96+
97+
# Useful options
98+
# -exec command {} +: Build command by appending each selected file name at the end, and then execute it
99+
# -execdir: Run command from the subdirectory containing the matched file
100+
101+
find ./docs -type f -print | xargs rm
102+
# Find all files in `./docs` and remove them
103+
104+
find . -name "*.foo" -print0 | xargs -0 grep bar
105+
# Use the null character to delimit file names (necessary for dealing with filenames with `,` or space)
106+
107+
find . -name "*.foo" -print0 | parallel -0 grep bar
108+
# Equivalent to above
95109
```
96110

97111
## `locate`
@@ -110,3 +124,118 @@ locate -i readme.md # Case-insensitive search
110124
locate -c *.md # Display number of found entries
111125
locate -S # Print statistics about each used database
112126
```
127+
128+
## `whereis`
129+
130+
`whereis` command can be used to efficiently locate the binary, source, and manual page files for a command.
131+
132+
```shell
133+
> whereis rg
134+
rg: /usr/bin/rg /usr/share/man/man1/rg.1.gz
135+
```
136+
137+
## `which`
138+
139+
`which` searches for the binary for a command in your `PATH`.
140+
141+
```shell
142+
> which rg
143+
/usr/bin/rg
144+
```
145+
146+
## `fd`
147+
148+
### Basic usage
149+
150+
```shell
151+
fd [options] <pattern> <path> # Basic usage
152+
fd # List all files in current directory recursively, similar to `ls -R`
153+
154+
fd netfl # Basic search; Recursively search current directory for the pattern `netfl`
155+
fd '^x.*rc$' # Regex search; Search for entries that start with `x` and end with `rc`
156+
fd '^x.*rc$' /etc # Search in `/etc` directory
157+
fd -g libc.so /usr # Glob-based search; Find all `libc.so` files in `usr`
158+
159+
# Useful options
160+
# -H, --hidden: Search in hidden directories
161+
# -I, --no-ignore: Search directories and show files that match `.gitignore` patterns
162+
# -p, --full-path: Search in full paths instead of just filenames
163+
# -l, --list-details: Use a long listing format with file metadata
164+
# -L, --follow: Follow symbolic links
165+
```
166+
167+
### Find by type
168+
169+
```shell
170+
fd -t <filetype> <pattern>
171+
# Common filetypes:
172+
# f: File
173+
# d: Directory
174+
# l: Symlink
175+
# x: Executable
176+
# e: Empty
177+
# s: Socket
178+
# p: Pipe
179+
```
180+
181+
### Find by size
182+
183+
```shell
184+
fd -s <size> <pattern>
185+
```
186+
187+
### Find by time
188+
189+
```shell
190+
fd --changed-within <date|dur> <pattern> # Filter by file modification time (newer than)
191+
fd --changed-before <date|dur> <pattern> # Filter by file modification time (older than)
192+
```
193+
194+
### Find by depth
195+
196+
```shell
197+
fd -d <depth> <pattern> # Set maximum search depth
198+
```
199+
200+
### Find by extension
201+
202+
```shell
203+
fd -e json # Find files with `.json` extension
204+
fd -e json <pattern> # Find json files that contain the pattern
205+
```
206+
207+
### Exclude files and directories
208+
209+
```shell
210+
fd -E '*.zip' <pattern> # Exclude zip files
211+
fd -H -E .git <pattern> # Search in hidden dirs but exclude matches from `.git`
212+
# You can create a `.fdignore` file (similar to `.gitignore`) to make exclude-patterns permanent.
213+
```
214+
215+
### Execute commands on `fd` results
216+
217+
```shell
218+
# -x, --exec <cmd>: Execute a command for each search result
219+
# -X, --exec-batch <cmd>: Execute a command with all search results at once
220+
221+
fd -e zip -x unzip # Recursively find and unzip all zip archives
222+
fd -e h -e cpp -x clang-format -i # Find all `*.h` and `*.cpp` files and auto-format them inplace with `clang-format -i`
223+
224+
fd -g 'test_*.py' -X vim # Find all `test_*.py` files and open them with `vim`
225+
# https://vimhelp.org/usr_07.txt.html#07.2
226+
227+
fd -H '^\.DS_Store$' -tf -X rm # Recursively remove all `.DS_Store` files
228+
```
229+
230+
#### Placeholder tokens
231+
232+
```shell
233+
fd -e jpg -x convert {} {.}.png # Convert all `*.jpg` files to `*.png` files
234+
# `{}` will be replaced by the path of the search result (`docs/images/party.jpg`)
235+
236+
# Other placeholder tokens
237+
# {.}: Like {}, but without the file extension (`docs/images/party`)
238+
# {/}: The basename of the search result (`party.jpg`)
239+
# {//}: The parent of the discovered path (`docs/images`)
240+
# {/.}: The basename without the extension (`party`)
241+
```

docs/linux/fzf.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: fzf
3+
title: fzf
4+
---
5+
6+
`fzf` is a command line fuzzy finder. It can search through the entries in a list of files, command history, processes, hostnames, bookmarks, git commits, etc.
7+
8+
`fzf` provides 3 keyboard shortcuts:
9+
10+
- **`CTRL-T`** to paste the selected **files and directories** onto the command-line.
11+
12+
By default, `fzf` uses `find` to search for files. This can be configured setting `FZF_CTRL_T_COMMAND` in `.bashrc`/`,zshrc`:
13+
14+
```shell title="~/.zshrc"
15+
# Set fd as the default source for fzf
16+
export FZF_DEFAULT_COMMAND='fd -type f --follow --hidden --exclude .git'
17+
18+
# Apply the command to CTRL-T as well
19+
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
20+
21+
# Preview contents of files
22+
export FZF_CTRL_T_OPTS="--preview '(highlight -O ansi -l {} 2> /dev/null || cat {} || tree -C {}) 2> /dev/null | head -200'"
23+
24+
# Set color scheme to Dracula
25+
export FZF_DEFAULT_OPTS=$FZF_DEFAULT_OPTS'
26+
--color=dark
27+
--color=fg:-1,bg:-1,hl:#5fff87,fg+:-1,bg+:-1,hl+:#ffaf5f
28+
--color=info:#af87ff,prompt:#5fff87,pointer:#ff87d7,marker:#ff87d7,spinner:#ff87d7
29+
'
30+
```
31+
32+
- **`CTRL-R`** to paste the selected **command from history** onto the command-line.
33+
34+
Pressing `CTRL_R` again will show the commands in chronological order.
35+
36+
- **`ALT-C`** to **cd** into the selected directory.
37+
38+
```shell title="~/.zshrc"
39+
# Use fd here as well
40+
export FZF_ALT_C_COMMAND="fd -t d"
41+
42+
# Preview files
43+
export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -200'"
44+
```
45+
46+
To start the finder in a tmux split pane:
47+
48+
```shell title="~/.bashrc"
49+
export FZF_TMUX=1
50+
```

docs/linux/git.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ git commit --amend -m "New commit message" # Edit the last commit message
2525
- Get a list of the deleted files in history
2626

2727
```shell
28-
## without commit hashes
29-
git log --diff-filter=D --summary | grep delete
28+
## without commit hashes
29+
git log --diff-filter=D --summary | grep delete
3030

31-
## with commit hashes
32-
git log --diff-filter=D --summary | grep -E 'delete|^commit\s+\S+'
31+
## with commit hashes
32+
git log --diff-filter=D --summary | grep -E 'delete|^commit\s+\S+'
3333
```
3434

3535
- Amend older or multiple commit messages

docs/linux/grep.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ grep "^[[:upper:]]" <file> # Equivalent to above; Using POSIX classes
3030

3131
### Extended regular expressions
3232

33-
Along with basic regular expressions (BREs, which is the default), `grep` also supports [extended regular expressions (EREs)](https://en.wikipedia.org/wiki/Regular_expression#Standards) by using the `-E` flag or by calling the `egrep` command.
33+
Along with basic regular expressions (BREs, which is the default), `grep` also supports [extended regular expressions (EREs)](https://en.wikipedia.org/wiki/Regular_expression#Standards) by using the `-E` option or by calling the `egrep` command.
3434

3535
```shell
3636
grep "\(group\)" <file> # Grouping

0 commit comments

Comments
 (0)