A few useful command line tips
Just to spare you the pain, I won’t go into my usual spiel about how useful the command line is. If you’re interested, use the search box in the upper right to dig up my thoughts on that subject. And if you’ve been reading this space for any length of time, you know that I have more than a couple of those thoughts …
At the terminal window, there are so many commands and so little time to learn them all. While I don’t think that everyone needs to learn every terminal command, there are a number of tips and tricks that can make life (or even just a visit) to the command line a bit easier.
Let’s take a look at a few of my favourite tips. If you’ve been using the command line for any length of time, these tips will probably be old hat to you. But if you’re still learning about the Linux command line, then you might find these tips useful.
Quickly clearing the terminal window
I don’t know about you, but I find a cluttered terminal window to be more than a little distracting. Normally, you’d get rid of everything that’s in the window using the clear command. But why use six keystrokes (five for the command, plus Enter) when you can use three?
To quickly clear a terminal window, type CTRL+l (that’s a lower-case L if you’re wondering).
The dot is your friend
At least when moving things around …
By the dot I mean . – a simple period. When you’re copying or moving files around your computer, or when transferring files via ssh from a remote server, the dot signifies the directory that you’re currently in.
If you issue a copy or move command without a destination, then you’ll get a missing destination file error. That’s where the dot comes in.
Let’s say you’re in the directory /home/scott/Writing/Completed/, and you have a directory one level above named WiP (where you keep drafts of your writing). Use the following command to move the file CLI_Tips.odt from the directory WiP to the current directory:
cp ../WiP/CLI_Tips.odt .
Continuing an interrupted scp transfer
scp is a very useful tool for securely transferring files between your computer and a remote server. But, sometimes, things can go wrong.
A couple of months back, I was transferring a large file to my computer when the connection with the remote server was lost. I’d only received about 48% of the file. Instead of starting from scratch, I used another command called rsync to pick up the transfer where it left off.
To do that, you’d use the following command:
rsync –partial –progress –rsh=ssh user@host:/path_to/remote_file .
Notice the dot at that end of the command? I told you it was useful …
The semicolon and ampersand are your friends
If you need to run multiple commands but don’t run those commands often enough to justify writing a script, there are two easy ways to string them together. You can separate the commands (and any parameters they take) with a semicolon (;) or with a pair of ampersands (&&).
If you use a semicolon, make sure it comes right after the command, followed by a space. With the two ampersands, make sure there’s a space before and after them.
For example, here’s the string of commands that I’d use to add a cover to a PDF ebook, then update the metadata of the book:
joinPDF TwoViewsofJapan.pdf cover.pdf TwoViews.pdf && pdftk TwoViewsofJapan.pdf update_info metadata.txt output “Two Views of Japan.pdf”
Quickly returning to your /home directory
Specifically, to the top level of the directory. How? By typing cd ~/ or just cd ~.
The tilde is simply a shortcut. Your system knows it means /home, and you can use it to return to the top level of your /home directory from any folder on your computer. That includes any sub folder in your /home directory. Like many of the tips in this post, using the tilde saves you keystrokes.
Do you have any useful command line tips and tricks? Why not share them by leaving a comment.
Photo credit: mulligand