Getting to grips with the basics of Linux server administration isn’t too difficult. The fundamental command set and its options can be picked up fairly quickly. But if you just skim the surface and learn only what’s strictly necessary, you’re missing out on a lot of the power that the Linux command line has to increase productivity.
Linux, as a Unix-based operating system, has a venerable history stretching back more than 4 decades, and as an OS favored by geeks and hackers it has accumulated a huge number of optimizations and tools that make life on the command line more efficient. In the time-saving command line tricks we’re going to look at today, you’re definitely going to find a few that will have you facepalming when you think about how many times you’ve gone the long way round to achieve something that can be done much more quickly.
Make The Most Of Your Command History
When I first started out on the Linux command line, as a super-efficient (aka lazy) sort, I didn’t want to have to retype commands that I’d used before, so, even if I only had a vague memory of using the command in the last few days, I’d go back through my history using the cursor keys, pressing it as many times as necessary to find the command I wanted. Often that ended up using many more keystrokes than just typing it out again.
Then I found out about Ctrl-R and my command line sessions improved considerably. Ctrl-R activates the history search feature. Hit it and enter a few letters of the command you want and the matching command from your history will appear, hit enter, and you’re done.
One of the more annoying aspects of working on the command line is forgetting when you need root privileges to run a command. Usually you’d arrow up, cursor to the start of the command, and type ‘sudo’. Try this instead:
Directory Navigation Shortcuts
Everyone knows they can tab to complete commands, including directory names, but ‘cd’ also has a number of handy shortcuts. Don’t forget to remove the quotes from the examples we give in this article.
‘cd ~’ — takes you to your home directory.
‘cd -’ — takes you to the previous directory.
‘cd ..’ — move up one directory. This can be chained to go up more than one directory, e.g. ‘cd ../../..’.
If you ever get lost in the directory tree, the ‘pwd’ (present/print working directory) command will print out your current location. If you’ve been hacking away all night and forgotten who you are, the ‘whoami’ command can be pretty useful too.
For really fancy directory navigation, you can try out the ‘pushd’ and ‘popd’ commands, which let you push directory names onto a stack and then pop them off again.
If you tend to use the same options to a command or type the same command often, it’s handy to alias that command to something simpler. For example, you could make an alias for ‘apt-get update && apt-get upgrade’ that reads ‘upgrade’.
To do this you run the command
alias update=“apt-get update && apt-get upgrade”
This time leave the quotes in. The shell only remembers aliases for the current session, so if you want to make them permanent, put the commands in your ~/.bashrc file.
As a supplement, you might have notice the ’‘&&’ notation I used above. That’s useful for chaining two commands together such that the second one only runs if the first one completes successfully.
Easy File Sharing
Woof is a simple, single serving web server. Run ‘woof myfile’ and it’ll spit out a URL which you can use from anywhere on your network to get the file.
You wouldn’t think that the command line is the ideal place for modifying images, but if you’re working on a website and want to quickly resize a batch of images, or add an effect like a blur, or even convert between formats, ImageMagick is extremely useful. ImageMagick supplies a suite of different commands, of which ‘convert’ and ‘mogrify’ are probably the most useful.
‘convert myimage.jpg myimage.png’ — this does exactly what you’d expect it to, and if you like you can add a blur, crop the image, draw on it, re-sample it, and many other things, while it converts.
ImageMagick is a very powerful tool, rivaling graphical image editors like GIMP for features. Check out its documentation to learn more.
Watch What You’re Doing
Have you ever been in the situation of needing to monitor something over time using a command that just spits out its data and ends? For example, you might want to watch changes in a file’s size using ‘ls -l’. One way of doing it is to run the same command over and over again, but that’s not exactly elegant.
‘watch -n 3 “ls -l”’
Will run the command at three-second intervals, overwriting the previous result so that your window doesn’t fill up.
This is a small fraction of the number of time saving tricks that are baked into the Linux command line. We’d love it if you’d share your favorites with us in the comments — there’s always something new for us to learn.