lk/system/bash_tricks.md

117 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2022-01-16 18:20:39 +00:00
---
2023-09-16 16:10:04 +00:00
title: "bash tips"
tags: [ "Documentation", "Shell", "POSIX" ]
2022-01-16 18:20:39 +00:00
---
2023-06-17 17:02:28 +00:00
## Track Live Changes
2022-11-28 22:50:53 +00:00
See changes in a file as it changes:
2023-06-17 17:02:28 +00:00
`tail -f *somefile*`
2022-11-28 22:50:53 +00:00
See changes in a directory, as it changes:
2023-06-17 17:02:28 +00:00
`watch -d ls *directory*`
2020-01-02 00:04:35 +00:00
2023-09-08 17:11:08 +00:00
Or use the `-g` flag to exit once the output changes.
This command will look at whether you're connected to the internet, and turn into a rainbow once the connection hits.
> watch -g ip address && clear && ip address | lolcat
2023-06-17 17:02:28 +00:00
## Automatic Renaming
2020-01-02 00:04:35 +00:00
There are a bunch of files:
* Column CV.aux
* Column CV.log
* Column CV.out
* Column CV.pdf
* Column CV.tex
* tccv.cls
2022-11-28 22:50:53 +00:00
Goal: swap the word "Column" for "Alice" in all files.
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
```
IFS=$'\n'
for f in $(find . -name "Col*"); do
mv "$f" $(echo "$f" | sed s/Column/Alice/)
done
```
2020-01-02 00:04:35 +00:00
IFS is the field separator. This is required to denote the different files as marked by a new line, and not the spaces.
2023-09-08 17:11:08 +00:00
(Alternatively, just install `renameutils` and do `rename Column Alice *`)
2023-06-17 17:02:28 +00:00
## Arguments and Input
2020-01-02 00:04:35 +00:00
The `rm' program takes arguments, but not `stdin' from a keyboard, and therefore programs cannot pipe results into rm.
2023-09-08 17:11:08 +00:00
To fix this, use `xargs` to turn the stdin into an argument.
For example, if we have a list of files called `list.txt' then we could use cat as so:
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
```bash
cat list.txt | xargs rm
```
2020-01-02 00:04:35 +00:00
2023-09-08 17:11:08 +00:00
Of course if spaces are included in the file, you would have to account for that.
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
## Numbers
2020-01-02 00:04:35 +00:00
Add number to variables with:
2023-06-17 17:02:28 +00:00
* `let "var=var+1"`
* `let "var+=1"`
* `let "var++"`
* `((++var))`
* `((var=var+1))`
* `((var+=1))`
* `var=$(expr $var + 1)`
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
`((n--))` works identically.
2020-01-02 00:04:35 +00:00
2023-09-16 16:10:04 +00:00
### POSIX WARNING
The number commands above work in `bash`, but not in bare-ass POSIX shells, such as `dash`.
Instead, you might do:
```sh
x=2
x=$(( x +1 ))
x=$(( x*x ))
```
2023-06-17 17:02:28 +00:00
## Finding Duplicate Files
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
```bash
find . -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 15 > all-files.txt
```
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
## Output random characters
2020-01-02 00:04:35 +00:00
2023-06-17 17:02:28 +00:00
```bash
cat /dev/urandom | tr -cd [:alnum:] | dd bs=1 count=200 status=none && echo
```
2023-09-08 17:11:08 +00:00
## Temporary Working Directory
Try something out in a random directory in `/tmp` so the files will be deleted when you next shut down.
```bash
mktemp -d
```
That gives you a random directory to mess about in.
```bash
dir=$(mktemp -d)
for x in {A..Z}; do
fortune > "$dir"/chimpan-$x
done
cd $dir
```
2023-09-16 16:10:04 +00:00
### POSIX WARNING
These smart-brackets are a bash feature.
If you try to use `{A..Z}` in dash, it will think of this as a single item.