Unix
Unix
In An Introduction to the Command-Line (on Unix-like systems) [1], which I'll borrow
heavily from in this article, we covered my subjective list of the 20 most important
command line utilities. In case you forgot, it was:
pwd
ls
cd
mkdir
echo
cat
cp
mv
rm
man
head
tail
less
more
sort
grep
which
chmod
history
clear
These are indispensable but, for anything more than the basics, a vocabulary of about
100 commands is where you want to be. Here are my picks for the top 100(ish)
commands. I'll briefly introduce each one below along with a quick use case or hint.
A few of the commands, mostly clustered at the end of the article, are not standard shell
commands and you have to install them yourself (see apt-get, brew, yum). When this
is the case, I make a note of it.
[1] Note: Unix-like systems include Linux and Macintosh ↑
To see where we are, we can print working directory, which returns the path of the
directory in which we currently reside:
$ pwd
Sometimes in a script we change directories, but we want to save the directory in which
we started. We can save the current working directory in a variable with command
substitution:
# do something else
$ readlink -m .
cd TOP VIEW_AS_PAGE
$ cd /some/path
$ cd / # go to root
What if you want to visit the directory you were in two or three directories ago? The
utilities pushd and popd keep track of the directories you move through in a stack.
When you use:
$ pushd some_directory
It acts as a:
$ cd some_directory
except that some_directory is also added to the stack. Let's see an example of how to
use this:
~/TMP ~
~/DATA ~/TMP ~
$ pushd ~ # now we're in ~/DATA
~ ~/DATA ~/TMP ~
~/DATA ~/TMP ~
~/TMP ~
$ # now we're in ~/
This is interesting to see once, but I never use pushd and popd. Instead, I like to use a
function, cd_func, which I stole off the internet. It allows you to scroll through all of your
past directories with the syntax:
$ cd --
Here's the function, which you should paste into your setup dotfiles (e.g., .bash_profile):
cd_func ()
local -i cnt;
dirs -v;
return 0;
fi;
the_new_dir=$1;
[[ -z $1 ]] && the_new_dir=$HOME;
index=${the_new_dir:1};
adir=$(dirs +$index);
the_new_dir=$adir;
fi;
the_new_dir=$(pwd);
do
cnt=cnt-1;
fi;
done;
return 0
alias cd=cd_func
$ cd --
0 ~
1 ~/DATA
2 ~/TMP
to see all the directories you've visited. To go into ~/TMP, for example, enter:
$ cd -2
Changing the subject and dipping one toe into the world of shell scripting, you can
put cd into constructions like this:
cd $outputdir || exit
This can be a useful line to include in a script. If the user gives an output directory as an
argument and the directory doesn't exist, we exit. If it does exist, we cd into it and it's
business as usual.
ls TOP VIEW_AS_PAGE
ls lists the files and directories in the cwd, if we leave off arguments. If we pass
directories to the command as arguments, it will list their contents. Here are some
common flags.
List in long form—show file permissions, the owner of the file, the group to which he
belongs, the date the file was created, and the file size:
$ ls -l # long form
List in human-readable (bytes will be rounded to kilobytes, gigabytes, etc.) long form:
List in human-readable long form sorted by the time the file was last modified:
The coloring is key. It will color directories and files and executables differently, allowing
you to quickly scan the contents of your folder.
Note that you can use an arbitrary number of arguments and that bash uses the
convention that an asterik matches anything. For example, to list only files with
the .txt extension:
$ ls *.txt
This monstrosity would list anything in the cwd; anything in directory dir1; anything in
the directory one above us; anything in directory dir2 that ends with .txt; and anything in
directory dir3 that starts with A and ends with .html. You get the point!
With this construct, we could do some series of commands on all .txt files in /some/path.
You can also do a very similar thing without ls:
$ for i in /some/path/*.txt; do echo $i; done
$ mkdir mynewfolder
To make nested directories (and don't complain if trying to make a directory that already
exists), use the -p flag:
However, since the directory must be empty to use this command, it's not convenient.
Instead, use:
Since rm has all the functionality you need, I know few people who actually use rmdir.
About the only occasion to use it is if you want to be careful you're not deleting a
directory with stuff in it. (Perhaps rmdir isn't one of the 100 most useful commands,
after all.)
$ echo joe
joe
$ echo "joe"
joe
joe joe
$ echo
Supress newline:
joe joe
joe
As we've seen above, if you want to print a string with spaces, use quotes. You should
also be aware of how bash treats double vs single quotes. If you use double quotes,
any variable inside them will be expanded (the same as in Perl). If you use single
quotes, everything is taken literally and variables are not expanded. Here's an example:
$ var=5
$ joe=hello $var
-bash: 5: command not found
That didn't work because we forgot the quotes. Let's fix it:
$ joe="hello $var"
$ echo $joe
hello 5
$ joe='hello $var'
$ echo $joe
hello $var
Sometimes, when you run a script verbosely, you want to echo commands before you
execute them. A std:out log file of this type is invaluable if you want to retrace your
steps later. One way of doing this is to save a command in a variable, cmd, echo it, and
then pipe it into bash or sh. Your script might look like this:
cmd="ls -hl";
echo $cmd;
$ cat file.txt
Some people deride this as unnecessarily verbose, but I'm so used to piping anything
and everything that I embrace it. Another common construction is:
cat file.txt | awk ...
We'll discuss awk below, but the key point about it is that it works line by line.
So awk will process what cat pipes out in a linewise fashion.
hello kitty
The -vet flag allows us to "see" special characters, like tab, newline, and carriage
return:
^I$
$
$ echo -e "\r" | cat -vet
^M$
This can come into play if you're looking at a file produced on a PC, which uses the
horrid \r at the end of a line as opposed to the nice unix newline, \n. You can do a
similar thing with the command od, as we'll see below .
There are two variations on cat, which occasionally come in handy. zcat allows you
to cat zipped files:
$ zcat file.txt.gz
You can also see a file in reverse order, bottom first, with tac (tac is cat spelled
backwards).
cp TOP VIEW_AS_PAGE
$ cp file1 file2
$ cp -R dir1 ../../
Answer: it would make a copy of dir1 up two levels from our current working directory.
Tip: If you're moving a large directory structure with lots of files in
them, use rsync instead of cp. If the command fails midway through, rsync can start
from where it left off but cp can't.
mv TOP VIEW_AS_PAGE
$ mv file1 file2
In a sense, this command also moves files, because we can rename a file into a
different path. For example:
$ mv file1 dir1/dir2/file2
would move file1 into dir1/dir2/ and change its name to file2, while:
$ mv file1 dir1/dir2/
would simply move file1 into dir1/dir2/ (or, if you like, rename ./file1 as ./dir1/dir2/file1).
$ mv a a.1
$ mv b a
$ mv a.1 b
$ mv test.txt test.html
Shortcut:
$ mv test.{txt,html}
mv can be dangerous because, if you move a file into a directory where a file of the
same name exists, the latter will be overwritten. To prevent this, use the -n flag:
rm TOP VIEW_AS_PAGE
If there's a permission issue or the file doesn't exist, rm will throw an error. You can
override this with the force flag, -f:
You may be aware that when you delete files, they can still be recovered with effort if
your computer hasn't overwritten their contents. To securely delete your files—meaning
overwrite them before deleting—use:
or use shred.
For example:
man shows the usage manual, or help page, for a command. For example, to see the
manual for ls:
$ man ls
$ man man
The manual pages will list all of the command's flags which usually come in a one-dash-
one-letter or two-dashes-one-word flavor:
command -f
command --flag
A related command discussed below is info.
Based off of An Introduction to the Command-Line (on Unix-like systems) - head and
tail: head and tail print the first or last n lines of a file, where n is 10 by default. For
example:
These are great alternatives to cat, because often you don't want to spew out a giant
file. You only want to peek at it to see the formatting, get a sense of how it looks, or
hone in on some specific portion.
If you combine head and tail together in the same command chain, you can get a
specific row of your file by row number. For example, print row 37:
$ cat hello.txt
hello
hello
hello
$ cat kitty.txt
kitty
kitty
kitty
hello
hello
hello
kitty
kitty
kitty
head will print out the file names when it takes multiple arguments, as in:
hello
hello
kitty
kitty
which is useful if you're previewing many files. To preview all files in the current
directory:
$ head *
$ ls | head
less, zless, more TOP VIEW_AS_PAGE
Based off of An Introduction to the Command-Line (on Unix-like systems) - less and
more: less is, as the man pages say, "a filter for paging through text one screenful at a
time...which allows backward movement in the file as well as forward movement." This
makes it one of the odd unix commands whose name seems to bear no relation to its
function. If you have a big file, vanilla cat is not suitable because printing thousands of
lines of text to stdout will flood your screen. Instead, use less, the go-to command for
viewing files in the terminal:
Another nice thing about less is that it has many Vim-like features, which you can read
about on its man page (and this is not a coincidence). For example, if you want to
search for the word apple in your file, you just type slash ( / ) followed by apple.
If you have a file with many columns, it's hard to view in the terminal. A neat less flag
to solve this problem is -S:
This enables horizontal scrolling instead of having rows messily wrap onto the next line.
As we'll discuss below, this flag works particularly well in combination with
the column command, which forces the columns of your file to line up nicely:
I included less and more together because I think about them as a pair, but I told a
little white lie in calling more indispensible: you really only need less, which is an
improved version of more. Less is more :-)
grep, egrep TOP VIEW_AS_PAGE
Also useful are what I call the ABCs of Grep—that's After, Before, Context. Here's what
they do:
# as well as 1 after
# as well as 2 before
You can do an inverse grep with the -v flag. Find lines that don't contain apple:
$ grep -v apple myfile.txt # return lines that don't
contain apple
Find any occurrence of apple in any file in a directory with the recursive flag:
Exit after, say, finding the first two instances of apple so you don't waste more time
searching:
There are more powerful variants of grep, like egrep, which permits the use of regular
expressions, as in:
as well as other fancier grep-like tools, such as ack, available for download. For some
reason—perhaps because it's useful in a quick and dirty sort of a way and doesn't have
any other meanings in English—grep has inspired a peculiar cult following. There are
grep t-shirts, grep memes, a grep function in Perl, and—unbelievably—even a whole
O'Reilly book devoted to the command:
(Image credit: O'Reilly Media)
Tip: If you're a Vim user, running grep as a system command within Vim is a neat way
to filter text. Read more about it in Wiki Vim - System Commands in Vim.
Tip: Recently a grep-like tool called fzf has become popular. The authors call it a
"general-purpose command-line fuzzy finder".
which shows you the path of a command in your PATH. For example, on my computer:
$ which less
/usr/bin/less
$ which cat
/bin/cat
$ which rm
/bin/rm
If there is more than one of the same command in your PATH, which will show you the
one which you're using (i.e., the first one in your PATH). Suppose your PATH is the
following:
$ echo $PATH
/home/username/mydir:/usr/local/bin:/bin:/usr/bin:/usr/local/s
bin
$ ls /home/username/mydir/myscript.py
/home/username/mydir/myscript.py
$ ls /usr/local/bin/myscript.py
/usr/local/bin/myscript.py
Then:
$ which myscript.py
My friend, Albert, wrote a nice tool called catwhich, which cats the file returned
by which:
#!/bin/bash
file=$(which $1 2>/dev/null)
cat $file
else
fi
This is useful for reading files in your PATH without having to track down exactly what
directory you put them in.
u - user
g - group
o - other/world
Everyone with an account on the computer is a unique user (see whoami) and,
although you may not realize it, can be part of various groups, such as a particular lab
within a university or a team in a company. There are also three types of permission:
r - read
w - write
x - execute
$ ls -hl myfile
We can mix and match permission types and entities how we like, using a plus sign to
grant permissions according to the syntax:
chmod entity+permissiontype
or a minus sign to remove permissions:
chmod entity-permissiontype
E.g.:
If you find the above syntax cumbersome, there's a numerical shorthand you can use
with chmod. The only three I have memorized are 000, 777, and 755:
$ chmod 000 myfile # revoke all permissions (---------)
Read more about the numeric code here. In general, it's a good practice to allow your
files to be writable by you alone, unless you have a compelling reason to share access
to them.
chown, less commonly seen than its cousin chmod, changes the owner of a file. As an
example, suppose myfile is owned by root, but you want to grant ownership
to ubuntu (the default user on ubuntu) or ec2-user (the default user on Amazon Linux).
The syntax is:
If you want to change both the user to someuser and the group to somegroup, use:
If you want to change the user and group to your current user, chown works well in
combination with whoami:
$ history
The easiest way to search the history, as we've seen, is to bind the Readline
Function history-search-backward to something convenient, like the up arrow. Then you
just press up to scroll backwards through your history. So, if you enter a c, pressing up
will step through all the commands you entered that began with c. If you want to find a
command that contains the word apple, a useful tool is reverse intelligent search, which
is invoked with Cntrl-r:
(reverse-i-search)`':
Although we won't cover unix pipelines until the next section, I can't resist giving you a
sneak-preview. An easier way to find any commands you entered containing apple is:
If you wanted to see the last 10 commands you entered, it would be:
$ history | tail
What's going on under the hood is somewhat complicated. Bash actually records your
command history in two ways: (1) it stores it in memory—the history list—and (2) it
stores it in a file—the history file. There are a slew of global variables that mediate the
behavior of history, but some important ones are:
$ history | tail
$ tail ~/.bash_history
The former will show you the last 10 commands you entered in your current session
while the later will show you last 10 commands from the previous session. This is all
well and good, but let's imagine the following scenario: you're like me and you have 5 or
10 different terminal windows open at the same time. You're constantly switching
windows and entering commands, but history-search is such a useful function that you
want any command you enter in any one of the 5 windows to be immediately accessible
on all the others. We can accomplish this by putting the following lines in our setup
dotfile (.bash_profile):
shopt -s histappend
How does this bit of magic work? Here's what the histappend option does, to quote from
the man page of shopt:
If set, the history list is appended to the history file when the shell exits, rather than overwriting the
history file.
shopt -s histappend
To append every line to history individually set:
PROMPT_COMMAND='history -a'
With these two settings, a new shell will get the history lines from all previous shells instead of the
default 'last window closed'>history (the history file is named by the value of the HISTFILE variable)
Let's break this down. shopt stands for "shell options" and is for enabling and disabling
various miscellaneous options. The
history -a
part will "Append the new history lines (history lines entered since the beginning of the
current Bash session) to the history file" (as the man page says). And the global
variable PROMPT_COMMAND is a rather funny one that executes whatever code is stored
in it right before each printing of the prompt. Put these together and you're immediately
updating the ~/.bash_history file with every command instead of waiting for this to
happen at logout. Since every shell can contribute in real time, you can run multiple
shells in parallel and have access to a common history on all of them—a good situation.
What if you are engaged in unsavory behavior and want to cover your tracks? You can
clear your history as follows:
However, this only deletes what's in memory. If you really want to be careful, you had
better check your history file, .bash_history, and delete any offending portions. Or just
wipe the whole thing:
A wise man once said, "Those who don't know history are destined to [re-type] it." Often
it's necessary to retrace your footsteps or repeat some commands. In particular, if
you're doing research, you'll be juggling lots of scripts and files and how you produced a
file can quickly become mysterious. To deal with this, I like to selectively keep shell
commands worth remembering in a "notes" file. If you looked on my computer, you'd
see notes files scattered everywhere. You might protest: but these are in your history,
aren't they? Yes, they are, but the history is gradually erased; it's clogged with trivial
commands like ls and cd; and it doesn't have information about where the command
was run. A month from now, your ~/.bash_history is unlikely to have that long command
you desperately need to remember, but your curated notes file will.
alias n="history | tail -2 | head -1 | tr -s ' ' | cut -d' ' -f3- | awk
'{print \"# \"\$0}' >> notes"
or equivalently:
alias n="echo -n '# ' >> notes && history | tail -2 | head -1 | tr -s ' '
| cut -d' ' -f3- >> notes"
$ n
Now a notes file has been created (or appended to) in our cwd and we can look at it:
$ cat notes
$ clear
It gives you a clean slate without erasing any of your commands, which are still
preserved if you scroll up. clear works in some other shells, such as ipython's. In most
shells—including those where clear doesn't work, like MySQL's and Python's—you
can use Cntrl-l to clear the screen.
Both logout and exit do exactly what their name suggests—quit your current
session. Another way of doing the same thing is the key combination Cntrl-D, which
works across a broad variety of different shells, including Python's and MySQL's.
Based off of An Introduction to the Command-Line (on Unix-like systems) - sudo and
the Root User: sudo and the Root User sounds like one of Aesop's Fables, but the
moralist never considered file permissions and system administration a worthy subject.
We saw above that, if you're unsure of your user name, the command whoami tells you
who you're logged in as. If you list the files in the root directory:
$ ls -hl /
you'll notice that these files do not belong to you but, rather, have been created by
another user named root. Did you know you were sharing your computer with
somebody else? You're not exactly, but there is another account on your computer for
the root user who has, as far as the computer's concerned, all the power (read
Wikipedia's discussion about the superuser here). It's good that you're not the root user
by default because it protects you from yourself. You don't have permission to
catastrophically delete an important system file.
If you want to run a command as the superuser, you can use sudo. For example, you'll
have trouble if you try to make a directory called junk in the root directory:
$ mkdir /junk
mkdir: cannot create directory ‘/junk’: Permission denied
However, if you invoke this command as the root user, you can do it:
provided you type in the password. Because root—not your user—owns this file, you
also need sudo to remove this directory:
$ sudo -i
oliver
Password:
root
logout
oliver
Obviously, you should be cautious with sudo. When might using it be appropriate? The
most common use case is when you have to install a program, which might want to
write into a directory root owns, like /usr/bin, or access system files. I discuss
installing new software below. You can also do terrible things with sudo, such as gut
your whole computer with a command so unspeakable I cannot utter it in syntactically
viable form. That's sudo are em dash are eff forward slash—it lives in infamy in
an Urban Dictionary entry.
You can grant root permissions to various users by tinkering with the configuration file:
/etc/sudoers
which says: "Sudoers allows particular users to run various commands as the root user,
without needing the root password." Needless to say, only do this if you know what
you're doing.
su TOP VIEW_AS_PAGE
su switches the user you are in the terminal. For example, to change to the user jon:
$ su jon
wc TOP VIEW_AS_PAGE
Count lines:
$ cat myfile.txt
aaa
bbb
ccc
ddd
$ cat myfile.txt | wc -l
Count words:
$ echo -n joe | wc -w
Count characters:
$ echo -n joe | wc -c
$ cat testsort.txt
vfw 34 awfjo
a4 2
f 10 10
beb 43 c
f2 33
f1 ?
Then:
$ sort testsort.txt
a4 2
beb 43 c
f1 ?
f 10 10
f2 33
vfw 34 awfjo
What happened? The default behavior of sort is to dictionary sort the rows of a file
according to what's in the first column, then second column, and so on. Where the first
column has the same value—f in this example—the values of the second column
determine the order of the rows. Dictionary sort means that things are sorted as they
would be in a dictionary: 1,2,10 gets sorted as 1,10,2. If you want to do a numerical sort,
use the -n flag; if you want to sort in reverse order, use the -r flag. You can also sort
according to a specific column. The notation for this is:
sort -kn,m
where n and m are numbers which refer to the range column n to column m. In practice,
it may be easier to use a single column rather than a range so, for example:
sort -k2,2
means sort by the second column (technically from column 2 to column 2).
f1 ?
f2 33
a4 2
f 10 10
vfw 34 awfjo
beb 43 c
vfw 34 awfjo
f1 ?
f2 33
f 10 10
beb 43 c
a4 2
Answer: the file has been sorted first by the first column, in reverse dictionary order, and
then—where the first column is the same—by the second column in numerical order.
You get the point!
Sort uniquely:
Behind the curtain, sort does its work by making temporary files, and it needs a place
to put those files. By default, this is the directory set by TMPDIR, but if you have a giant
file to sort, you might have reason to instruct sort to use another directory and that's
what this flag does.
$ sort -g testsort.txt
sort works particularly well with uniq. For example, look at the following list of
numbers:
6
6
If you have a Macintosh, you can allow users to ssh into your personal computer by
turning ssh access on. Go to:
System Preferences > Sharing > Remote Login
ssh with the flag -Y to enable X11 (X Window System) forwarding:
$ ssh -Y [email protected]
ssh also allows you to run a command on the remote server without logging in. For
instance, to list of the contents of your remote computer's home directory, you could
run:
Cool, eh?
The file:
~/.ssh/config
determines ssh's behavior and you can create it if it doesn't exist. In the next section,
you'll see how to modify this config file to use an IdentityFile, so that you're spared the
annoyance of typing in your password every time you ssh (see also Nerderati: Simplify
Your Life With an SSH Config File).
If you're frequently getting booted from a remote server after periods of inactivity, trying
putting something like this into your config file:
ServerAliveInterval = 300
If this is your first encounter with ssh, you'd be surprised how much of the work of the
world is done by ssh. It's worth reading the extensive man page, which gets into
matters of computer security and cryptography.
On your own private computer, you can ssh into a particular server without having to
type in your password. To set this up, first generate rsa ssh keys:
$ ssh [email protected]
Host Myserver
HostName myserver.com
User myusername
IdentityFile ~/.ssh/localkey
$ ssh Myserver
You can also use this technique to push to github.com, without having to punch your
password in each time, as described here.
If you have ssh access to a remote computer and want to copy its files to your local
computer, you can use scp according to the syntax:
scp username@host:/some/path/on/remote/machine /some/path/on/my/machine
However, I would advise using rsync instead of scp: it does the same thing only
better.
rsync can remotely sync files to or from a computer to which you have ssh access.
The basic syntax is:
rsync source destination
For example, copy files from a remote machine:
$ rsync username@host:/some/path/on/remote/machine
/some/path/on/my/machine/
$ rsync /some/path/on/my/machine
username@host:/some/path/on/remote/machine/
You can also use it to sync two directories on your local machine:
$ rsync directory1 directory2
The great thing about rsync is that, if it fails or you stop it in the middle, you can re-
start it and it will pick up where it left off. It does not blindly copy directories (or files) but
rather syncronizes them—which is to say, makes them the same. If, for example, you
try to copy a directory that you already have, rsync will detect this and transfer no files.
The -a flag is for archive, which "ensures that symbolic links, devices, attributes,
permissions, ownerships, etc. are preserved in the transfer"; the -z flag compresses files
during the transfer; -v is for verbose; and --progress shows you your progress. I've
enshrined this in an alias:
In this example, the directory /my/source/mydir will not be copied, and you can omit
more directories by repeating the --exclude flag.
Copy the files pointed to by the symbolic links ("transform symlink into referent file/dir")
with the --L flag:
$ rsync -azv -L --progress [email protected]:/my/source
/my/destination/
Note: using a trailing slash can alter the behavior of rsync. For example:
$ mkdir folder1 folder2 folder3 # make directories
$ tree folder2
folder2
└── folder1
├── file1
└── file2
whereas using a trailing slash copies the files in folder1 into folder2:
$ tree folder3
folder3
├── file1
└── file2
#!/bin/bash
myvariable=54
echo $myvariable
If we run it and then check what happened to the variable on our command line, we get:
$ ./test_src.sh
54
$ echo $myvariable
The variable is undefined. The command source is for solving this problem. If we want
the variable to persist, we run:
$ source ./test_src.sh
54
$ echo $myvariable
54
and—voilà!—our variable exists in the shell. An equivalent syntax for sourcing uses a
dot:
$ . ./test_src.sh # this is the same as "source
./test_src.sh"
54
But now observe the following. We'll make a new script, test_src_2.sh, such that:
$ cat ./test_src_2.sh
#!/bin/bash
echo $myvariable
$ ./test_src_2.sh
Nothing! So $myvariable is defined in the shell but, if we run another script, its existence
is unknown. Let's amend our original script to add in an export:
$ cat ./test_src.sh
#!/bin/bash
echo $myvariable
54
$ ./test_src_2.sh
$ source ./test_src.sh
54
$ ./test_src_2.sh
54
So, at last, we see how to do this. If we want access on the shell to a variable which is
defined inside a script, we must source that script. If we want other scripts to have
access to that variable, we must source plus export.
ln TOP VIEW_AS_PAGE
$ ln -s /path/to/target/file mylink
This produces:
$ rm mylink
If we give the target (or source) path as the sole argument to ln, the name of the link
will be the same as the source file's. So:
$ ln -s /path/to/target/file
produces:
Links are incredibly useful for all sorts of reasons—the primary one being, as we've
already remarked, if you want a file to exist in multiple locations without having to make
extraneous, space-consuming copies. You can make links to directories as well as files.
Suppose you add a directory to your PATH that has a particular version of a program in
it. If you install a newer version, you'll need to change the PATH to include the new
directory. However, if you add a link to your PATH and keep the link always pointing to
the most up-to-date directory, you won't need to keep fiddling with your PATH. The
scenario could look like this:
$ ls -hl myprogram
version1
version2
version3
(where I'm hiding some of the output in the long listing format.) In contrast to our other
examples, the link is in the same directory as the target. Its purpose is to tell us which
version, among the many crowding a directory, we should use.
Another good practice is putting links in your home directory to folders you often use.
This way, navigating to those folders is easy when you log in. If you make the link:
~/MYLINK -->
/some/long/and/complicated/path/to/an/often/used/directory
$ cd MYLINK
rather than:
$ cd
/some/long/and/complicated/path/to/an/often/used/directory
$ readlink -m mydir
$ readlink -m .
Note this is different than:
$ pwd
because I'm using the term absolute path to express not only that the path is not a
relative path, but also to denote that it's free of symbolic links. So if we have the link
discussed above:
~/MYLINK -->
/some/long/and/complicated/path/to/an/often/used/directory
then:
$ cd ~/MYLINK
$ readlink -m .
/some/long/and/complicated/path/to/an/often/used/directory
One profound annoyance: readlink doesn't work the same way on Mac unix (Darwin).
To get the proper one, you need to download the GNU coreutils.
git is a famous utility for version control in software development written by Linus
Torvalds. Everyone doing serious software development uses version control, which is
related to the familiar concept of saving a file. Suppose you're writing an article or a
computer program. As you progress, you save the file at different checkpoints ("file_v1",
"file_v2", "file_v3"). git is an extension of this idea except, instead of saving a single
file, it can take a snapshot of the state of all the files and folders in your project
directory. In git jargon, this snapshot is called a "commit" and, forever after, you can
revisit the state of your project at this snapshot.
The popular site Github took the utility git and added servers in the cloud which (to a
first approximation) respond only to git commands. This allows people to both back up
their code base remotely and easily collaborate on software development.
git is such a large subject (the utility has many sub-commands) I've given it a full post
here.
The command sleep pauses for an amount of time specified in seconds. For example,
sleep for 5 seconds:
$ sleep 5
ps, pstree, jobs, bg, fg, kill, top, htop TOP VIEW_AS_PAGE
What if you have a program that takes some time to run? You run the program on the
command line but, while it's running, your hands are tied. You can't do any more
computing until it's finished. Or can you? You can—by running your program in the
background. Let's look at the command sleep which pauses for an amount of time
specified in seconds. To run things in the background, use an ampersand:
$ sleep 60 &
[1] 6846
$ sleep 30 &
[2] 6847
The numbers printed are the PIDs or process IDs, which are unique identifiers for every
process running on our computer. Look at our processes:
$ ps -f
The header is almost self-explanatory, but what's TTY? Wikipedia tells us that this is an
anachronistic name for the terminal that derives from TeleTYpewriter. Observe that
bash itself is a process which is running. We also see that this particular bash—PID of
5166—is the parent process of both of our sleep commands (PPID stands for parent
process ID). Like directories, processes are hierarchical. Every directory, except for the
root directory, has a parent directory and so every process, except for the first
process—init in unix or launchd on the Mac—has a parent process. Just as tree shows
us the directory hierarchy, pstree shows us the process hierarchy:
$ pstree
-+= 00001 root /sbin/launchd
|-+= 00132 oliver /sbin/launchd
| |-+= 00252 oliver /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
| | \-+= 05165 root login -pf oliver
| | \-+= 05166 oliver -bash
| | |--= 06846 oliver sleep 30
| | |--= 06847 oliver sleep 60
| | \-+= 06848 oliver pstree
| | \--- 06849 root ps -axwwo user,pid,ppid,pgid,command
This command actually shows every single process on the computer, but I've cheated a
bit and cut out everything but the processes we're looking at. We can see the parent-
child relationships: sleep and pstree are children of bash, which is a child of login,
which is a child of Terminal, and so on. With vanilla ps, every process we saw began in
a terminal—that's why it had a TTY ID. However, we're probably running Safari or
Chrome. How do we see these processes as well as the myriad others running on our
system? To see all processes, not just those spawned by terminals, use the -A flag:
Returning to our example with sleep, we can see the jobs we're currently running with
the command jobs:
$ jobs
What if we run our sleep job in the foreground, but it's holding things up and we want
to move it to the background? Cntrl-z will pause or stop a job and the
commands bg and fg set it running in either the background or the foreground.
^Z
sleep 60
$ sleep 60 &
[1] 7161
$ jobs
$ kill %1
In this notation %n refers to the nth job. Recall we can also kill a job in the foreground
with Cntrl-c. More generally, if we didn't submit the command ourselves, we can kill any
process on our computer using the PID. Suppose we want to kill the terminal in which
we are working. Let's grep for it:
We see that this particular process happens to have a PID of 252. grep returned any
process with the word Terminal, including the grep itself. We can be more precise
with awk and see the header, too:
(that's print the first row OR anything with the 2nd field equal to 252.) Now let's kill it:
$ kill 252
i.e., running something in the background and saving both the output and error.
Two other commands that come to mind are time, which times how long your script
takes to run, and nohup ("no hang up"), which allows your script to run even if you quit
the terminal:
(Of course, you'd want to run something more useful than sleep!) If you're lucky
enough to work on a large computer cluster shared by many users—some of whom
may be running memory- and time-intensive programs—then scheduling different users'
jobs is a daily fact of life. At work we use a queuing system called the Sun Grid
Engine to solve this problem. I wrote a short SGE wiki here.
To get a dynamic view of your processes, loads of other information, and sort them in
different ways, use top:
$ top
As we mentioned above, nohup ("no hang up") allows your script to run even if you quit
the terminal, as in:
If you start such a process, you can squelch it by using kill on its process ID or, less
practically, by shutting down your computer. You can track down your process with:
$ ps -Af | grep script.py
nohup is an excellent way to make a job running in the background more robust.
As we mentioned above, time tells you how long your script or command took to run.
This is useful for benchmarking the efficency of your program, among other things. For
example, to time sleep:
$ time sleep 10
real 0m10.003s
user 0m0.000s
sys 0m0.000s
How do you tell how much memory a program or command consumed? Contrary to
what its name suggests, the time command also gives us this information with the -
v flag:
$ time -v sleep 10
$ type time
Confusingly, the reason is that time is actually a keyword, so we can't invoke it with
flags as usual. To use it, we have to call the program by its full path:
$ /usr/bin/time -v sleep 10
Command being timed: "sleep 10"
Swaps: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
$ seq 1 5
5
If you add a number in the middle of your seq range, this will be the "step":
$ seq 1 2 10
cut cuts one or more columns from a file, and delimits on tab by default. Suppose a
file, sample.blast.txt, is:
Then:
gi|27151736|ref|NP_006727.2|
gi|55749932|ref|NP_001918.3|
gi|157785645|ref|NP_005867.3|
gi|157785645|ref|NP_005867.3|
NP_006727.2
NP_001918.3
NP_005867.3
NP_005867.3
although this is long-winded and in this case we can achieve the same result simply
with:
NP_006727.2
NP_001918.3
NP_005867.3
NP_005867.3
Don't confuse cut with its non-unix namesake on Macintosh, which deletes text while
copying it to the clipboard.
Tip: If you're a Vim user, running cut as a system command within Vim is a neat way to
filter text. Read more: Wiki Vim - System Commands in Vim.
$ cat file1.txt
$ cat file2.txt
a1
b2
c3
a;1
b;2
c;3
As with cut, paste's non-unix namesake on Macintosh—printing text from the
clipboard—is a different beast entirely.
A neat feature of paste is the ability to put different rows of a file on the same line. For
example, if the file sample.fa is:
>TCONS_00046782
FLLRQNDFHSVTQAGVQWCDLGSLQSLPPRLKQISCLSLLSSWDYRHRPPHPAFFLFFFLF
>TCONS_00046782
MRWHMPIIPALWEAEVSGSPDVRSLRPTWPTTPSLLKTKNKTKQNISWAWCMCL
>TCONS_00046782
MFCFVLFFVFSRDGVVGQVGLKLLTSGDPLTSASQSAGIIGMCHRIQPWLLIY
>TCONS_00046782
FLLRQNDFHSVTQAGVQWCDLGSLQSLPPRLKQISCLSLLSSWDYRHRPPHPAFFLFFFLF
>TCONS_00046782
MRWHMPIIPALWEAEVSGSPDVRSLRPTWPTTPSLLKTKNKTKQNISWAWCMCL
>TCONS_00046782
MFCFVLFFVFSRDGVVGQVGLKLLTSGDPLTSASQSAGIIGMCHRIQPWLLIY
The key point about awk is, it works line by line. A typical awk construction is:
cat file.txt | awk '{ some code }'
Awk executes its code once every line. Let's say we have a file, test.txt, such that:
$ cat test.txt
1c
3c
2t
1c
In awk, the notation for the first field is $1, $2 is for second, and so on. The whole line
is $0. For example:
1c
3c
2t
1c
3c
2t
1c
There are two exceptions to the execute code per line rule: anything in a BEGIN block
gets executed before the file is read and anything in an END block gets executed after
it's read. If you define variables in awk they're global and persist rather than being
cleared every line. For example, we can concatenate the elements of the first column
with an @ delimiter using the variable x:
@1
@1@3
@1@3@2
@1@3@2@1
@1@3@2@1
Awk has a bunch of built-in variables which are handy: NR is the row number; NF is the
total number of fields; and OFS is the output delimiter. There are many more you can
read about here. Continuing with our very contrived examples, let's see how these can
help us:
1c
3c
2t
1c
1 c
3 c
2 t
1 c
Setting OFS spares us having to type a "\t" every time we want to print a tab. We can
just use a comma instead. Look at the following three examples:
1c
3c
2t
1c
11 c
23 c
32 t
41 c
12 1 c
22 3 c
32 2 t
42 1 c
So the first command prints the file as it is. The second command prints the file with the
row number added in front. And the third prints the file with the row number in the first
column and the number of fields in the second—in our case always two. Although these
are purely pedagogical examples, these variables can do a lot for you. For example, if
you wanted to print the 3rd row of your file, you could use:
$ cat test.txt | awk '{if (NR==3) {print $0}}' # print the 3rd row of your
file
2 t
$ cat test.txt | awk '{if (NR==3) {print}}' # same thing, more compact
syntax
2 t
2 t
Sometimes you have a file and you want to check if every row has the same number of
columns. Then use:
t
c
An important point is that by default awk delimits on white-space, not tabs (unlike,
say, cut). White space means any combination of spaces and tabs. You can tell awk to
delimit on anything you like by using the -F flag. For instance, let's look at the following
situation:
a b
When we feed a space b into awk, $1 refers to the first field, a. However, if we explicitly
tell awk to delimit on tabs, then $1 refers to a b because it occurs before a tab.
You can also use shell variables inside your awk by importing them with the -v flag:
$ x=hello
hello 1 c
hello 3 c
hello 2 t
hello 1 c
$ cat test.txt | awk '{if ($1==1) {print > "file1.txt"} else {print >
"file2.txt"}}'
$ cat file1.txt
1 c
1 c
$ cat file2.txt
3 c
2 t
Question: In the following case, how would you print the row numbers such that the first
field equals the second field?
$ echo -e "a\ta\na\tc\na\tz\na\ta"
aa
ac
az
aa
Here's the answer:
Question: How would you print the average of the first column in a text file?
The take-home lesson is, you can do tons with awk, but you don't want to do too much.
Anything that you can do crisply on one, or a few, lines is awk-able. For more involved
scripting examples, see An Introduction to the Command-Line (on Unix-like systems) -
More awk examples.
From An Introduction to the Command-Line (on Unix-like systems) - sed: Sed, like awk,
is a full-fledged language that is convenient to use in a very limited sphere (GNU Sed
Guide). I mainly use it for two things: (1) replacing text, and (2) deleting lines. Sed is
often mentioned in the same breath as regular expressions although, like the rest of the
world, I'd use Perl and Python when it comes to that. Nevertheless, let's see what sed
can do.
Sometimes the first line of a text file is a header and you want to remove it. Then:
$ cat test_header.txt
This is a header
1 asdf
2 asdf
2 asdf
1 asdf
2 asdf
2 asdf
2 asdf
1,3 is sed's notation for the range 1 to 3. We can't do much more without entering
regular expression territory. One sed construction is:
/pattern/d
where d stands for delete if the pattern is matched. So to remove lines beginning with #:
$ cat test_comment.txt
1 asdf
# This is a comment
2 asdf
# This is a comment
2 asdf
1 asdf
2 asdf
2 asdf
hello X. goodbye X
This is such a useful ability that all text editors allow you to perform find-and-replace as
well. By replacing some text with nothing, you can also use this as a delete:
hello . goodbye
Sed is especially good when you're trying to rename batches of files on the command
line. I often have occasion to use it in for loops:
file1.txt
file2.txt
file3.txt
$ ls
Sed has the ability to edit files in place with the -i flag—that is to say, modify the file
wihout going through the trouble of creating a new file and doing the re-naming dance.
For example, to add the line This is a header to the top of myfile.txt:
$ date
Sat Mar 21 18:23:56 EDT 2014
You can choose among many formats. For example, if the date were March 10, 2012,
then:
$ date "+%y%m%d"
120310
$ date "+%D"
03/10/12
$ date "+%s"
$ cal
January
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
See the calendar for the whole year:
$ cal -y
$ cal 12 2011
It's a good practice to conserve disk space whenever possible and unix has many file
compression utilities for this purpose, gzip and bzip2 among them. File compression
is a whole science unto itself which you can read about here.
Zip a file:
$ gzip file
(The original file disappears and only the .gz file remains)
Unzip:
$ gunzip file.gz
Bunzip:
$ bunzip2 file.bz2
$ zcat file.gz
$ zless file.gz
To emphasize the point again, if you're dealing with large data files, you should always
compress them to save space.
(The original dir remains) The options I'm using are -c for "create a new archive
containing the specified items"; -f for "write the archive to the specified file"; and -v for
verbose.
uniq filters a file leaving only the unique lines, provided the file is sorted. Suppose:
$ cat test.txt
aaaa
bbbb
aaaa
aaaa
cccc
cccc
Then:
aaaa
bbbb
aaaa
cccc
This can be thought of as a local uniquing—adjacent rows are not the same, but you
can still have a repeated row in the file. If you want the global unique, sort first:
$ cat test.txt | sort | uniq
aaaa
bbbb
cccc
aaaa
bbbb
cccc
uniq also has the ability to show you only the lines that are not unique with
the duplicate flag:
aaaa
cccc
And uniq can count the number of distinct rows in a file (provided it's sorted):
3 aaaa
1 bbbb
2 cccc
dirname, basename TOP VIEW_AS_PAGE
$ basename /some/path/to/file.txt
file.txt
$ dirname /some/path/to/file.txt
/some/path/to
The first gets the file name, the second the directory in which the file resides. To say the
same thing a different way, dirname gets the directory minus the file,
while basename gets the file minus the directory.
In a bash script, it's sometimes useful to grab the directory where the script itself resides
and store this path in a variable:
Use set to set various properties of your shell, somewhat analogous to a Preferences
section in a GUI.
$ set -o emacs
This causes all commands to be echoed to std:err before they are run. For example,
consider the following script:
#!/bin/bash
set -eux
sleep 5
echo joe
This will echo every command before running it. The output is:
+ echo hello
hello
+ sleep 5
+ echo joe
joe
$ TEST=asdf
$ echo $TEST
asdf
$ unset TEST
$ echo $TEST
There are 3 notable things about env. First, if you run it as standalone, it will print out all
the variables and functions set in your environment:
$ env
Second, as discussed in An Introduction to the Command-Line (on Unix-like systems) -
The Shebang, you can use env to avoid hard-wired paths in your shebang. Compare
this:
#!/usr/bin/env python
to this:
#!/some/path/python
And, third, as Wikipedia says, env can run a utility "in an altered environment without
having to modify the currently existing environment." I never have occasion to use it this
way but, since it was in the news recently, look at this example (stolen from here):
$ echo $COLOR
The COLOR variable is only defined temporarily for the purposes of the env statement
(when we echo it afterwards, it's empty). This construction sets us up to understand
the bash shellshock bug, which Stack Exchange illustrates using env:
As its man page says, uname prints out various system information. In the simplest
form:
$ uname
Linux
If you use the -a flag for all, you get all sorts of information:
$ uname -a
df reports "file system disk space usage". This is the command you use to see how
many much space you have left on your hard disk. The -h flag means "human
readable," as usual. To see the space consumption on my mac, for instance:
$ df -h .
df shows you all of your mounted file systems. For example, if you're familiar
with Amazon Web Services (AWS) jargon, df is the command you use to examine your
mounted EBS volumes. They refer to it in the "Add a Volume to Your Instance" tutorial.
du is similar to df but it's for checking the sizes of individual directories. E.g.:
$ du -sh myfolder
284M myfolder
If you wanted to check how much space each folder is using in your HOME directory, you
could do:
$ cd
$ du -sh *
This will probably take a while. Also note that there's some rounding in the calculation of
how much space folders and files occupy, so the numbers df and du return may not be
exact.
Find all the files in the directory /my/dir in the gigabyte range:
For the first two, you can use the default Emacs way:
However, reaching for the Esc key is a royal pain in the ass—you have to re-position
your hands on the keyboard. This is where key-binding comes into play. Using the
command bind, you can map a Readline Function to any key combination you like. Of
course, you should be careful not to overwrite pre-existing key bindings that you want to
use. I like to map the following keys to these Readline Functions:
Cntrl-forward-arrow - forward-word
Cntrl-backward-arrow - backward-word
up-arrow - history-search-backward
down-arrow - history-search-forward
$ cat file.txt
$ c file.txt
Another use of alias is to weld particular flags onto a command, so every time the
command is called, the flags go with it automatically, as in:
alias cp="cp -R"
or
alias mkdir="mkdir -p"
Recall the former allows you to copy directories as well as files, and the later allows you
to make nested directories. Perhaps you always want to use these options, but this is a
tradeoff between convenience and freedom. In general, I prefer to use new words for
aliases and not overwrite preexisting bash commands. Here are some aliases I use in
my setup file:
# coreutils
alias c="cat"
alias e="clear"
# awk
alias n="history | tail -2 | head -1 | tr -s ' ' | cut -d' ' -f3- | awk
'{print \"# \"\$0}' >> notes"
# HTML-related
alias htmlsed="sed 's|\&|\&\;|g; s|>|\>\;|g; s|<|\<\;|g;'"
# git
$ alias
$ unalias myalias
This puts your file into a nice table, which is what the -t flag stands for. Here's an
illustration:
The file tmp.txt is tab-delimited but, because the length of the fields is so different, it
looks ugly.
With column -t:
If your file has many columns, the column command works particularly well in
combination with:
less -S
which allows horizontal scrolling and prevents lines from wrapping onto the next row:
(where you make a tab in the terminal by typing Cntrl-v tab). This makes the terminal
feel almost like an Excel spreadsheet. Observe how it changes the viewing experience
for this file of fake financial data:
find is for finding files or folders in your directory tree. You can also use it to list the
paths of all subdirectories and files of a directory. To find files, you can use the simple
syntax:
find /some/directory -name "myfile"
For instance, to find all files with the extension .html in the current directory or any of its
sub-directories:
This would find any file or directory containing ALEX, Alex, alex, and so on
in /my/dir and its children.
To see the path of every child file and directory from the cwd on down, simply type:
$ find
This could come into play, for example, when you're trying to clean up your folders. In
addition to how big a folder is (remember du -sh), you might also want to know if the
folder has a gazillion files in it. Try:
$ find mydirectory | wc -l
Say you want to refine this and ask how many files are in each of mydirectory's child
directories. Then use:
All this is to say, find is one of the best tools to print out file paths, irrespective of
whether you're looking for a file.
find works particularly well in combination with xargs. For example, delete any files
called tmp.txt in mydirectory:
$ find mydirectory -name "tmp.txt" | xargs rm
touch makes an empty file. E.g., to make an empty file called test:
$ touch test
Sometimes you find yourself running this command to see if you have write permission
in a particular directory.
diff prints out the differences between two files. It's the best way to find discrepancies
between files you think are the same. If:
$ cat tmp1
a
b
$ cat tmp2
2c2
< a
---
> x
comm, for common, is similar to diff but I rarely have occasion to use it.
Note: If you're familiar with git, you know that the diff operation plays a central role in its
version control system. Git has its own flavor of diff: git diff. For example, to see the
difference between commit c295z17 and commit e5d6565:
join joins two sorted files on a common key (the first column by default). If:
$ cat tmp1.txt
1a
2b
3c
$ cat tmp2.txt
2 aa
3 bb
Then:
2 b aa
3 c bb
#!/usr/bin/env perl
# About:
# join two text files using the first column of the first file
as the "key"
# Useage:
# tableconcatlines example/fileA.txt example/fileB.txt
while (<$ifile>)
/^(\S*)/;
chop;
Imagine the following scenario. You've just downloaded a large file from the internet.
How do you know no data was lost during the transfer and you've made an exact copy
of the one that was online?
To solve this problem, let's review of the concept of hashing. If you're familiar with a dict
in Python or a hash in Perl, you know that a hash, as a data structure, is simply a way to
map a set of unique keys to a set of values. In ordinary life, an English dictionary is a
good representation of this data structure. If you know your key is "cat" you can find
your value is "a small domesticated carnivorous mammal with soft fur, a short snout,
and retractile claws", as Google defines it. In the English dictionary, the authors
assigned values to keys, but suppose we only have keys and we want to assign values
to them. A hash function describes a method for how to boil down keys into values.
Without getting deep into the theory of hashing, it's remarkable that you can hash, say,
text files of arbitrary length into a determined range of numbers. For example, a very
stupid hash would be to assign every letter to a number:
A -> 1
B -> 2
C -> 3
and then to go through the file and sum up all the numbers; and finally to take,
say, modulo 1000. With this, we could assign the novels Moby Dick, Great
Expectations, and Middlemarch all to numbers between 1 and 1000! This isn't a good
hash function because two novels might well get the same number but nevermind—
enough of a digression already.
md5 is a hash function that hashes a whole file into a long string. The
commands md5 and md5sum do about the same thing. For example, to compute the
md5 hash of a file tmp.txt:
$ md5 tmp.txt
84fac4682b93268061e4adb49cee9788 tmp.txt
$ md5sum tmp.txt
84fac4682b93268061e4adb49cee9788 tmp.txt
This is a great way to check that you've made a faithful copy of a file. If you're
downloading an important file, ask the file's owner to provide the md5 sum. After you've
downloaded the file, compute the md5 on your end and check that it's the same as the
provided one.
md5 is one of many hashing functions. Another one, for example, is sha1—the unix
utility is sha1sum—which will be familiar to users of git:
$ sha1sum tmp.txt
fbaaa780c23da55182f448e38b1a0677292dde01 tmp.txt
tr TOP VIEW_AS_PAGE
tr stands for translate and it's a utility for replacing characters in text. For example, to
replace a period with a newline:
joe
joe
JOE
$ cat blast_header
qid sid pid alignmentlength mismatches numbergap
query_start query_end subject_start subject_end evalue
bitscore
1 qid
2 sid
3 pid
4 alignmentlength
5 mismatches
6 numbergap
7 query_start
8 query_end
9 subject_start
10 subject_end
11 evalue
12 bitscore
tr, with the -d flag, is also useful for deleting characters. If we have a file tmp.txt:
$ cat tmp.txt
a a a a
a b b b
a v b b
1 b 2 3
then:
a a a a
a v
1 2 3
a a a aa b b ba v b b1 b 2 3
Tip: To destroy carriage return characters ("\r"), often seen when you open a Windows
file in linux, use:
od TOP VIEW_AS_PAGE
One of the most ninja moves in unix is the od command which, with the -tc flag,
explicitly prints every character in a string or a file. For example:
0000000 j o e \n
0000004
We see everything: the j, the o, the e, and the newline. This is incredibly useful for
debugging, especially because some programs—notably those that bear the Microsoft
stamp—will silently insert evil "landmine" characters into your files. The most common
issue is that Windows/Microsoft sometimes uses a carriage-return (\r), whereas
Mac/unix uses a much more sensible newline (\n). If you're transferring files from a
Windows machine to unix or Mac and let your guard down, this can cause unexpected
bugs. Consider a Microsoft Excel file:
If we save this spreadsheet as a text file and try to cat it, it screws up? Why?
Our od command reveals the answer:
0000000 1 \t 2 \r 1 \t 2 \r 1 \t 2
0000013
0000013
Score!
$ cat test.txt
10
If we want to split this file into sub-files with 3 lines each, we can use:
$ head test_split_*
10
Note that the last file doesn't have 3 lines because 10 is not divisible by 3—its line count
equals the remainder.
nano is a basic a text editor that should come pre-packaged with your linux distribution.
There are two fundamental commands you need to know to use nano:
Cntrl-O - Save
Cntrl-X - Quit
nano is not a great text editor and you shouldn't use it for anything fancy. But for simple
tasks—say, pasting swaths of text into a file—it's a good choice.
For more serious text editing, use Vim or Emacs. These programs have too many
features to discuss in this post but, as a Vim partisan, I have an introduction to
Vim here.
Note: tree is not a default shell program. You may have to download and install it.
tree prints out a tree, in ASCII characters, of your directory structure. For example:
$ mkdir -p tmp/{a,b}/c/{d,e}
$ tree -L 2 tmp
Imagine the following scenario: you're working in the terminal and you accidentally close
the window or quit or it crashes. You've lost your session. If you were logged in
somewhere, you'll have to log in again. If you had commands you wanted to refer back
to, they're lost forever. This is where screen or tmux comes in. Regardless of whether
or not you quit the terminal, your sessions are persistent processes that you can always
access until the computer is shut off. You can get your exact terminal window back
again: you're still logged in wherever you were and, moreover, you can see everything
you typed in this window by simply scrolling up. For this reason, it's a good idea to make
using screen or tmux a habit every time you're on the terminal, if only as
insurance. screen also allows you to access multiple terminal instances within the
same terminal window and more.
A word of advice: skip the rest of this section. Learn tmux, the superior program
(discussed next), instead of wasting brain space on screen.
Start screen:
$ screen
This looks like an ordinary terminal window except that you see a bar at the bottom with
some names:
0 bash
1 bash
2 bash
These are all the terminal instances we have access to in this one screen session (and
in this one window).
So, let's get this straight: we can have many different screen sessions and, within each
session, we can have multiple instances which are—muddying the terminology—also
called windows (but not the kind of window you can create on the Mac by
typing ⌘N which I was referring to above).
$ screen -ls
Another figure:
$ screen -r 1167
Once you're in a screen session, what are the commands? By default, each screen
command starts with Cntrl-a, but I find this impractical because this combination's
default key binding in bash is to jump your cursor to the beginning of the line, which I
use all the time. So I like to use Cntrl-f as my Leader Key. You can use whatever you
like—we'll see how to set it below with .screenrc. Let's learn some of the basic screen
commands. I will copy some of these directly from here:
A note: to scroll inside screen (you'll find you can't use the window scrollbar anymore),
enter into copy mode and then navigate up and down with Cntrl-u and Cntrl-d or j and k,
as in Vim.
You can configure screen with the file .screenrc, placed in your home directory. Here's
mine, which my co-worker, Albert, "lent" me:
# change Leader key to Cntrl-f rather than Cntrl-a
escape ^Ff
defscrollback 5000
shelltitle '$ |bash'
autodetach on
#use F8 to turn the status bar off at the bottom of the screen
bindkey -k k5 hardstatus alwayslastline
This demonstrates how to use Cntrl-f as the Leader key, rather than Cntrl-a.
Note: tmux is not a default shell program. You may have to download and install it.
sessions
windows
panes
Sessions are groupings of tmux windows. For most purposes, you only need one
session. Within each session, we can have multiple "windows" which you see as tabs
on the bottom of your screen (i.e., they're virtual windows, not the kind you'd create on a
Mac by typing ⌘N). You can further subdivide windows into panes, although this can
get hairy fast. Here's an example of a tmux session containing four windows (the tabs
on the bottom), where the active window contains 3 panes:
I'm showing off by logging into three different computers—home, work, and Amazon.
$ tmux
Detach from your session: Leader d, where the leader sequence is Cntrl-b by default.
$ tmux ls
Kill a session:
Start copy mode (which will obey vim conventions per order of the
<Leader> [
config file)
Number the panes (within the window) - whereupon you can jump
<Leader> q
to a specific pane by pressing its numerical index
<Leader> z Toggle the expansion of one of the panes (within the window)
<Leader> m Turn mouse mode on (allows you to resize panes with the mouse)
* With tmux, in contrast to screen, you give a slightly longer pause after pressing the
Leader sequence. In this particular case, however, they are pressed simultaneously.
As before, I like to use Cntrl-f as my Leader sequence. Here's a sample .tmux.conf file,
which accomplishes this, among other useful things:
### Default Behavior
# Use screen key binding
set-option -g prefix C-f
# 1-based numbering
set -g base-index 1
### Views
# Highlight Active Window
set-window-option -g window-status-current-bg red
# Status Bar
set -g status-bg black
set -g status-fg white
set -g status-left ""
set -g status-right "#[fg=green]#H"
### Shortcut
# Last Active Window
bind-key C-a last-window
# set -g mode-mouse on
$ tmux attach
and now you're running tmux within tmux. To execute tmux commands on the inner
session, use Cntrl-b after the <Leader> sequence. For example, to list your windows on
the inner session, you'd type <Leader> Cntrl-b w.
make is a program which executes a special type of script called a Makefile. As this
tutorial notes:
As a build automation tool, Make generates files (called targets), each of which can depend upon the
existence of other files (called dependencies). Targets, dependencies, and instructions for how to
build them (called recipes) are defined in a special file called a Makefile.
Suppose you have a file called input1 and you want to produce a file called output3.
You write a makefile, which species how to do this with a bunch of rules. In
pseudocode:
You run make and produce your output3. At this point you're asking, why didn't I just
use an ordinary script? Suppose now you delete output3 and want to produce it anew.
The beauty of make is that it works backwards from your final rule, figures out which
dependencies it needs to create your final file, and only runs the necessary rules. In our
example, if output2 were still present, it would only run Rule3.
If make detected output2 were missing, it would look to run Rule2, etc.
I'll let the experts speak here, since I don't have much experience with this utility:
Note: The make concept—a script that starts backwards and only runs what it needs
to—is so useful it's been adapted into a Python version called Snakemake primarily
used for bioinformatics.
yes prints out the character y in an infinite loop (so be careful - you'll have to stop it
with Cntrl-c). If you give yes an argument, it will print out the argument rather than y.
For instance, to print the word oliver 30 times, the command would be:
nl TOP VIEW_AS_PAGE
$ cat tmp.txt
aaa
bbb
ccc
ddd
eee
$ nl -b a tmp.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
$ whoami
$ groups
$ who
$ w
hostname prints the system's host name, which is like the name of the computer. If
you've ssh-ed into multiple computers in a bunch of different terminal sessions, this
command will remind you where you are:
$ hostname
finger is a neat command that prints out information about a user on the system. For
example, to finger yourself:
$ finger $( whoami )
No mail.
Plan:
Hello, fools and lovers!
What's the plan thing? When you finger a user it will output the contents of the file
~/.plan
(Was this the genesis of Amherst College's PlanWorld? I think so! :-)
read is a shell builtin that I like to use in loops. You can read up a file with a while loop
using read—e.g., spit out file.txt exactly as is:
Another example:
$ echo -e '1\t2\n3\t4\n5\t6'
1 2
3 4
5 6
1c
3c
2t
1c
Then:
$ cat tmp.txt
1c
2t
3c
tee, in rough analogy with a plumber's tee fitting, allows us to save a file in the middle
of the pipeline and keep going. In this case, the output of sort is both saved
as tmp.txt and passed through the pipe to wc -l, which counts the lines of the file.
joe
$ cat test.txt
joe
The same idea: joe is echoed to std:out as well as saved in the file test.txt.
shopt, for shell options, controls various togglable shell options, as its name suggests.
You can see the options it controls by simply entering the command. For example, on
my system:
$ shopt
cdable_vars off
cdspell off
checkhash off
checkwinsize off
cmdhist on
compat31 off
dotglob off
true and false are useful to make multi-line comments in a bash script:
# multi-line comment
if false; then
echo hello
echo hello
echo hello
fi
if true; then
echo hello
echo hello
echo hello
fi
The formal man page definitions of these commands are amusing: true: "do nothing,
successfully (exit with a status code indicating success)"; false: "do nothing,
unsuccessfully (exit with a status code indicating failure)".
As in Perl and many other languages, shift pops elements off the array of input
arguments in a script. Suppose tmpscript is:
#!/bin/bash
echo $1
shift
echo $1
Then:
$ ./tmpscript x y
This produces the executable myprogram. Stackoverflow has a nice post about
the g++ optimization flags, which includes the bit:
The rule of thumb:
When you need to debug, use -O0 (and -g to generate debugging symbols.)
When you are preparing to ship it, use -O2.
When you use gentoo, use -O3...!
When you need to put it on an embedded system, use -Os (optimize for size, not for efficiency.)
xargs is a nice shortcut to avoid using for loops. For example, let's say we have a
bunch of .txt files in a folder and they're symbolic links we want to read. The following
two commands are equivalent:
$ for i in *.txt; do readlink -m $i; done
In xargs world, the {} represents "the bucket"—i.e., what was passed through the pipe.
$ ls /some/path/*.txt | xargs -i ln -s {}
xargs works particularly well with find. For example, zip all the files with
extension .fastq (bioinformatics) found in a directory or any of its sub-directories:
Delete all pesky .DS_store files in the cwd or any of its sub-directories:
hour 0-23
day 1-31
month 1-12
day-of-week 0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
command-line-to- the command to run along with the parameters to that command if
execute any
Once you make a file of this form, you can run it. For example, if:
$ cat mycron.txt
$ crontab mycron.txt
And it will empty the trash folder every day at 10:45 a.m.
$ crontab -l
One peculiar thing about cron is that, if you're on a shared computer system, your
system administrator might be the one running cron in his name and, when you invoke
cron, you won't start a process running under your name. For this reason, you have to
use absolute paths in the commands you put in your cron job: cron doesn't know about
the local folder from which you run the command.
type TOP VIEW_AS_PAGE
When you enter some text at the terminal prompt, that text could be a command (a
binary or a script), an alias, a bash function, a bash keyword, a bash builtin, and so
on. type will tell you, among these, what type of entity you're dealing with. For
example, on my system:
$ type ssh
ssh is /usr/bin/ssh
This is a straight-up command, not an alias or function. But, if I've aliased quickssh to
some specific ssh address, then type will reveal this:
$ type quickssh
$ type if
if is a shell keyword
$ type set
type tells us that if is a reserved word in bash, while set is a built-in command. You
get the idea!
The info command has a wealth of information about, well, nearly everything:
$ info
To get information about, say, the grep command:
$ info grep
The apropos command will display commands related to the string you give it as an
argument. For instance:
$ apropos grep
bzegrep [bzgrep] (1) - search possibly bzip2 compressed files for a regular
expression
bzfgrep [bzgrep] (1) - search possibly bzip2 compressed files for a regular
expression
bzgrep (1) - search possibly bzip2 compressed files for a regular
expression
egrep [grep] (1) - print lines matching a pattern
fgrep [grep] (1) - print lines matching a pattern
git-grep (1) - Print lines matching a pattern
grep (1) - print lines matching a pattern
grep (1p) - search a file for a pattern
pgrep (1) - look up or signal processes based on name and other
attributes
pkill [pgrep] (1) - look up or signal processes based on name and other
attributes
ptargrep (1) - Apply pattern matching to the contents of files in a tar
archive
xzgrep (1) - search compressed files for a regular expression
xzgrep [lzegrep] (1) - search compressed files for a regular expression
xzgrep [lzfgrep] (1) - search compressed files for a regular expression
xzgrep [lzgrep] (1) - search compressed files for a regular expression
xzgrep [xzegrep] (1) - search compressed files for a regular expression
xzgrep [xzfgrep] (1) - search compressed files for a regular expression
zgrep (1) - search possibly compressed files for a regular expression
zipgrep (1) - search files in a ZIP archive for lines matching a pattern
$ echo -e "asdfasdfasdf\nasdfasdfasdf"
asdfasdfasdf
asdfasdfasdf
asdfa
sdfas
df
asdfa
sdfas
df
Here's an example from Wiki - Bioinformatics. How would you find the sequence
composition of the following fasta file?
$ cat myfasta.fa
>entry1
AACCCGGCTGCGTACGTACCACAGAGAGGGGTGTA
>entry2
TTATGCGATAAACCCGGGTGTAATTTTATTTTTTT
17 A
13 C
18 G
22 T
olleh
As noted in the bioinformatics wiki, we can use this trick to find the reverse
complement of a string representing DNA nucleotides:
CCCGCAAGTG
mount TOP VIEW_AS_PAGE
mount, as the docs say, mounts a filesystem. Imagine two directory trees, one on your
computer and one on an external hard drive. How do you graft these trees together?
This is where mount comes in. On your Macintosh, external hard drives get mounted by
default in the
/Volumes
directory of your computer. Put differently, the directory tree of your computer and the
directory tree of your HD are fused there. I've only ever used the
actual mount command in the context of mounting an EBS volume on an EC2 node on
Amazon Web Services.
mktemp will make a temporary directory with a unique name in your designated
temporary directory. Recall that, usually, this is the directory:
/tmp
However, you can set it to be anything you like using the variable TMPDIR. Let's
see mktemp in action:
$ echo $TMPDIR
/path/tempdir
$ mktemp
/path/tempdir/tmp.LaOYagQwq3
$ mktemp
/path/tempdir/tmp.h5FapGH4LS
Sometimes you want to monitor how much space you have left on your computer:
$ watch df -h .
If a program is churning away and slowly writing to a log file, you can watch the tail:
Another example is using watch in combination with the Oracle Grid Engine's qstat to
monitor your job status:
$ watch qstat
Perl and Python aren't really unix commands, but whole massive programming
languages in themselves. Still, there are some neat things you can do with them on the
command line. See:
As the docs say, ping "uses the ICMP protocol’s mandatory ECHO_REQUEST
datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway." Practically
speaking, it allows you to send a signal to some IP address to see if it's responding.
Think of it as an exchange between two people:
$ ping some_IP_address
For instance, the internet tells me that 74.125.224.18 belongs to Google, so:
$ ping 74.125.224.18
^C
(Interrupt ping with Cntrl-c.) You can see this on the network. However, if you try
to ping the address 00.000.000.00:
$ ping 00.000.000.00
^C
dig is the "DNS lookup utility." If we use the same test IP address as we did in the
example above:
$ dig 74.125.224.18
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.32.amzn1 <<>>
74.125.224.18
;; Got answer:
;; QUESTION SECTION:
;74.125.224.18. IN A
;; AUTHORITY SECTION:
. 10467 IN SOA \
;; SERVER: 172.16.0.23#53(172.16.0.23)
$ ifconfig
If you're looking for your MAC address, it should be labeled as HWaddr. I'm the
opposite of a network guru and am ignorant about the many other capabilities of this
command, but you can read ifconfig's Wikipedia entry.
wget is a tool for downloading files from the web. If the movie test.MOV is hosted
on example.com, you can grab it with wget:
$ wget http://example.com/test.MOV
Of course, you can batch this using a loop. Suppose that there are a bunch of files
on example.com and you put their names in a text file, list.txt. Instead of surfing over to
the page and having to click each link in your browser, you can do:
or, equivalently:
As a concrete example, to get and untar the latest version (as of this writing) of the GNU
Coreutils, try:
$ wget http://ftp.gnu.org/gnu/coreutils/coreutils-8.23.tar.xz
You can also use wget to download a complete offline copy of a webpage. This page in
Linux Journal describes how. I will quote their example verbatim:
$ wget \
--recursive \
--no-clobber \
--page-requisites \
--html-extension \
--convert-links \
--restrict-file-names=windows \
--domains website.org \
--no-parent \
www.website.org/tutorials/html/
Note: elinks is not a default shell program. You may have to download and install it.
elinks is a terminal-based web browser. Surfing the web from the terminal is novel, if
not terribly convenient.
$ elinks www.nytimes.com
More usefully, elinks provides a way to scrape webpages into text files:
As noted in the Vim wiki, you can also examine the HTML source code of a page right
from your terminal with Vim:
$ vim http://www.nytimes.com/
When possible, avoid this approach and instead use a package manager, which takes
care of installing a program—and all of its dependencies—for you. Which package
manager you use depends on which operating system you're running. For Macintosh,
it's impossible to live without brew, whose homepage calls it, "The missing package
manager for OS X." For Linux, it depends on your distribution's lineage: Ubuntu
has apt-get; Fedora has yum; and so on. All of these package managers can search
for packages—i.e., see what's out there—as well as install them.
Let's use the program gpg2 (The GNU Privacy Guard), a famous data encryption tool
which implements the OpenPGP standard, to see what this process looks like. First,
let's search:
The exact details may vary on your computer but, in any case, now you're ready to wax
dangerous and do some Snowden-style hacking! (He reportedly used this tool.)
Note: display, convert, and identify are not default shell programs. You have to
download and install them from ImageMagick, an awesome suite of command-line tools
for manipulating images.
display is a neat command to display images right from your terminal via the X
Window System:
$ display my_pic.png
$ identify my_pic.png
convert is a versatile command that can do about 8 gzillion things for you. For
example, resize an image:
Add whitespace around the image file my_pic.png until its 600px by 600px and convert
it into gif format:
$ convert my_pic.png -background white -gravity center -extent 600x600
my_pic.gif
Do the same thing, but turn it into jpg format and put it at the top of the new image file (-
gravity north):
$ convert my_pic.png -background white -gravity north -extent 600x600
my_pic.jpg
Change an image's width (in this case from 638 to 500 pixels) while preserving its
aspect ratio:
As a common use case, the ImageMagick commands are invaluable for optimizing
images you're using on your websites. See How to Make a Website - Working with and
Optimizing Images for Your Site for details.
Note: gpg is not a default shell program. You have to download and install it.
gpg, "a complete and free implementation of the OpenPGP standard," according to the
official docs, is a program for encrypting files. You can use it to implement public-key
cryptography, which Wikipedia describes as follows:
Public-key cryptography, or asymmetric cryptography, is a cryptographic system that uses pairs of
keys: public keys, which may be disseminated widely, and private keys, which are known only to the
owner. The generation of such keys depends on cryptographic algorithms based on mathematical
problems to produce one-way functions. Effective security only requires keeping the private key
private; the public key can be openly distributed without compromising security.
In such a system, any person can encrypt a message using the receiver's public key, but that
encrypted message can only be decrypted with the receiver's private key.
Here are the basics about gpg. The first thing to do is to generate a public/private key
pair for yourself:
$ gpg --full-generate-key
Since you're already going through the trouble, choose a 4096 bit key and a secure
passphrase (you're going to need this passphrase in the future to decrypt stuff, so
remember it). Provide your real name and email, which will be associated with the key
pair.
If you want to generate keys with default options, just use:
$ gpg --gen-key
To encrypt a message (or file, or tarball) for somebody, you need to have his or her
public key. You encrypt with the recipient's public key, and only the recipient can
decrypt with his or her private key (you can, of course, use your own public key if you're
encrypting something for yourself). The first step in this process is importing the
recipient's public key into your keyring. If you want to practice this, you can save my
pubic key in a file called oli.pub and import it:
$ gpg --list-keys
$ gpg --list-secret-keys
This will create the file tmp.txt.gpg, which is in non-human-readable binary OpenPGP
format. If you're encrypting a bit of text, you can use the -a flag, which creates "ASCII
armored output"—i.e., a file you can cat or paste into the body of an email:
This will create the file tmp.txt.asc, which will look something like this:
Version: GnuPG v2
hQIMA4P86BWIDBBVAQ/+KIfnVGBF0Ei/9G/a8btxHu1wXrOpxp77ofPLzJ47e3
pm
Y4uO17sbR9JJ12gPHoc5wQT60De9JNtSsPbyD7HVUF0kD+mfnyM8UlyWd7P4Bj
E5
vRZLhlMt4R88ZvjsU5yTRmLFSkMTIl5NUXTiGuBfjkRZUL+1FooUKgrpu5osAC
y/
6/7FZ75jReR8g/HEYHody4t8mA3bB5uLoZ8ZEHluj6hf6HjI8nFivNO487IHMh
z3
UnDeoStL4lrx/zU0Depv/QNb4FRvOWUQMR7Mf61RcFtcHqfDyjg3Sh5/zg5icA
c6
/GEx/6fIuQtmVlXtDCuS17XjaTNBdBXgkBqxsDwk3G1Ilgl9Jo83kBkQLgo3Fd
B6
3qs9AafNxTzloba8nF38fp0LuvvtSyhfHpYIc4URD5Mpt5ojIHNBVxevY0OeRD
X8
x6Bmqu1tKSsKJz7fb0z3K/4/dYTMspIMUIw64y6W3De5jJv9pkbaQ/T1+y5oqO
eD
rNvkMYsOpMvHBrnf0liMgn+sLuKE+G26lQfGm4MdnFQmb7AWBC5eqK8p1MnSoj
Gm
klTlTpRSKfx4vOhB4K64L2hlH0rBHE3CvOOsJivzZ7r7MKsBoX6ZHwxVR0YOh/
5J
m0Fzk0iprP9vzv5bWlADpCWYVUp6I6WHDfaFnwCDxH2O6y+krxHGjHei7u7GV9
fS
SgEVvZZDErHr/ZTwwa7Xld37cJ9dOpesBECrk5ncLr25mNzDNGxgDXqM2yEuzh
Na
HDmO0dVloPnVuQ/2SYL/4JP4Z6Uitm13nKQK
=55in
Conversely, if somebody wants to send you a secret message or file, they're going to
need your public key. You can export it and send it to them:
where myusername was the user name you chose during key generation. (There are
also keyservers, such as keyserver.ubuntu.com, which host public keys.)
If somebody sends you the encrypted file secret.txt.asc, you can decrypt it with:
Suppose you want to transfer your keys to another computer or a hard drive. First,
remind yourself what your keys are:
$ gpg --list-keys
$ gpg --list-secret-keys
This is all you have to do if you're storing them on a hard drive. If you want to import
them into a keyring on another computer, it's:
Edit keys:
More on gpg
Note: datamash is not a default shell program. You have to download and install it.
GNU datamash is a great program for crunching through text files and collapsing rows
on a common ID or computing basic statistics. Here are some simple examples of what
it can do.
$ cat file.txt
3 d
2 w
3 c
4 x
1 a
1 a
2 w
3 d,c
4 x
The -g flag is the ID column; the collapse field picks the second column; the -s flag pre-
sorts the file; and the -W flag allows us to delimit on whitespace.
$ cat file.txt
A 1 3 SOME_OTHER_INFO
A 1 4 SOME_OTHER_INFO2
B 2 30 SOME_OTHER_INFO4
A 2 5 SOME_OTHER_INFO3
B 1 1 SOME_OTHER_INFO4
B 2 3 SOME_OTHER_INFO4
B 2 1 SOME_OTHER_INFO4
A 1 3 SOME_OTHER_INFO 3.5
A 2 5 SOME_OTHER_INFO3 5
B 1 1 SOME_OTHER_INFO4 1
B 2 30 SOME_OTHER_INFO4 11.333333333333
In this case, the ID is the combination of columns one and two and the mean of column
3 is added as an additional column.
Note: virtualenv is not a default shell program. You have to download and install it.
Do you use Python? virtualenv is a special command line tool for Python users. We
learned about package managers in An Introduction to the Command-Line (on Unix-like
systems) - Installing Programs on the Command Line, and Python's is called pip.
Suppose you're working on a number of Python projects. One project has a number of
dependencies and you've used pip to install them. Another project has a different set of
dependencies, and so on. You could install all of your Python modules in your global
copy of Python, but that could get messy. It would be nice if you could associate your
dependencies with your particular project. This would also ensure that if two projects
have conflicting dependencies—say they depend on different versions of the same
module—you can get away with it. Moreover, it would allow you to freely install or
update modules to your global Python worry-free, since this won't interfere with your
projects. This is what virtualenv does and why it's a boon to Python users.
$ virtualenv venv
To emphasize the point, this is a whole new copy of Python. To use this Python, type:
$ source venv/bin/activate
/some/path/venv/bin/python
You can see that you only have the Django module (and wheel):
Django==1.8.7
wheel==0.24.0
(venv) $ deactivate
$ lsof -i
Read more:
Wikipedia: lsof
Tecmint: 10 lsof Command Examples in Linux
IT'S ME, TOMMY: lsof
$ set
$PATH
$ PATH=$PATH:/my/new/path
$ PATH=/my/new/path:$PATH
Now we know where to put bash commands, but what about other programs? What if
you've installed a package or a module in a local directory and you want the program to
have access to it? In that case, the following global variables come into play.
$MATLABPATH
$R_LIBS
$AWKPATH
$PYTHONPATH
$LD_LIBRARY_PATH
$PERL5LIB
This will come into play when you've locally installed a module and have to make sure
Perl sees it. Then you'll probably have to export it. E.g.:
$ export PERL5LIB=/some/path/lib64/perl5
Text editor:
$EDITOR
export EDITOR=/usr/bin/nano
$TMPDIR
$RANDOM
For example:
$ echo $RANDOM
1284
$ echo $RANDOM
27837
Network sleuthing is far outside my expertise, but here are some utilities to check out:
tshark
netstat
dig
nslookup
nmap
lsof
Advertising
© 2020 Oliver
VVVVVV