Skip to content

Latest commit

 

History

History
245 lines (169 loc) · 4.88 KB

useful_shell_commands.md

File metadata and controls

245 lines (169 loc) · 4.88 KB

Useful shell commands

Basics

man (manual)

man {command} # shows the manual of the command

pwd (present working directory)

pwd # prints in shell the path of the pwd

ls (list)

ls # list contents of pwd

ls -latrh # detailed view of list reverse sorted by modification time
# l(ong): long list format
# a(ll): includes hidden files and directories
# t(ime): sort by modification time
# r(everse): reverse the order of the sort
# h(uman): makes file sizes human readable

ls ../other_directory # lets you inspect other directories

cd (change directory)

cd {target/directory} # change pwd to target directory

echo

echo 'Hello, World' # prints text

cat (catenare - to chain)

cat file.txt # prints the contents of the file

touch

touch hello.txt # creates file if not exists or update the timestamps if it doesn't

cp (copy)

cp hello hello-again # copy a file/directory to target file/directory

mv (move)

mv hello-again goodbye # move a file/directory to target file/directory

rm (remove)

rm hello # remove a file
rm -r mydir # remove a directory (needs recursive flag)

ln (link)

ln -s text.txt other_directory/other_text.txt # creates a symbolic link

less

less book.txt # lets you view less text, in a scrollable manner

grep

grep 'Sinbad' the-count-of-monte-cristo.txt # lets you pattern match against text

Recommendation for a faster grep: ripgrep

find

find -name 'node_modules' -type d # lets you find files and directories

Recommendation for an easier find: fd

sed (stream editor)

cat template.yaml
name: sinbad the sailor
email: REPLACE_EMAIL # target
url: https://example.com
sed 's/REPLACE_EMAIL/[email protected]/' template.yaml # lets you find files and directories
name: sinbad the sailor
email: [email protected] # replaced here
url: https://example.com

awk

It's a programming language for text processing, filtering and extraction. Use it with RegEx.

awk -F'\t' '{print $3 " | " $4}' quotes.tsv
# -F'\t': Sets the field separator to a tab character ('\t'). This tells AWK to split each line of input based on tabs.
# '{print $3 " | " $4}': Specifies the action to perform on each line of input. Here, it prints the contents of the third column, followed by a pipe character ('|'), followed by the contents of the fourth column.

sort

sort data.txt # sorts text contents (default is alphabetic)
sort -k2 -n data.txt # sort by numeric

head

head data.txt # lets you see the first lines of a file

tail

tail data.txt # lets you see the last lines of a file
tail -f log # -f flag makes so the terminal prints any new lines appended

fzf (fuzzy finder)

It loads the directory tree into the fuzzy finder

compgen

Generate possible completions for commands. When used on it's own, lists all possible commands from current shell.

compgen -c | less

Piping commands

The standard output of a command can become the standard input of the next command with the pipe | operator.

echo 'Hello, world!' | sed 's/world/universe'
Hello, universe!

xargs

Will split the (piped) stdin and take them as arguments to the next function.

ls | xargs du -sh # command to show the disk usage of each directory in ls
# du: disk usage
# -s: summarizes disk usage of each argument
# -h: human readable
ls | grep 'l' | xargs du -sh # filtering the results of ls by the letter 'l'

Subshell

You can use commands inside commands, and whole pipelines with this syntax: $(command)

echo "My current directory is: $(pwd)"
My current directory is: /home/user/notes

Redirection

Direct the stdout of a command to a file with the > operator

ls --help > ls-help.txt

Append to the end of the file with >>

You can use the contents of a file as the stdin of a command with < operator

tr 'a-z' 'A-Z' < template.yaml

Useful pipelines

Fuzzyfind all command manual entries

compgen -c | fzf | xargs man

Suggestion - set an alias with

alias fman="compgen -c | fzf | xargs mancompgen "

List all avaiable kubernetes pods

kubectl get pods -A --no-headers | fzf \awk '{print $2, $1}' | xargs -n 2 sh -c 'kubectl describe pod $0 -n $1'

Finding the heaviest files and directories

du -ah . | sort -hr | head -n 10

Finding all node projects and setup deletion

find . -name 'node_modules' -type d |
  xargs du -sh |
  sort -hr |
  fzf -m --header "Select which ones to delete" --preview 'cat $(dirname {})/package.json' |
  xargs -r rm -rf