# Bash (exemples)



#### Ajouter un préfixe à la ligne d'un fichier texte:

```bash
brew list | sed -e 's/^/brew install /' > brew-list.txt
```

Pour un suffixe:

```bash
sed -e 's/$/suffix/'
```



#### Cherche tous les .jpg d'un répertoire et les effacer:

```bash
find . -name '*jpg' -exec rm {} +
```



#### Concaténer tous les .csv:

```bash
find . -type f -iname "*.csv" -exec cat {} \; > toutes.txt
```



#### Couleurs dans le terminal:

```bash
export UNDERLINE=$(tput sgr 0 1)
export BOLD=$(tput bold)
export BLACK=$(tput setaf 0)
export RED=$(tput setaf 1)
export GREEN=$(tput setaf 2)
export YELLOW=$(tput setaf 3)
export BLUE=$(tput setaf 4)
export PURPLE=$(tput setaf 5)
export CYAN=$(tput setaf 6)
export WHITE=$(tput setaf 7)
export GRAY=$(tput setaf 8)
export LIGHTRED=$(tput setaf 9)
export LIGHTGREEN=$(tput setaf 10)
export LIGHTYELLOW=$(tput setaf 11)
export LIGHTBLUE=$(tput setaf 12)
export LIGHTPURPLE=$(tput setaf 11)
export LIGHTCYAN=$(tput setaf 11)
export RESET=$(tput sgr0)
```

```bash
echo "${BLUE}blue${RESET} ${RED}red${RESET}"
blue red
for i in {0..255}; do echo "$(tput setaf $i)test"; done

```



#### Supprimer récursivement tous les fichiers .DS_ST0RE:

```bash
sudo find / -name ".DS_Store" -depth -exec rm {} \;
```

```bash
find . -name '*.DS_Store' -type f -delete
```

```bash
find . -name '.DS_Store' -type f -print -exec rm -f {} +
```



#### Commentaire multi-lignes:

```bash
res1=$(gdate +%s.%N)

: <<'END_COMMENT'
brew info oniguruma 
brew info gnutls 
brew info php

res2=$(gdate +%s.%N)
dt=$(echo "$res2 - $res1" | bc)
dd=$(echo "$dt/86400" | bc)
dt2=$(echo "$dt-86400*$dd" | bc)
dh=$(echo "$dt2/3600" | bc)
dt3=$(echo "$dt2-3600*$dh" | bc)
dm=$(echo "$dt3/60" | bc)
ds=$(echo "$dt3-60*$dm" | bc -l)

#printf "\e[1;34mTotal runtime: %d:%02d:%02d:%02.4f\n" $dd $dh $dm $ds
echo -e "\e[1;34m $dd $dh $dm $ds \e[0m"
END_COMMENT
```

```bash
: '
comment
'
```



#### Créer une playlist .m3u:

```bash
$ ~/Music/Shaka Ponk - Apelogies/CD1 master*
# Musepack(mpc) Monkey's Audio(ape) WavPack(wv) Apple alac(m4a)

printf "#EXTM3U\n" > playlist.m3u

ls -1v | grep -E '.mp3|.mp4|.m4a|.aac|.alac|.flac|.ogg|.mpc|.ape|.wma|.wav' >> playlist.m3u

#EXTM3U
01. ————————THE GREATEST TITS.mp3
02. Hombre Que Soy (2020).mp3
03. I'm Picky (2020).mp3
04. Sex Ball (2020).mp3
05. Smells Like Teen Spirit (2020).mp3
06. Hell'o (2020).mp3
07. French Touch Puta Madre (2020).mp3
08. My Name Is Stain.mp3
09. Wanna Get Free (2020).mp3
10. Story o' My Lf (2020).mp3
11. Let's Bang (2020).mp3
12. Fonk Me (2020).mp3
13. Mysterious Ways.mp3
14. How We Kill Stars (2020).mp3
15. Do (Unplugged).mp3
16. Body Cult (2020).mp3
17. Palabra Mi Amor (2020).mp3
```

```bash
printf "#EXTM3U\n" > playlist.m3u
find . -name '*.mp3' | sort >> playlist.m3u
```

```bash
for i in {1..5}; do printf "#EXTM3U\n" > ${i}star.m3u; find . -type f -exec grep -i -l --text "rating.$i" '{}' \; >> ${i}star.m3u; done
```



#### Remplacer les espaces dans les noms de dossiers/fichiers:

```bash
 find ./thumsup -name "* *" -print0 | sort -rz | while read -d $'\0' f; do mv -v "$f" "$(dirname "$f")/$(basename "${f// /_}")"; done
r
```



#### Connaitre le nom du Terminal:

```bash
$ ps -p $$ -o args,ppid
ARGS  PPID
-zsh   705

$ ps -p 705 -o args					# <- PPID
ARGS
/usr/bin/login -fqpl bruno /Applications/iTerm.app/Contents/MacO

```

