[3/4] Beginners BASH basics - Wait, BASH can do that?
Hayden Rouille
Posted on June 30, 2020
Part 3 of a 4 part series
Part 1 is available here, and 2 here
In part 3 of the beginners BASH basics series, we're going to dive into some seemingly daunting tools. By the end, you're going to start to see that the power the command line provides is much greater than you ever realised..!
This article cannot even scrape the surface of the likes of vim
, tmux
or sed
, so if you're getting to love them as much as I do, then I'm going to recommend some further reading which I'll leave at the bottom of part 4.
Remember, there are people who have used these tools for 30+ years and are still learning things...
On that note - let's get started.
sort
sort
does exactly what it says on the tin. It will sort lines of text files. sort
is often used to help display a commands output in a more human readable fashion. Some useful flags include -n
to sort numerically instead of alphabetically, -r
to sort in descending order, or --ignore-case
to... ignore case.
Example usage: sort my_file.md
piping
Lets put together a few of our learnt techniques into one command!
Much like the common pipeline operator in other languages such as Elixir, "piping" is a very common way to pass the STDOUT of one command to other commands and manipulate the end result in the way you want.
So how do we do it? Well, the |
key, AKA "the pipe" can be used after any command that will give STDOUT.
Example usage: ls | grep .md
will get you all files in the current directory with .md
in the name by sending the result list of ls
into the last argument of grep
.
You can also pipe each individual argument into another command using xargs
such as ls | xargs cat
which will use cat
on each result.
If you wanted to use the argument in a specific usecase, you can use -I{}
after xargs
, which will give you access to the argument through {}
in a following command.
sed
sed
is used to manipulate text is a scriptable manner. It's commonly used for find and replace, or deleting values from a file or list of files.
Example usage: sed -i 's/my_search/my_new_value/' my_file.md
Find out how to create some awesome bindings for super-fast project wide search and replace in my article about optimizing your workflow with fzf & ripgrep!
awk
awk
is a great dynamic tool for working with files. It offers an API capable of printing or manipulating text in ways you can only imagine!
One of the most common ways I use awk
is to print out specific values from a column of data.
Say you want to see the filesize of every file in your directory. You'd probably start by executing ls -lah
. You can then pipe into column
, which maybe not for ls
, but for a lot of commands helps get the STDOUT into a format usable by awk.
Then you can pipe the result into awk '{print $5,$9}'
to see the values of the fifth and ninth column, the size and name respectively.
If you want to get real fancy, you can use awk
s OFS
to set a tab delimiter and then sort them by filesize.
Check this out:
ls -lah | column | awk 'OFS="\t" {print $5,$9}' | sort -h
Awesome, right?
ssh
ssh
is a secure shell protocol used to securely login to remote servers. If you have your own website, or are looking at creating one - you should at least know the basics of ssh
.
Providing you know the name and IP of the server you want to connect to, you can connect simply by using ssh username@ip
.
ssh
supports a lot of useful flags, some to note are -J
if you need to jump from one server to another, -p
for a specific port or -D
for tunnelling.
Unless you've setup an ssh key and added it to the .ssh/authorized_keys
file at your remote server, you'll be required to enter a password when accessing your server. Learn more about setting up ssh keys with Digital Ocean's handy article here
Taking it a step further, you can even specify different hostnames, ports and access keys if you are accessing several servers and don't want to type the IP or full command every time by using your ssh
config file.
The config file will be at $HOME/.ssh/config
, and will look something like the below:
Host thenameiwant
HostName 127.0.0.1
Port 3000
User myuser
IdentityFile /home/userName/.ssh/id_rsa
Now, when you use ssh thenameiwant
, it will be the same as writing ssh myuser@127.0.0.1 -p 3000
, except it will use the specified ssh-key (in case you need multiple).
I've started writing a utility to help manage my ssh configuration file, you can check it out here. It's still in early development, but it's a fun tool!
tmux
tmux
is a terminal multiplexer. This means that within your terminal application, be it the basic terminal, Alacritty or iTerm, you can spin up multiple terminal sessions and easily move between them.
This comes in super useful when you want to run a long download in one, and continue working in another. Or running a server in one and using your command line editor in another.
tmux
usually comes standard with any Linux server you may have, and so when you're accessing another server, if you wanted to leave a command hanging on a port for instance, you could spin up tmux
, run the command, detach and exit. This will continue running after you exit ssh
.
To enter a tmux
session, you can simply type tmux
to the command line, or create a named session with tmux new-session -s my_name
. This allows you to kill the session by name (tmux kill-session -t my_name
) or attach to a specific session (tmux attach-session -t my_name
).
All tmux
specific commands are used after the set tmux
prefix. The default for this is ctrl-b
.
For instance, if you want to detach your session, you can use ctrl-b+d
.
You can then view your sessions with tmux ls
and attach or kill as per the above.
vim
Vim is an extremely powerful text editor accessed via the command line. Whether you want to use it as your IDE or not, it's worth knowing the basics because there will undoubtedly be a day when you have to edit a file on a server and you'll be stuck using nano
. And nobody likes nano
.
Vim has a concept of different modes. To start with, we'll just cover the two you need to edit a simple file - normal and insert mode.
To move around in vim
, use the arrow keys, or hjkl
. w
will move you forward a word, and you can move down by paragraphs by pressing {
and }
, or half a page at a time with ctrl-d
and ctrl-u
.
Vim uses a concept of motions when in normal mode. You can use dd
to delete a line, or dw
to "delete word". The same applies with c
.
When you're in insert mode after using c
, pressing i
or a
(append), you can type as normal.
Remember, you can always get back to normal mode by pressing esc.
And the notorious question, how do you leave vim? :wq
will save and exit the current file, or :q!
will quit without saving.
conclusion
Feel like a command line pro now?
As I've mentioned before, these command line tools have improved my productivity tenfold... it's just a bonus that you can show them off to your friends in your fancy scripts!
If you want to check out how I customise tmux or my bashrc, head over to my repository below. If you have any questions, feel free to pop me a message or leave a comment below!
Posted on June 30, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.