split files

This commit is contained in:
holm / Christian Müller 2025-04-16 02:45:49 +02:00
parent c3a12e401e
commit 9983273fe1

View file

@ -0,0 +1,470 @@
# holms Shell Spickzettel
[Short Link](https://r6n.it/shell) ::: [Original Share](https://joplin.cloud.nerdraum.de/shares/4GWSqhy0eSKKJmRgbMSihv) ::: [Source (Git/forgejo)](https://forgejo.mueller.network/holm/holms-shell-spickzettel)
## Zusammenschrieb
… und copypasted Shell Standards aller Art.
Überwiegend passend für bash und zsh.
Diese Sammlung wird stets erweitert, ergänzt und korrigiert.
---
[TOC]
---
## Internetressourcen
### Links zu anderen Sammlungen und Dokus
- [Bite Size Bash - Beste Zines von Julia Evans. Meine Gold-Empfehlung. Alle!](https://store.wizardzines.com/products/bite-size-bash)
- [commandlinefu - Shell Snippet Sammlung](https://www.commandlinefu.com/commands/browse)
## kombinierte Listen
### Beispiel 1
```bash
for finger in {left,right}-{thumb,{index,middle,ring,little}-finger};
do fprintd-enroll -f "$finger" "$USER";
done
```
### Beispiel 2
```bash
for finger in {left,right}-{index,middle}-finger;
do fprintd-enroll -f "$finger" "$USER";
done
```
### Beispiel 3
```bash
for richtung in {oben,unten,}{links,rechts,};
do echo "$richtung";
done
obenlinks
obenrechts
oben
untenlinks
untenrechts
unten
links
rechts
```
## File Management / Navigation
### cd back to last directory
~~~
cd -
~~~
### find / rename files in subfolders
~~~bash
find . -maxdepth 4 -name '*.jpg' -execdir mv {} cover.jpg \;
~~~
### find with multiple patterns
#### Alles was **nicht** .deb oder -vmdk heisst:
~~~bash
find ./ ! -regex '.*\(deb\|vmdk\)$'
~~~
### find files by extension and replace strings in it per sed
~~~bash
find . -maxdepth 4 -name '*.json' -exec sed -i s/"Development Version"/"Live On Stage"/ {} \;
~~~
#### Alle .xls, .doc, xlsx files
~~~bash
find ./ -regex '.*\(doc\|xls\|xlsx\)$' -execdir echo {} \;
~~~
### cp / mv multi extensions
```bash
mv *.{png,jpg} ~/Dev
cp foo.conf{,.orig}
```
### touch
#### create File1, File2 ... File9
~~~bash
$ touch file-{1..9}.txt
$ ls
file-1.txt file-2.txt file-3.txt file-4.txt file-5.txt file-6.txt file-7.txt file-8.txt file-9.txt
~~~
### compress / zip / tar / rar
#### entpacke alle `.zip` mit overwrite
~~~bash
for i in *.zip; do echo unzip -o $i; done | sh
~~~
### download file1, file2 ... file2342
~~~bash
$ for i in {1..2342}; do curl -O https://server/path/file-$i.jpg; done
~~~
## rsync
### Eierlegendewollmilchsauinschnell
~~~bash
rsync -aHAXxv --info=progress2 -e "ssh -T -c aes128-ctr -o Compression=no -x" ./ holm@10.1.2.3:~/ --exclude .cache --exclude .local/share/gnome-boxes --exclude ISO --exclude 'vm*.raw
~~~
### archive
copy all files recursive with user information and date
~~~bash
##
rsync -a SRC DST
~~~
### folder vs. content of folder
#### Demo files
~~~bash
$ find SRC
.
./SRC
./SRC/SUBSRC
./SRC/SUBSRC/subsrc-1.dat
./SRC/SUBSRC/subsrc-3.dat
./SRC/SUBSRC/subsrc-2.dat
./SRC/src-1.dat
./SRC/src-3.dat
./SRC/src-2.dat
~~~
#### folder
~~~bash
$ rsync -a SRC DST
$ find DST
.
./DST
./DST/SRC
./DST/SRC/src-1.dat
./DST/SRC/SUBSRC
./DST/SRC/SUBSRC/subsrc-1.dat
./DST/SRC/SUBSRC/subsrc-3.dat
./DST/SRC/SUBSRC/subsrc-2.dat
./DST/SRC/src-3.dat
./DST/SRC/src-2.dat
~~~
#### content
~~~bash
$ rsync -a SRC/ DST
$ find DST
DST
DST/src-3.dat
DST/src-2.dat
DST/src-1.dat
DST/SUBSRC
DST/SUBSRC/subsrc-2.dat
DST/SUBSRC/subsrc-3.dat
DST/SUBSRC/subsrc-1.dat
~~~
### show Progress
#### per File
~~~bash
$ rsync -a -P SRC DST
sending incremental file list
created directory DST
SRC/
SRC/src-1.dat
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
SRC/src-2.dat
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=5/8)
SRC/src-3.dat
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/8)
SRC/SUBSRC/
SRC/SUBSRC/subsrc-1.dat
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=2/8)
SRC/SUBSRC/subsrc-2.dat
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=1/8)
SRC/SUBSRC/subsrc-3.dat
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=0/8)
~~~
#### over all
slows down start of copy process until a complete file list exists
~~~bash
$ rsync -a --info=progress2 SRC DST
86,621,168 99% 139.78MB/s 0:00:00 (xfr#28, to-chk=0/75)
~~~
---
# Bash Zine-Export
## [Bite Size Bash](https://store.wizardzines.com/products/bite-size-bash) zine Mitschrift /
- Allgemeine Dinge
`shellcheck`
* wiki
* shellcheck.net
```bash
"$var" "${var}.png" $?=exit code
$0=scriptname
"$@"=all arguments
shift
inkscape "$1" -b white --export-png="$2"
for i in "$0"
do
done
```
### sudo pipe
***falsch:*** ```sudo echo x > xyz```
***richtig*** ```echo x | sudo tee xyz```
~~~bash
x=$((2+2)) # = 4
a{.png,.svg} # = a.png a.svg
VAR=$(c at x.txt)
x=(1 2 3 4 5)
x={1..5}
y={001..087}
< (command) == command |
${var//search/replace}
[[ $DIR=/home/* ]]
diff <(./command1) <(./command2)
~~~
### for
#### for LINE
~~~bash
for i in *.png
do
convert"$i" "${i/png/jpg}"
done
~~~
#### for WORD
~~~
while bashcommand
do
...
done
# ---
for i in $(seq 1 5)
# or
for i in {1..5}
# ---
read -r text1 text2 | FS=''
# or
echo "$text" | FS=''
~~~
### pipes
~~~bash
mkfifo mypipe
ls > mypipe
wc < mypipe
~~~
### vars
~~~bash
${}
${var} # var
${#var} # length of var
${var:-$othervar} # => wenn var unset/null, dann var=othervar
${var:?Fehlermeldung}
${var#prefix} # entfernt PREFIX
${var%suffix} # entfernt SUFFIX
${var/suche/ersetze} # FIRST
${var//suche/ersetze} # ALLE
${var:offset:length} # subvar
~~~
### trap - cleanup
~~~bash
trap 'kill $(jobs -p)' INT
# killall background processes when ctrl-c
function cleanup() {
rm -rf $TEMPDIR
rm $TEMPFILE
}
trap cleanup EXIT
~~~
### errors
#### stops after exit
~~~bash
set -e # stops after exit
~~~
#### stops the script on unset variables
~~~bash
set -u
~~~
#### makes the pipe fail if any command fails
~~~bash
set -o pipefail
~~~
#### example
~~~bash
set -e
unzip fle.zip # typo in filename
# -> error
# -> stop
~~~
### Debugging
"@ECHO On"
~~~bash
set -x
# or
bash -x script.sh
~~~
## commandlinefu extract
Snippets von [commandlinefu - Shell Snippet Sammlung](https://www.commandlinefu.com/commands/browse)
### kill process by TCP-Port
> In emergency situations, in order not to panic, shut down the following port on the network with the following rather then shutting down the PC.
~~~bash
fuser -k 445/tcp
~~~
wuseman1 · 2024-08-26 19:11:03
### Record a certain command output
~~~bash
script -c "ls -la" logfile.log
~~~
wuseman1 · 2024-08-26 18:34:05
### ffmpeg: Generate GIF from video
> The 30 means start extracting frames from 30 seconds into the video. The 3 means extract the next 3 seconds from that point. The fps can be adjusted based on your preferences. The 320 is the width of the gif, the height will be calculated automatically. input.mp4 is the video file, which can be any video file ffmpeg supports. The output.gif is the gif created.
~~~bash
ffmpeg -ss 30 -t 3 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
~~~
keyboardsage · 2024-03-19 00:34:23
### ´find´ and delete all hidden dot files
#### seek
~~~bash
find ./path_to_dir -type f -name '.*'
~~~
#### and destroy
~~~bash
find ./path_to_dir -type f -name '.*' -exec rm '{}' \;`
~~~
keyboardsage · 2024-03-16 23:47:01
### 7zip hochverdichtet und encrypted
> Create a 7zip archive named "some_directory.7z" and adds to it the directory "some_directory". The `-mhe=on` is for header encryption, basically it mangles the file names so no one knows whats inside the 7z. If -mhe=on wasn't included, then a person without the password would still be able to view the file names inside the 7z. Having this option ensures confidentiality. To ensure the result is small use lzma2, level 9 compression. Lzma2 fast bytes range from 5 to 272, the higher the number the more aggressive it is at finding repetitive bytes that can be added to the dictionary. Here the fast bytes are set to 64 bytes and the dictionary is 32 MB. Depending on your purposes (the directory size and desired file size), you can be more aggressive with these values. Lastly, `-ms=on` just says concatenate all the individual files and treat them as a singular file when compressing. This leads to a higher compression ratio generally.
~~~bash
$ du -sh Dev*
114M Dev
36M Dev.7z
49M Dev.tgz
~~~
~~~bash
7z a -t7z -mhe=on -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on Dev.7z Dev/
~~~
keyboardsage · 2024-03-16 23:36:38
### Disk usage skipping mount points (even top-level ones)
> Other solutions that involve doing du -sx /* are incomplete because they will still descend other top-level filesystems are that mounted directly at "/" because the * expands to explicitly include all files and directories in "/", and du will still traverse them even with -x because you asked it to by supplying the directory name as a parameter (indirectly via "*"). Show Sample Output
~~~bash
for a in /*; do mountpoint -q -- "$a" || du -shx "$a"; done | sort -h
~~~
dmmst19 · 2024-02-28 01:43:19
### $(var) from JSON
> A recursive version might be useful too. /dev/tty is used to show which shell variables just got defined. Show Sample Output
~~~bash
json='{"a":42, "b":"s t r i n g", "c": []}' ; eval $(echo $json | jq -r 'to_entries | .[] | select(.value | scalars) | .key + "=\"" + (.value | tostring) + "\";"' | tee /dev/tty)
~~~
penthief · 2023-09-26 03:11:38
### logge sich ändernde files
> Monitor changed files into a log file, with day rotation, using fswatch (MacOS). This command monitors changes in the current folder structure (subfolders included) and files, and log it into a hidden file in the same folder, called `.file_changes_YYMMDD.log`. Modify the `--exclude` parameters to define what should be skipped. Show Sample Output
~~~bash
fswatch --exclude=.git/* --exclude=.settings --event-flags --event-flag-separator=\; -t -f '%Y-%m-%d %H:%M:%S' . >> ./.file_changes_$(date +"%Y-%m-%d" | sed s/-//g).log
~~~
paulera · 2023-08-17 23:06:30
### Color bars
~~~bash
while :; do for ((i=0;i<$(tput cols);i++));do clear;for ((j=0;j<$(tput lines);j++));do printf "\e[48;5;$((RANDOM%256))m%*s\e[0m\n" $(((j+i)%2?$(tput cols)-i:i)) "";done;sleep 0.05;done;done
~~~
bzw.
~~~bash
while :; do printf "\e[48;2;$((RANDOM % 256));$((RANDOM % 256));$((RANDOM % 256))m%*s\e[0m" $(tput cols) ""; sleep 0.1; done
~~~
wuseman1 · 2023-07-04 00:47:37
### ls sort by git commit
> This lists all the files in a folder, then finds the commit date for them one by one, then sorts them from newest to oldest
~~~bash
git ls-tree --name-only HEAD foldername/ | while read filename; do echo "$(git log -1 --format="%ci " -- $filename) $filename"; done | sort -r
~~~
fivestones · 2023-03-01 17:02:51
### Copy a file with progress and save hash to a different file
~~~bash
pv file.txt | tee >(sha1sum > file.sha1) > file-copy.txt
~~~
bugmenot · 2022-11-24 20:23:02