Unix and GNU/Linux - Basic Tips

First published — Aug 06, 2023
Last updated — Aug 06, 2023
#tools

Rapid-fire Unix and Linux tips. Basic usage everyone should know. Desktop, shell, terminal, copy-paste, key shortcuts. You’d be surprised.

Table of Contents

Introduction

This article gives quick, summarized tips on basic topics related to Unix and GNU/Linux.

The idea is to explain topics in absolutely shortest terms and provide links to longer description of each.

Thus, be prepared for breadth, not depth in this article.

At first, you might think there is too much stuff listed, or that the commands are too cryptic. Don’t worry, you will remember them by practicing over time. For now just be aware that these things exist. Also, this is basic knowledge and has existed in Unix and computing for decades. It is supported in a compatible way in a large number of Unix programs. Learning these things will improve your overall Unix and computer skills directly and indirectly.

Copy-Pasting

Like Unix, GNU/Linux systems use the X Window System for graphics.

X comes with 3 separate buffers for copying and pasting data between applications, called PRIMARY, SECONDARY, and CLIPBOARD. By default, SECONDARY is not used.

PRIMARY

When ever you select anything with the mouse, it is automatically stored to PRIMARY. You don’t have to press any keys nor choose Copy from the menu.

You can then paste its contents by clicking the middle mouse button. Using PRIMARY does not affect CLIPBOARD at all.

CLIPBOARD

When you choose Copy in the menu or press Ctrl+c, this stores the selection to CLIPBOARD. Similarly, if you choose Cut or press Ctrl+x, it will also copy to CLIPBOARD, but additionally delete the content from it original location.

In either case, you can then paste from CLIPBOARD by choosing Paste in the menu or by pressing Ctrl+v.

Using the CLIPBOARD automatically copies its contents into PRIMARY – it overwrites whatever was in PRIMARY at the time.

Sometimes, especially in terminals, you might not have access to Copy/Paste menu options, or keys like Ctrl+x/c/v are not applicable because they conflict with keys used in the shell. In such cases, use Ctrl+Insert to copy, and Shift+Insert or Ctrl+Shift+Insert to paste.

Alternatively, you can get Sun Type 6 keyboard or a similar one which has physical cut/copy/paste keys.

GUI Paste Managers

If you want an even bigger system for management of pastes, graphical programs that do it are copyq.

Old school tools are called xsel, xclip, xclipboard (package x11apps), and yank-cli (package yank).

Copy/Pasting in Shell

While we are on the subject, we could briefly mention the behavior of copy/paste in Linux (pseudo)terminals.

In Terminal Emulators (Pseudo Terminals)

GUI terminals in Linux usually play nice with copy-pasting. Some offer direct copy/paste functionality, either using Ctrl+c/v or via buttons in the menu.

Others, like xterm, implement their own method. Text selected in Xterm is by default placed to PRIMARY, as expected. If you want the selection to go to CLIPBOARD directly, press Ctrl+Middle Mouse Button and select option “Select to Clipboard”.

In Virtual Consoles

In Linux Virtual Consoles it is possible to install gpm for mouse support. Copying and pasting with the mouse then works like PRIMARY in the X environment.

Command Confirmations

When using Unix command line, you may notice that output from the system is somewhat brief.

For example, here is a session of creating an empty file, renaming it, and finally deleting it:

> myfile
mv myfile oldfile
rm oldfile

In all those three lines, the system did not return any visible message back to us. To newcomers, this might seem strange and not user-friendly. They might even doubt whether commands were executed successfully or at all.

That behavior of the system is normal, and is one of the design goals laid out in Unix philosophy. Unix does what you tell it to, and in general does not print anything unless there is an error.

But, separately from printed, visible messages, commands in Unix do have an exit status. It is available in a variable named $?, which always contains the exit status of the very last command executed. Thus, to get familiar with things, after running some command, you can run echo $?, which will print its exit status. A value of 0 indicates success. Other values, which are typically in range 0-127, indicate particular errors, which can then be looked up by number in the corresponding program’s documentation.

Clear / Redraw Screen

Consoles or terminals might often get cluttered with text, or you want to clear them to start from the top of a clear, blank screen.

This can be done amateurishly by invoking the command clear. A more professional and convenient keyboard shortcut is Ctrl+l, which should be preferred because it is quicker, does not require the command line to be empty, and also works as “redraw screen” command in most terminal-based applications.

Clearing only clears the screen, without reinitializing the terminal. To also reinitialize it, run reset. It is not a requirement that text is legible while you are typing reset. As long as you type it in and press Enter, it will work.

Similar functionality can be obtained with commands tput clear, tput init, and tput reset.

Yet other commands for the same purpose are setterm -clear all, setterm -clear reset, and setterm -reset.

Scrollback Buffer

When you are watching terminal output, often times there is more text than it can fit on a screen, so it scrolls out of the view.

You can scroll back and forth within the buffer with keys Shift+PgUp and Shift+PgDown.

If you want to pause text while it is being printed to the screen, you can do that with Ctrl+s, and resume it afterwards with Ctrl+q.

Depending on the terminal in use, e.g. in Xterm, holding the left mouse button also has the effect of momentarily stopping the terminal output.

Shell Prefix

When using a shell or reading documentation, you might notice command lines prefixed with ...$ or ...# .

By convention in Unix, $ indicates shells run by regular users, and # indicates shells running as root.

Just by looking at the prompt, you are able to determine its general privilege level.

Environment Variables

PATH

When you type commands in the Unix shell, such as ls, free, df -h, etc., you are specifying them without an absolute file name.

The shell thus first has to locate the commands on disk to be able to execute them.

The directories it will search for commands are found in the environment variable PATH, separated by colons (:).

Here’s an example of seeing, and setting, a PATH:

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

export PATH="$PATH:$HOME/go/bin/:$HOME/bin"

Modifying the variable will only affects the current shell, unless the variable was exported, in which case it would be inherited by subprocesses started from the parent shell.

To make changes be present in all shells you run, add them to one of the initialization files that are executed at the start of every shell process. Those files are ~/.profile or ~/.bash_profile for interactive (login) shells, and ~/.bashrc for all others.

Notably, Unix PATH does not include ., the current directory. This is because behavior should be deterministic, and not vary depending on which directory you might be in at the moment. That’s especially true for admins, since otherwise users could trick them into cd-ing into particular directories and inadvertently executing commands there.

To run files that are in your current directory, prefix them with ./.

Running Commands Periodically

Often times you want to run commands in a loop.

This could be, for example, to monitor network traffic, or disk usage, and so on.

Let’s say you cd into a particular directory and want to keep running du -s in it, to keep track of total size as you are copying some files.

You could do this the hard way, using a shell loop such as:

while [ 1 ]; do
	du -s
	sleep 1
end

Luckily, there is a command watch which takes care of everything itself:

watch -n1 du -s

System Stats

Basic system runtime stats can be obtained with free; uptime; df -h.

Programs using terminal graphics for displaying similar stats are top and htop. (In top, press 1 to see all CPUs.)

Key Shortcuts in Terminals

  • Arrows Up and Down - navigate between previous typed lines. In some shells or versions, lines which start with a space are not saved in history, so do not appear in the list

  • Ctrl+Left, Ctrl+Right - move one word left and right

  • Ctrl+a, Ctrl+e - go to the beginning and end of line

  • Ctrl+u, Ctrl+k - delete from cursor to beginning of line and end of line

  • Ctrl+r - invoke a search through history of previously typed lines. Press Ctrl+r and start typing any part of the line you want to find. Exit search with Esc

  • Esc+_ - insert last word from the previous line. Keep pressing for going through the history of lines. This is also available on Alt+., but this combination usually doesn’t work due to missing configuration in ~/.inputrcor/etc/inputrc`

  • Ctrl+w - cut one word to left of cursor

  • Ctrl+y - paste whatever is in the buffer

  • Ctrl+t - exchange two letters, useful for typos such as teh[Ctrl+t] -> the`

  • Ctrl+o, Ctrl+j, Ctrl+m - equivalent of pressing Enter to run the command

  • Ctrl+p - same as arrow Up, to invoke previous line

  • Ctrl+f, Ctrl+b - same as arrows Right and Left, move one character to the right or left

  • Ctrl+h - same as backspace, delete character to the left

  • Ctrl+d - same as delete when in command line. When attached to program, sends End-of-Input control character

  • Ctrl+\ - when attached to program, sends Quit control character (useful when Ctrl+c doesn’t suffice)

  • Ctrl+z - when attached to program, suspend it and return to shell. Then use command fg to go back to program, bg to resume it in background, and/or jobs to see current jobs

  • Ctrl+c - cancel current command, or when attached to program, send break control char to program

  • Ctrl+v - visual/literal. Display next character instead of interpreting it. Try e.g. Ctrl+v, Enter, to see Enter printed as ^M rather than interpreted as new line

  • Ctrl+n - empty the current line, start anew

  • Ctrl+/ - undo of sorts

Keyboard Language

Sometimes you want to change the keyboard input locale, but don’t see a graphical button to change it.

In X, this can be done with command setxkbmap, such as setxkbmap us for English or setxkbmap de for German.

In the console, this can be done with an equivalent command called loadkeys.

Tab Completion

The primary interface into Unix is text. But Unix authors did not want to write more text that necessary.

It is very useful to be able to auto-fill, or auto-complete, parts of the command lines that you write.

By default, your shell will auto-complete commands and paths. Type a and press Tab twice. You will see all possible commands starting with a. When you type enough characters to make the choice unambiguous, pressing Tab will complete its word.

Similar for file names. Try typing ls /etc/ and then press Tab twice. You will see possible choices for completion in the directory /etc/.

In more modern times, this basic and always present auto completion has been extended to be more context-sensitive. It can auto-complete arguments to a command, or provide auto-completion choices only for type of files that the command accepts. For example, when this is active, typing cd /etc/ and pressing Tab,Tab will only try to auto-complete further subdirectories inside /etc/, and not all other files found in it. Some people prefer to see all files, even if they are not applicable, rather than a subset of pre-filtered choices.

Shell History

For convenience, shells save a history of commands we typed in to a file in our home directory, such as ~/.bash_history.

This makes history available even after we close and re-open terminals. It is automatically available when we use arrows Up and Down or press Ctrl+r to search through history. It is also available as a list when we run the shell builtin history.

Each line in the history list is numbered. That is the beginning of being able to recall any previous lines or parts of the lines into the current line.

The complete manual is in man history, but here is a summary of most often used functionality:

  • !n - expand into line n from the history list

  • !! - expand into the previous line. Alias for !-1

  • !-n - expand into a line n lines back

  • !string - expand into first line up the history which begins with string

  • !?string? - expand into first line up the history which contains string

  • ^string1^string2^ - in the last command, replace string 1 with string 2, and run it. Alias for !!:s/string1/string2/

In addition to whole lines, after : it is possible to choose individual arguments from those lines, such as:

  • !!:n -word n from previous line

  • !!:^ - first argument (word n + 1) from previous line

  • !!:$ - last argument from previous line, alias of Alt+. or Esc+_

There are also ranges and additional modifiers available. See the mentioned man page history(3) for all details.

Display / Screen Settings

Command xset is a “user preference utility for X”.

Various settings can be controlled through it, e.g. xset b off to turn off audible bell, or xset s off to disable screen blanking after idle timeout.

Zapping X

Similar to keyboard shortcut Ctrl+Alt+Del which reboots the machine (or shows a prompt to do so), in X there is a shortcut for terminating, or “zapping”, the graphics. It is done by pressing Ctrl+Alt+Backspace, which will tear down your current X session. Usually, what will happen afterwards is that your display manager will notice X has exited, and will restart it.

This was a standard and useful shortcut. But, not to “confuse users”, the zap shortcut has been disabled by default a long time ago.

Re-enabling it required adding an Option "DontZap" "false" to the X config file (usually /etc/X11/xorg.conf) as follows:

Section "ServerFlags"
    Option "DontZap" "false"
EndSection

However, that file might not even exist on recent installations (because everything is auto-detected), and creating it just to add these lines requires a restart of X.

Luckily, there is a newer command which can enable or disable this feature on the fly:

setxkbmap -option "terminate:ctrl_alt_bksp"

After executing the above line, pressing Ctrl+Alt+Backspace will terminate X, which will most likely then restart.

Command Line Arguments

You probably know that every command line you type into a terminal is basically an invocation of some program, followed by any additional options for it. E.g., here’s how we invoke program ls with options -al:

ls -al

Well, strictly speaking, the first word can be a program, alias, keyword, shell builtin, or a shell function. This can be tested and displayed with command type. E.g.:

type  /bin/ls ls [[ help type

/bin/ls is /bin/ls    # (This means it is a program, a file)
ls is aliased to `ls --color=auto'
[[ is a shell keyword
help is a shell builtin
type is a shell builtin

Unix has chosen not to declare how programs should be named and which arguments they should accept. That’s why each program can accept options in a way that makes the most sense for it.

Often times, this means accepting options/arguments anywhere on the line, even including among other parameters. For example, the following lines are all equal because commands like cp, mv support it:

cp -i -a /some/dir /another/dir/
cp /some/dir /another/dir/ -i -a
cp /some/dir -i /another/dir/ -a
cp -a /some/dir -i /another/dir/

Options usually begin with -, but that is just one of standard methods. Problems may occur when files are named starting with -, or any other character that the program will interpret as options.

For example:

mkdir /tmp/test
cd /tmp/test
touch ./-myfile
rm -myfile

rm: invalid option -- 'm'
Try 'rm ./-myfile' to remove the file '-myfile'.
Try 'rm --help' for more information.

Instead of this error, you were probably expecting that the command would delete the file “-myfile”.

There are solutions to it:

By convention, when you want to signify the end of options and the beginning of parameters, you use --. This is equally useful when using the shell interactively and writing scripts.

So, the preceding line should have been written as:

rm -- -myfile

Or, equally functional, but not as robust:

rm ./-myfile

Specifying Directories

In the shell, when you are specifying any paths and want those paths to be treated as directories, always specify them with an ending /.

For example:

cp my* /var/tmp/

It is the only way to ensure that paths are treated as directories. For example, consider the following examples:

find /some/path -name '*test*'

If /some/path was a symlink, without the ending / the command would not enter the directory, and search would return no results.

cp myfiles* /some/where

If /some/where wasn’t a directory, or if it didn’t exist, you would expect to receive an error. But if myfiles* expanded to just one file, then the command would in effect copy that one file into or over file /some/where, rather than into the directory /some/where/.

There are probably other examples. But as a quick and simple rule, always end paths you intend to be directories with /.

Creating And Emptying Files

When you need to create an empty file, usually you might do something like echo > myfile.

However, that creates not a file with size 0 bytes, but a file of size 1 byte, because echo by default adds a newline to the end. If it makes a practical difference for you, or simply if you want to do it right, to prevent the newline you should at least use echo -n > myfile.

Another option to create empty files is the command touch, such as touch myfile. This command creates files as a side-effect; if a file already exists, then it will update its last modification date to the current time, without modifying its content. So this command can’t be used to empty existing files. Also, touch is a command so using it requires starting a new process.

Finally, the third option is to use shell redirection alone. Typing just > myfile will create or empty an existing file.

IP Addresses and Ports

Often times in tutorials users are instructed to go to e.g. http://localhost:80/.

Sometimes most of that text can be shortened as follows:

  • If the protocol (http:// in this case) is default for the command you are running, it can be omitted

  • If the port (80 in this case) is the default for the command you are running, it can be omitted. For example, it is unnecessary to be connecting to a Postgres database at port 5432 explicitly, because that is its default

  • The hostname localhost is acceptably convenient to write. But, localhost can also be written as 127.0.0.1, 127.1, and 0. The 0 is very little known, yet very convenient

So in summary, in most cases the the above example can can be reduced to http://0.

Automatic Links

The following links appear in the article:

1. Linux Virtual Consoles - /linux-virtual-consoles/
2. Sun Type 6 Keyboard - https://deskthority.net/wiki/Sun_Type_6
3. Unix Philosophy - https://en.wikipedia.org/wiki/Unix_Philosophy
4. X Window System - https://en.wikipedia.org/wiki/X_window_system