# 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
```

