# Bash Ninja 🥷
###### tags: `mac` `bash`
[TOC]
### Learn about stuff
```bash
# short description
whatis stuff
# long version
info stuff
man stuff
```
while in a man page, Type slash `/` and then type the string to search for. Keep pressing `n` to get to the next item.
```bash
stuff -h
stuff --help
# retrieve commands to see examples on how to write them
history | grep 'git'
```
another way to do it is to trigger ‘reverse-i-search’ press `ctrl` + `r` in terminal
### Piping & Command Substitution
execute a command and feed it as input to another command
```bash
# with ` `
file `which python`
# with $()
pth=$(which python)
file $pth
# with piping, i.e. "|"
which python | xargs file
````
#### with `-exec`
```bash
# find files of size 0 and remove them
find . -size 0 -exec rm -i {} \;
```
`{}` is replaced by each file name found in the executed command
`\;` is for delimiting the end of the exec command
[:link: more on exec](https://superuser.com/questions/1072679/bash-what-does-means#:~:text=%7B%7D%20has%20absolutely%20no%20meaning,the%20command%20executed%2C%20here%20find%20.&text=It%20is%20normally%20used%20to,on%20the%20same%20command%20line.)
### Operators
```bash
# run command1, wait for it to finish and run command2
command1 ; command2
# same but command1 has to exit successfully (no errors)
commmand1 && command2
# write to file
find ~/Documents > search_log.txt
# append to file
find ~/Code >> search_log.txt
```
[:link: more on operators](https://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators)
### View content & Explore files
#### view
```bash
# in terminal
cat ./file # useful for greping, e.g. cat ./file | grep 'string'
less ./file # simple and fast
# in apps
subl -n ./file # Sublime
code ./file # VS Code
```
#### compare
```bash
code -dn ./file1 ./file2
```
### Find stuff
```bash
# searches for "possibly useful" binaries and sources
whereis stuff
# only searches for executables
which stuff
```
Find all files (`-type f`), ending with '.xml' and discard the errors (e.g. permission denied)
```bash
sudo find / -type f -name '*.xml' 2>/dev/null
```
Redirect error spam to null and store the rest in output.txt
```bash
find . -name "data*.txt" -print 2>/dev/null > output.txt
```
display as `ls` list
```bash
sudo find /Users/simon/.config/ -type f -name '*.xml' 2>/dev/null -exec ls -Ghla {} \;
```
counts lines of code in a project
```bash
find . -type f -name "*.py" -exec wc -l {} \;
```
dump it to sum
### Move / Rename stuff
move all content (files & directories)
```bash
mv -v ~/Downloads/* ~/Documents/
```
To move all files, but not folders:
```bash
find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Documents
````
To move only files from the Downloads folder, but not from sub-folders:
```bash
find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Documents
```
### Permissions
#### attributes
| Permissions | No permission | Execute | Write | Write and execute | Read | Read and execute | Read and write | Read, write and execute |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Octal notation | `0` | `1` | `2` | `3` | `4` | `5` | `6` | `7` |
| Symbolic notation | `---` | `--x` | `-w-` | `-wx` | `r--` | `r-x` | `rw-` | `rwx` |
| Binary notation | `000` | `001` | `010` | `011` | `100` | `101` | `110` | `111` |
```bash
# example
chmod 755 ./file
chmod -R 755 ./dir
```
#### users
```bash
whoami
chown bob ./file
```
#### privilege escalation
```bash
sudo chown bob ./file
```
### Tasks & Processes
```bash
# see processes
top
# alternative (brew install htop)
htop
# kill process with PID
kill 68984
# kill processes which name include
killall python3
# look for running tasks, get PID
ps aux | grep python3
# look for proccesses on a specific port & kill it
lsof -ti:8890 | xargs kill -9
```
### Environment Variables
```bash
# list environment variables
printenv
env
# set variable
export PYTHONPATH="$PWD/code"
# print variable
echo $PYTHONPATH
# search variable
env | grep PYTHON
```
### Disk Management
Format USB stick to work with both Windows and Mac file systems (using exFAT)
```bash
# list drives to get disk identifiers
diskutil list
# formatting command
diskutil eraseDisk FILE_SYSTEM DISK_NAME DISK_IDENTIFIER
# example
diskutil eraseDisk ExFAT DiskName /dev/DiskNodeID
```
### Subshell command
execute commands in a subshell with `(<commands>)` when you don't want them to affect your current shell, *e.g.*:
```bash
(cd /usr/share/doc && find * -name index.html)
```
### Miscellaneous
```bash
# display PATH
echo $PATH
# get all aliases
alias
# for one command
alias ls
# bypass aliases from .bash_profile
command ls
# see users
less /etc/passwd
```