Bash
- https://www.shellscript.sh/index.html
- https://github.com/alexanderepstein/Bash-Snippets
- https://zwischenzugs.com/2018/01/06/ten-things-i-wish-id-known-about-bash/
Note that the commands are grouped by theme (e.g. utilities, networking) with examples where possible.
Hello World
$ echo '#!/bin/sh' > my-script.sh
$ echo 'echo Hello World' >> my-script.sh
$ chmod 755 my-script.sh
$ ./my-script.sh
Hello World
Creating a directory
mkdir
Using the -p
flag will create the directory and any parent directories that don't exist.
mkdir -p myparentdirectory/mysubdirectory
Finding
tree
Native to Linux/Unix (needs to be manually installed via brew install tree
on MacOS). More examples here.
tree -L 1 <dirname>
grep
grep xyz *
grep -v '^#' /etc/rsyslog.conf | grep -v '^$' | grep -v '^\$'
Grepping the output (note that it will return the line(s) that it finds a match on).
docker ps | grep postgres
"Amal" Shell tip. If you need to grep the result of a ps and don't want the grep command included in the result give grep a regex search term. For example if you want to search for zsh processes, use the regex [z]sh. This works because the regular expression [z]sh will not match the string [z]sh
$ ps -a | grep zsh
62604 ttys000 0:02.80 -zsh
78745 ttys000 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox zsh
$ ps -a | grep zsh | wc -l
2
$ ps -a | grep [z]sh | wc -l
1
find
find . -name "*pet*"
ag
- https://github.com/ggreer/the_silver_searcher
ag βvimgrep βhelloβ -l
xargs
"xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input."
Utilities
The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals.
Shell Check
"finds bugs in your shell scripts"
- https://www.shellcheck.net/
less
"...similar to more (1), but which allows backward movement in the file as well as forward movement"
less -n 1 xyz.txt
time
"The time utility executes and times utility. After the utility finishes, time writes the total time elapsed, the time consumed by system overhead, and the time used to execute utility to the standard error stream. Times are reported in seconds."
time run go hell.go
ps (process status)
ps -a
pgrep / pkill
pgrep node
pkill node
Kill a process running on a specific port (e.g. 9010
):
kill $(lsof -t -i:9010)
lsof (list open files)
See what's running on a certain port...
lsof -i :8545
weather :)
https://wttr.in
unzip
which / where
wc
wc -l jsconfig.json storage.rules
6 jsconfig.json
7 storage.rules
13 total
pbpaste
Paste from the clipboard to a file...
pbpaste < diagram.py
pbcopy
Copy the contents of a file to the clipboard...
pbcopy < diagram.py
Copy the markup from a website to your clipboard...
curl -L "http://coolsite.com" | pbcopy
stdin, stdout, stderr
"So, by default, all UNIX systems support three special and standard filenames: /dev/stdin, /dev/stdout, and /dev/stderr, which can also be accessed using file descriptors 0, 1, and 2, respectively. These three file descriptors are also called standard input, standard output, and standard error, respectively."
Exit codes reference
Exit code 0 Success
Exit code 1 General errors, Miscellaneous errors, such as "divide by zero" and other impermissible operations
Exit code 2 Misuse of shell builtins (according to Bash documentation)
paths
- realpath
directory traversal
go back...
`cd -`
"The pushd command saves the current working directory in memory so it can be returned to at any time, optionally changing to a new directory. The popd command returns to the path at the top of the directory stack", Wikipedia
pushd
popd
bat
A fancier cat
.
bat
ls
ls
lsa
networking
lsof - lsof is a command meaning "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them
lsof
lsmod
"Show the status of modules in the Linux Kernel"
lsmod
id
"The id utility displays the user and group names and numeric IDs, of the calling process, to the standard output."
id
Text / Code Editing
vi
- insert - i
- page forward (ctrl + f)
- page back (ctrl + b)
wget
wget http://www.ghibli.jp/gallery/{ged,chihiro,karigurashi,ponyo,kokurikozaka,marnie,kaguyahime,kazetachinu}{001..050}.jpg`
versions
"The uname utility writes symbols representing one or more system characteristics to the standard output."
"How to check OS and version using a Linux command"
uname -a
Environment
Setting an env variable
export INFURA_MNEMONIC='abc xyz'
echo $INFURA_MNEMONIC
Run Commands (.bashrc)
PATH="/home/kevin/.linuxbrew/Cellar/gh/1.4.0/bin/:${PATH}"
export PATH
disk / ram / etc
Calculate available disk space...
df -h /
See the available RAM...
free -h
Symlink
On Unix / Linux: "A symbolic link, also known as a symlink or soft link, is a special type of file that points to another file or directory."
ln -s src/packages/ganache/CONTRIBUTING.md ./CONTRIBUTING.md
Searching history
history | grep x
type
...
Alias
ga. && gc -m ββ. && gc