lk/system/bash_tricks.md

74 lines
1.5 KiB
Bash
Raw Permalink Normal View History

2022-01-16 18:20:39 +00:00
---
2023-06-17 17:02:28 +00:00
title: "Terminal Tips"
2022-01-26 22:35:07 +00:00
tags: [ "Documentation", "System" ]
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-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-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.
That said, we can sometimes pipe into rm with `xargs rm' 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:
2023-06-17 17:02:28 +00:00
```bash
cat list.txt | xargs rm
```
2020-01-02 00:04:35 +00:00
... *However*, this wouldn't work if spaces were included, as rm would take everything literally.
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-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
```