Hello reader! So, I haven’t written a blog post for quite some time, why? Because I got a new job and returned to my passion as a software engineer which kept me quite busy. Previously I was in sales so this blog was a great outlet for my interests in everything software.

In my new role I have become more and more familiar with the linux terminal and using it throughout my work day. As a result of this, there’s times when I’ve picked up on commands which have drastically improved my proficiency.

Forgetting to sudo a command

So this probably happens to you all the time, you write apt install something but forget sudo, let’s say it’s a very long command as well. Typically you might click up, then hold down left for ages until you get to the start of the command again. Time consuming write? But, if you do this:

sudo !!

Then it will rerun the command with sudo at the front, the two exclamation marks means to linux “Take whatever command was just run and add sudo (with a space after) to the start of the command. Awesome right? The space I have included between sudo and !! is included. So it is basically anything including spaces before !! that are added.

There’s other instances where this is useful but sudo is definitely the most frequent and time consuming for any user.

Replacing commands

So typically you may ls to list a directory and then find your file you need and nano/vim that file subsequently. So what I now do is ls the file itself once I’ve found it as well. Then I simply enter:

^ls^nano

What this means to linux is take the text from the previous command that matches between the two ^ and replace with the text after the last ^. So ls /home/me/myfile.txt becomes nano /home/me/myfile.txt and I’m now writing to this file without needing to do any copy and pasting or up and hold left stuff.

Finding an old command

So we probably all know about history – btw this is why you should always pick bash over shell! So anyway, you have a command from way back when that you want to remember but you know your history is already more than 2000 lines long so it’ll be time consuming to find. As long as you remember some of the command, you can use grep here with a pipe. If you don’t know what pipes are yet in terminal please check them out, they’re very useful.

Pipes in short mean you can pipe the output of one command to another within the same line of execution in shell/bash. So if I do:

history|grep docker

I am piping the output of history to the command grep as input with the keyword search for docker. I will then get a result showing me all the commands I’ve run which included docker within the text of that command.

Pipes

As you learn pipes you’ll become more and more proficient with this kind of stuff. A really fast way of transferring data for instance is compressing a directory into a tar – within the same command then piping that to the directory you want and then decompressing on the end like so:

tar cf - . | (cd /work/bkup/jane && tar xvf -)

Notice how you use the &&? This is because we wouldn’t create a second pipe for the third command, otherwise our tar extracted data would finish at the cd command and not end up at the tar extraction command so instead we use &&. && if you don’t know means, run this commands after the previous, within the same shell process.

Note: Each shell command line executes as it’s own process before returning. This is particularly obvious when using tools like jenkins which run each command in their own docker instances so provides a similar behavior of the isolated execution.

Automating command tasks

So the final trick is let’s say you run a set of commands very frequently and you know it would be best to put it in a bash script, but you run that command in several different locations. Simple add it to PATH and run it anywhere:

export PATH=$PATH:/home/ed/myscript

Notice I didn’t add .sh to the end of the file, that’s because the file is known to be executable since it’s first like is #! (followed by the execution environment, such as /bin/sh or /bin/bash).

So now anywhere I can run the command myscript and this will execute my bash script. Optimizing my working day :).

Leave a Reply

All fields marked with an asterisk (*) are required