25-03-2025

This commit is contained in:
2025-03-25 15:52:48 +01:00
parent 259b9c6a24
commit 011cfcba40
64 changed files with 2993 additions and 45 deletions

View File

@@ -90,7 +90,39 @@ fi
### Conditions:
#### -fichier
#### -variables:
Si la variable est déclarée (présente) : -v
```bash
API_KEY=
if [ -v API_KEY ]; then echo "La variable API_KEY existe"; fi
La variable API_KEY existe
if [ -v $API_KEY ]; then echo "La variable API_KEY existe"; fi
La variable API_KEY existe
```
```bash
# Déclarée => vide
if [ -n "$API_KEY" ]; then echo "La variable API_KEY n'est pas vide"; else echo "La variable API_KEY est vide"; fi
La variable API_KEY est vide
# Non déclarée => vide
if [ -n "$API_KEY2" ]; then echo "La variable API_KEY2 n'est pas vide"; else echo "La variable API_KEY2 est vide"; fi
La variable API_KEY2 est vide
```
```bash
# Non déclarée => vide
if [ -z "$API_KEY3" ]; then echo "La variable API_KEY est vide"; else echo "La variable API_KEY n'est pas vide"; fi
La variable API_KEY est vide
```
#### -dossiers:
Si le répertoire *<u>directory</u>* existe
@@ -110,6 +142,8 @@ if find "$local_path/node_modules" -mindepth 1 -maxdepth 1 | read; then echo "di
if [ -d "$local_path/node_modules" ] && [ -n "$(ls -A "$local_path/node_modules")" ]; then echo "dir not empty"; else echo "dir empty"; fi
```
#### -fichiers:
Si le fichier *<u>regularfile</u> (ni un blockspecialfile, ni un characterspecialfile, ni un directory)* existe
```bash

View File

@@ -14,7 +14,7 @@
------------- Minute (0 - 59)
```
https://crontab.guru
@@ -48,3 +48,32 @@ Le fichier crontab s'ouvre dans l'éditeur spécifié par la variable d'environn
https://linuxize.com/post/scheduling-cron-jobs-with-crontab/
#### Voir si le cron est exécuté:
On ouvre la crontab avec `# crontab -e`:
```bash
# On redirige la sortie et les erreurs du scripts vers les logs avec 2>&1 | logger -t up_pihole
# Ici le cron est exécuté toutes les 2 minutes.
*/2 * * * * bash /root/update_pihole.sh 2>&1 | logger -t up_pihole
```
On cherche les sorties du cron dans les logs:
```bash
# journalctl -f | grep 'up_pihole\|cron'
```
```bash
Aug 25 16:40:01 PiHole1 CRON[438003]: (root) CMD (/usr/bin/env > /root/cron-env)
Aug 25 16:40:01 PiHole1 CRON[438004]: (root) CMD (bash /root/update_pihole.sh 2>&1 | logger -t up_pihole)
Aug 25 16:40:01 PiHole1 CRON[438001]: pam_unix(cron:session): session closed for user root
Aug 25 16:40:02 PiHole1 up_pihole[438007]: Find Pi-hole update on PiHole1
Aug 25 16:40:02 PiHole1 up_pihole[438007]:
Aug 25 16:40:02 PiHole1 up_pihole[438007]: Pi-hole Current Last
Aug 25 16:40:02 PiHole1 up_pihole[438007]: Pi-hole v5.18.3 v5.18.3
```

81
docs/Linux/curl.md Normal file
View File

@@ -0,0 +1,81 @@
# curl
#
#### Envoyer un fichier sur un ftp (Photomanager iPad):
```bash
curl -T /Users/bruno/Pictures/_Canon/Export/2023/Janvier/2023-01-21_FauconPelerin_0518.jpg ftp://192.168.2.112:2121 -u anonymous:
```
#### Envoyer plusieurs fichiers sur un ftp (funnymac):
```bash
find /Users/bruno/Pictures/_Canon/Export/2023/Janvier/ -type f | xargs -L 1 bash -c '/opt/homebrew/opt/curl/bin/curl --user funnymac:a3XELN4PHTYF9fFHtsY97eshXBxxFo -T $1 sftp://ftp.cluster011.ovh.net:22/home/funnymac/www/ftp/${1##*/}' \;
```
```bash
for file in /folder/path/*
do
curl -u username:password -T ${file} http://www.example.com/folder/${file}
done
```
```bash
# Si le dossier juin n'existe pas sur le ftp, on le crée:
find /Users/bruno/Desktop/Juin -type f | xargs -L 1 bash -c '/opt/homebrew/opt/curl/bin/curl --user funnymac:a3XELN4PHTYF9fFHtsY97eshXBxxFo --ftp-create-dirs -T $1 sftp://ftp.cluster011.ovh.net:22/home/funnymac/www/ftp/juin/${1##*/}' \;
```
#### Télécharger un fichier depuis un ftp (funnymac):
```bash
/opt/homebrew/opt/curl/bin/curl --user funnymac:a3XELN4PHTYF9fFHtsY97eshXBxxFo -O sftp://ftp.cluster011.ovh.net:22/home/funnymac/8_2022.jpg
```
#### Télécharger plusieurs fichiers depuis un ftp (funnymac):
```bash
/opt/homebrew/opt/curl/bin/curl --user funnymac:a3XELN4PHTYF9fFHtsY97eshXBxxFo -T '{2023-01-31_RuBlanc_0627,2023-01-31_RuBlanc_0623}.jpg' sftp://ftp.cluster011.ovh.net:22/home/funnymac/www/ftp/ \;
```
#### Rendre curl silencieux:
##### Masquer erreurs et barre de progression:
```bash
curl -s https://google.com
```
##### Complètement silencieux:
```bash
curl -s -o /dev/null https://google.com
```
##### Afficher juste les erreurs:
```bash
curl -S -s -o /dev/null https://google.com
```
#### Options:
```
-Z, --parallel
-#, --progress-bar
-Q, --quote <command>
-S, --show-error
-s, --silent
```

View File

@@ -8,3 +8,58 @@
```bash
# -d (delimiter) :
# -f (field) 1
$ cut -d':' -f1 /etc/passwd
nobody
root
daemon
_uucp
_taskgated
_networkd
cut -d':' -f1-3,5,6 /etc/passwd
```
```bash
# Tout sauf 7e champ
$ cut -d':' -f7 --complement /etc/passwd
```
```bash
# Remplace le séparateur ':' par ' '
$ cut -d':' -f7 --complement /etc/passwd --output-delimiter=' '
```
```bash
# 5e caractère
$ echo 'cut command' | cut -b 5
c
# 5 au 7e caractères
$ echo 'cut command' | cut -b 5-7
com
# 5 et 7e caractères
$ echo 'cut command' | cut -b 5,7
cm
# Du 5e à la fin
$ echo 'cut command' | cut -b 5-
command
# Du début au 5e
$ echo 'cut command' | cut -b -5
cut c
```

View File

@@ -32,6 +32,8 @@ pip/pip.conf
##### Recherche dans un répertoire particulier:
```bash
# Fichiers cachés (-H) dans le dossier .ssh
$ fd -HI 'id_*' .ssh
.ssh/id_ed25519
.ssh/id_ed25519.pub
@@ -72,6 +74,21 @@ $ fd -HI '.*[0-9]\.jpg$' ~
$ find ~ -iname '*[0-9].jpg'
```
##### Rechercher une extension:
```bash
# Rechercher les scripts bash (.sh) dans le répertoire courant
$ fd -e sh .
convert-videos-for-plex.sh
handbrake_for_plex.sh
keywords2insta.sh
macho.sh
```
##### Sans arguments:
```bash
@@ -96,6 +113,14 @@ $ fd -I -g php.ini /opt
/opt/homebrew/etc/php/8.0/php.ini
```
##### Rechercher plusieurs patterns:
```bash
$ fd -H ".env|docker-compose.yml"
.env
docker-compose.yml
```
#### Option:

View File

@@ -213,6 +213,20 @@ $ find /volume1/@appstore/PHP7.4/etc ! -perm 644
/volume1/@appstore/PHP7.4/etc/php/conf.d
```
Rechercher les fichiers avec permission 644 et les afficher avec ls:
```bash
$ find -maxdepth 1 -type f -perm -644 -ls
920871 148 -rw-r--r-- 1 sentier psacln 148546 Nov 14 16:37 ./12_2008.jpg
920718 276 -rw-r--r-- 1 sentier psacln 278540 Nov 14 16:37 ./7_2017.jpg
920675 120 -rw-r--r-- 1 sentier psacln 120837 Nov 14 16:37 ./5_2020.jpg
$ find -maxdepth 1 -type f -perm -644 -exec ls -la {} \;
-rw-r--r-- 1 sentier psacln 89608 Nov 14 16:37 ./11_2018.jpg
-rw-r--r-- 1 sentier psacln 258835 Nov 14 16:37 ./9_2007.jpg
-rw-r--r-- 1 sentier psacln 343441 Nov 14 16:37 ./7_2005.jpg
```
Rechercher les fichiers avec permission 777 et les modifiés en 644:
```bash

View File

@@ -228,6 +228,10 @@ https://reposhub.com/linux/shell-applications/lincheney-fzf-tab-completion.html
### Python
#### Activer un venv:
```bash
function activate-venv() {
local selected_env
@@ -239,3 +243,184 @@ function activate-venv() {
}
```
### Git
#### Git commit history
```bash
git log --oneline | fzf --preview 'git show --name-only {1}'
```
### Navigateurs
#### Recherche dans l'historique de Firefox:
```bash
cd ~/Library/Application\ Support/Firefox/Profiles/*.default-release
sqlite3 places.sqlite "SELECT url FROM moz_places" | fzf
```
#### Recherche dans les bookmarks de chrome:
```bash
b() {
bookmarks_path=~/Library/Application\ Support/Google/Chrome/Default/Bookmarks
jq_script='
def ancestors: while(. | length >= 2; del(.[-1,-2]));
. as $in | paths(.url?) as $key | $in | getpath($key) | {name,url, path: [$key[0:-2] | ancestors as $a | $in | getpath($a) | .name?] | reverse | join("/") } | .path + "/" + .name + "\t" + .url'
jq -r "$jq_script" < "$bookmarks_path" \
| sed -E $'s/(.*)\t(.*)/\\1\t\x1b[36m\\2\x1b[m/g' \
| fzf --ansi \
| cut -d$'\t' -f2 \
| xargs open
}
```
#### Recherche dans l'historique de Safari:
```bash
function sbh() {
local cols sep
cols=$(( COLUMNS / 3 ))
sep='{::}'
cp -f ~/Library/Safari/History.db /tmp/h
sqlite3 -separator $sep /tmp/h \
"select substr(id, 1, $cols), url
from history_items order by visit_count_score desc" |
awk -F $sep '{printf "%-'$cols's \x1b[36m%s\x1b[m\n", $1, $2}' |
fzf --ansi --multi | sed 's#.*\(https*://\)#\1#' | xargs open
}
fzf-safari-browser-history()
{
local cols sep
columns=$(( COLUMNS / 3 ))
separator='{::}'
sqlite3 -separator $separator $HOME/Library/Safari/History.db \
"select distinct substr(title, 1, $columns), url from history_items
inner join history_visits on history_items.id = history_visits.history_item
order by history_visits.visit_time desc;" |
awk -F $separator '{printf "%-'$columns's \x1b[36m%s\x1b[m\n", $1, $2}' |
fzf --ansi --multi | sed 's#.*\(https*://\)#\1#' | xargs open -a safari
}
```
### Terminal
#### Kill process:
```bash
kill -9 $(ps aux | fzf | awk '{print $2}')
```
#### File preview
```bash
fzf --preview 'bat --style=numbers --color=always --line-range :500 {}'
```
```bash
fd . '/opt/homebrew' | fzf --height=90% --reverse --preview 'cat {}' --query '_log'
```
### Docker
```bash
# Select a docker container to start and attach to
function da() {
local cid
cid=$(docker ps -a | sed 1d | fzf -1 -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker start "$cid" && docker attach "$cid"
}
```
```bash
# Select a running docker container to stop
function ds() {
local cid
cid=$(docker ps | sed 1d | fzf -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker stop "$cid"
}
```
```bash
# Select a docker container to remove
function drm() {
local cid
cid=$(docker ps -a | sed 1d | fzf -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker rm "$cid"
}
# Same as above, but allows multi selection:
function drm() {
docker ps -a | sed 1d | fzf -q "$1" --no-sort -m --tac | awk '{ print $1 }' | xargs -r docker rm
}
```
```bash
# Select a docker image or images to remove
function drmi() {
docker images | sed 1d | fzf -q "$1" --no-sort -m --tac | awk '{ print $3 }' | xargs -r docker rmi
}
```
### Homebrew Cask
```bash
# Install or open the webpage for the selected application
# using brew cask search as input source
# and display a info quickview window for the currently marked application
install() {
local token
token=$(brew search --casks "$1" | fzf-tmux --query="$1" +m --preview 'brew info {}')
if [ "x$token" != "x" ]
then
echo "(I)nstall or open the (h)omepage of $token"
read input
if [ $input = "i" ] || [ $input = "I" ]; then
brew install --cask $token
fi
if [ $input = "h" ] || [ $input = "H" ]; then
brew home $token
fi
fi
}
```
#### fzf-brew
```bash
antigen bundle thirteen37/fzf-brew
fbi: Fuzzy brew install
fbui: Fuzzy brew uninstall
fci: Fuzzy cask install
fcui: Fuzzy cask uninstall
```
https://github.com/thirteen37/fzf-brew?tab=readme-ov-file

View File

@@ -155,3 +155,9 @@ Afficher les 5 lignes qui suivent le motif recherché:
grep 'toto' -A5 fichier.txt
```
Afficher les 5 lignes qui précèdent le motif recherché:
```bash
grep 'toto' -B5 fichier.txt
```

View File

@@ -26,10 +26,21 @@ hello.txt link
```bash
~ ln -s link/hello.txt hello_you.txt
~ ls
~
$ ls
hello.txt link hello_you.txt
```
```bash
~/.local/bin
$ ln -s ~/Documents/Scripts/pihole/sync_pihole_lan.sh sync_pihole
```
```bash
~/.local/bin
$ ln -s ~/Documents/Scripts/bashbirds/bashbirds.sh bashbird
```
### hard link:

View File

@@ -136,3 +136,8 @@ PING 192.168.1.8 (192.168.1.8): 56 data bytes
bck-i-search: nas_
```
### Interactive shell
https://unix.stackexchange.com/questions/46789/check-if-script-is-started-by-cron-rather-than-invoked-manually

View File

@@ -28,12 +28,24 @@ bruno@macbook-pro:~$ sudo chmod 755 ~/.ssh
#### Copier un fichier:
#### Copier un fichier depuis le serveur:
```bash
macbook-pro:~ bruno$ ssh root@192.168.1.8 "cat prefs.tar.gz"> prefs.tar.gz
```
```bash
$ ssh pihole1 "cat update_pihole.sh"> update_pihole_pi1.sh
$ ssh pihole2 "cat update_pihole.sh"> update_pihole_pi2.sh
```
#### Envoyer un fichier sur le serveur:
```bash
$ cat update_pihole_pi1.sh | ssh pihole1 'cat > update_pihole.sh'
$ cat update_pihole_pi2.sh | ssh pihole2 'cat > update_pihole.sh'
```
#### Exécuter une (ou plusieurs) commande sur un serveur distant: