Basics

This is a living post and will be updated as I discover new tips and tricks that I think are cool and worth sharing.

cd

cd without any arguments changes to the user home directory.

cmd
cd
pwd

# output

/home/bradmin

cd - changes directory to the previous directory.

cmd
pwd

# output

/home/bradmin/

cd /tmp
cd -

# output

/home/bradmin/

grep

grep -v show only what did not match.

cmd
ip link show | grep -v DOWN

# output

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000

grep -c show a line count of what matched.

cmd
ip link show | grep -c mtu

# output

3

grep -w to only match whole words.

cmd
# match only whole word 'be'
cat phonetics | grep -w 'be'

# output

to be or not to be

grep -B and grep -A to only show the match with x lines of context before and after the match.

cmd
# 3 lines after match
cat phonetics | grep -A 3 'daddy'

# output

the daddy
that is easy peasy
when you are learning
your ABC

# 3 lines before match
cat phonetics | grep -B 3 'daddy'

# output

alpha bravo charlie
delta echo foxtrot
golf hotel india
the daddy

grep -r search recursively through files.

cmd
# match 'import' through files in current directory
grep -r 'import' ./

grep -r --include='*.<extension>' extends the -r option to only search files matching the file pattern. Very useful to limit searches to certain file types.

cmd
# match 'import' through Ruby (*.rb) files in current directory
grep -r --include='*.rb' 'import' ./

Use grep -H or grep -h to show the filename where the match occurred.

cmd
grep -rH 'import' /tmp

# output

/tmp/thing.py:import os

ls

ls -a lists all files including hidden files.

cmd
ls -a

# output

.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .ssh

ls -r lists files in reverse order.

cmd
ls -r

# output

phonetics  file  chef  .ssh  .bashrc  .bash_profile  .bash_logout  .bash_history

ls -t lists files ordered by modified time with the newest files at the bottom. Combine with -r to get the newest files at the top.

cmd
ls -t

# output

file  chef  phonetics

# newest files at the top
ls -tr

# output

phonetics  chef  file

ls -S lists files ordered by file size.

cmd
ls -S

# output

file  chef  phonetics

ls -h prints file size in human readable format. Combine with the -l option.

cmd
ls -lh

# output

-rw-r--r-- 1 bradmin bradmin  62M Mar 24 12:45 file
-rw-r--r-- 1 bradmin bradmin 4.1K Mar 24 12:14 chef
-rw-r--r-- 1 bradmin bradmin  118 Mar 24 04:08 phonetics

xargs

The xargs command allows you to build and execute commands from standard input. Great to use when piping the output of one command to the input of another.

xargs -I{} is used to add a flag to the input.

cmd
cat urls
https://jinja.palletsprojects.com
https://www.terraform.io/

# fetch the output of each URL utilizing 2 processes
cat urls | xargs -I{} -P2 curl -L {}

xargs -L<x> is used to limit the input into chunks of lines x is the number of lines per batch.

cmd
cat urls | xargs -L1 curl -L

# output

# ... output of curl https://jinja.palletsprojects.com ...
# ... output of curl https://www.terraform.io/ ...

xargs -P<x> is used to run commands in parallel. x is the number of processes.

cmd
cat urls | xargs -P2 -L1 curl -L

# output

# ... output of both curls simultaneously with 2 processes ...

Example

The below incantation recursively searches through all Jinja files for links. Cleans up the output and only shows the unique values and then curls the urls to check if there are any dead links.

cmd
grep \
  -r \
  --include='*.jinja' \
  -oh \
  'http[s]\?://[^"]\+' . \
  | sort \
  | uniq \
  | xargs -P20 -L1 curl --fail -o /dev/null -s -L -w "%{url_effective} > status: %{http_code} > type: %{content_type}\n"
cmd
# output

https://dev.to/bradmin > status: 200 > type: text/html; charset=utf-8
https://github.com/bradmin > status: 200 > type: text/html; charset=utf-8
https://twitter.com/bradmin_codes > status: 200 > type: text/html; charset=utf-8
https://www.linkedin.com/in/bradmin/ > status: 200 > type: text/html; charset=utf-8

Shell Expansion

Search your history file with the CTRL + r keyboard combination.

cmd
(reverse-i-search):

Expand the last command of the previously run set of commands with the ALT + . keyboard combination.

cmd
tail -f /var/log/some/nested/log/file
tail: cannot open '/var/log/some/nested/log/file' for reading: Permission denied

sudo tail -f <alt . expands> /var/log/some/nested/log/file

A space at the start of a set of commands stops the line being written to your ~/.bash_history. Good for keeping things like passwords and API keys out of your ~/.bash_history or when running dangerous sudo commands.

cmd
# space at front omits command from history

 sudo rm -rf /some/directory

!<number_of_command> to re-execute a command from the output of the history command.

cmd
history

# output

2001  cat Pipfile
2002  gunicorn -w 4 -b 0.0.0.0:5000 wsgi:app
2003  cat wsgi.py

!2002 # re-execute command 2002

!! repeats the last command.

cmd
!! # last command was 'pwd'


# output

pwd
/home/bradmin/

!<command> repeats the last occurrence of <command>.

cmd
!rails

# output

rails console
Running via Spring preloader in process 11499
Loading development environment (Rails 5.2.2.1)

tee

The tee command can be used to write content to a file. This is very useful when combined with a HERE doc for files that require sudo.

file
sudo tee /etc/yum.repos.d/nginx.repo > /dev/null << "EOF"
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

Keyboard Shortcuts

CTRL + e moves the cursor to the end of the line.

CTRL + a moves the cursor to the beginning of the line.

CTRL + u deletes from the cursor to the beginning of the line.

CTRL + k deletes from the cursor to the end of the line.

CTRL + w deletes characters behind the cursor up to the space character.

ALT + d deletes characters in front of the cursor up to the space character.

CTRL + f Move forward one character.

CTRL + b Move back one character.

ALT + f Move forward one word.

ALT + b Move back one word.

Tags