Three Tips for the Linux Terminal

  • By Brad Conte, January 11, 2009
  • Post Categories: General Tech

The power of Linux lies in the tools it uses, and the shell is an essential tool. If you spend a lot of time in a terminal, you likely value anything that makes the experience smoother. Here are a couple tips to help make the terminal experience as smooth as possible.

Interact with the X clipboard

Before I discovered xclip, one of the most annoying things about being in a terminal was my lack of access to the X clipboard. Some terminal/shell combinations work well with a standard desktop environment, but "highlight-and-middle-click" a) isn't always feasible, and b) doesn't always work. Thankfully, xclip makes it easier.

xclip can output from and write to the clipboard from the standard input. The "-i" and "-o" arguments tell xclip whether you are inputting or outputting clipboard contents, respectively.

Example:

$ pwd
/some/long/path/you/dont/want/to/retype
$ pwd | xclip -i

You may now "Control-V" paste that path where ever you choose. Another example:

wget `xclip -o`

This will download the file from the URL that is in the clipboard. I have found that "pasting" the contents of xclip into the shell using backticks (aka, `) a very convenient work-flow. The result of `xclip -o` is basically having the code right were you would have pasted it in the shell.

You can use xclip to read from and write to the different X clipboards, which allows you to interact with the clipboards for pasting via middle-click or Control-V. The "middle-click" clipboard is selected with the arguments -selection primary and the "Control-V" clipboard is selected with the arguments -selection clipboard.

I might suggest aliasing "xclip" to xclip -clipboard X, where X is your preferred clipboard to operate on.

Use "head" and "tail" more powerfully

You probably know how to use "head" to extract the first N lines from a file and "tail" to extract the last N lines of a file. While this is useful, it's often just as useful to extract the complement of those selections, namely, everything except the first N lines or everything except the last N lines.

The head and tail utilities are powerful enough to accommodate those needs. head allows you to extract either a finite quantity of text from the top or everything but a finite quantity of text from the bottom, and tail allows you to extract a finite quantity of text from the bottom or everything but a finite quantity of text from the top.

  • head -n N - by default outputs the first N lines (equivalent to using +N). Using -N outputs all but the last N lines.
  • tail -n N - by default outputs the last N lines (equivalent to using -N). Using +N outputs lines starting on the Nth.

Example:

tmp $ cat example
1
2
3
4
5
/tmp $ head -n 2 example
1
2
/tmp $ tail -n 2 example
4
5
/tmp $ head -n -2 example
1
2
3
/tmp $ tail -n +3 example
3
4
5

Note that tail's complement mode requires you to specify the first line number to include in the output, so if you want all but the top N lines actually specify the argument N+1.

Change directories with the directory stack

The Bash shell (as well as others, like Zsh) have a built-in "back" feature for navigating directories. The built-in pushd command will put your current working path at the top of a shell-maintained stack and allow you to change to another directory. To go back to the saved path you can use popd. This one is more commonly known than the first two, but worth including because it's so incredibly convenient.

Example:

$ pwd
/some/long/path/you/dont/want/to/retype
$ pushd /some/other/path/
/some/long/path/you/dont/want/to/retype
$ pwd
/some/other/path/
[...]
$ popd
/some/big/long/directory/you/dont/want/to/retype
$ pwd
/some/long/path/you/dont/want/to/retype

Since "pushd" stores the directories in a stack, you can push multiple directories onto the stack and later pop them off in the reverse order you pushed them. It's basically the standard "cd" only with a "back" feature. Speaking of which, the command cd - will always take you back to the previous directory you were in.