Learn from an experimental physicist

Aug 30, 2016 learning Linux SHELL phys-492/592

$ # things after # is a comment, it won't be executed
$ # $ sign is the command prompt, it is not part of the command
$ /sbin/ifconfig # print IP address of the machine you are in
$ uname -a # print operating system information
$ cat /proc/cpuinfo # find out how many CPUs are there in this machine
$ cat /proc/meminfo # find out how big the memory is
$ top # find out who's using the resource, type q to quit
$ df -h # check the disk usage
$ pwd # (print working diretory) find out where you are
$ ls # list contents in the current directory
$ ls -l # detailed list of the contents in the current directory
$ ls -a # list contents in the current directory including hidden files

After logging into a Linux machine using PuTTY from a Windows PC, you may want to make sure that you are indeed in the machine that you intend to log in. Just as you get to a new place in the real world, you’d like to look around. You need the above commands to figure out the surroundings.

You may ask immediately “how am I suppose to remember the usage of so many commands?” The answer is “No, you don’t need to remember, you man it”:

$ man ls

The command man shows the usage of a command in detail. Type q to quit when you are done.

You may also ask “What is this / thingy there?”. This is the sign to separate directories and files. The output of the command pwd may look like /home/YouUserName. That means you are in your home folder and your home folder is a subdirectory of the /home folder, which is in turn a subdirectory of the / folder, which is called the root directory.

You can go to a directory using the cd (change directory) command:

$ cd / # go to the root directory
$ ls
$ cd # cd without any argument bring you back to your home directory
$ cd - # go back to previous directory
$ cd ~YouUserName # bring you to /home/YouUserName/

Now you can get around. How about create something here?

$ mkdir bin doc # make two directories named bin and doc
$ ls # make sure you have created them
$ cd doc
$ touch test # create a file
$ ls -l test # check the time stamp of the file
$ file bin test # find our what are bin and test

You may notice the hidden files shown in your home directory using the ls -a command:

$ ls -a
.   ..    .bashrc     .profile

They all start with a dot. One dot means the current directory, two dots means the parent directory. So you can use the following command to go up one directory:

$ cd ..

and the following command to run an executable in the current directory:

$ ./someExecutableFileInCurrentDirectory

You may see something like .bashrc. This is a rc file. You will meet many rc files in the future. They are files to save configurations of various programs. .bashrc is the configuration file for a program called bash. The full name of the program is Bourne Again SHell. It is one of the many Linux Shells.

According to wikipedia,

a shell is a user interface for access to an operating system’s services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation. It is named a shell because it is a layer around the operating system kernel.

Practically, the word shell almost always refers to CLI instead of GUI. In some sense, to learn Linux command-line is to learn how to use a particular shell. If you don’t have any preference at this moment, please try to learn bash first because it is probably the most commonly available and used one.

What does a shell do? First of all, it shows a command prompt after which you can type in command. A command prompt sometimes looks like this:

UserName@HostName:~ >

or this:

root@HostName:/ $

or something really fancy:

UserName@HostName:~ | 8:30 am | battery: 34% | :-)

It all depends on the settings in your shell’s rc file.

Second, the shell analyzes what you have typed in, for example,

$ /sbin/ifconfig | grep inet | head -1 | awk '{print $2}'

This is a combination of many commands used to print out the IP address of the machine nicely. The shell needs to understand which words are commands, how to execute them one by one and how to pass output of one as input to another.

Third, the shell provides a set of key combinations, or shortcuts, to facilitate the editing of the command line. For example, Ctrl+a moves your cursor to the beginning of the command line, Ctrl+e moves it to the end. Search for bash shortcuts on Google for a complete list.

The shell also provides an environment for any command to run. It does so by bookkeeping a list of many environment variables. The following command gives you a full list of them in your system:

$ env

The output is a bit overwhelming. For the moment, you only need to understand several of them. You can use the echo command to print out the content of an environment variable (case sensitive):

$ echo $SHELL # The SHELL variable saves the type of shell you are using.
$ echo $HOME # the location of your home directory
$ echo $PATH # list of directories where executables are located

You can use the export command to declare or change an environment variables and make it take effect immediately:

$ export AMadeUpEnvVariable="some random text"
$ echo $AMadeUpEnvVariable
$ export SHELL=/bin/zsh # switch to another shell
$ export SHELL=/bin/bash # switch back to use bash
$ export PATH=~/bin:$PATH # add /home/YouUserName/bin/ to $PATH
$ export PS1='\u@\h:\w $' # set prompt to be UserName@HostName:PWD $

These settings can be saved in .bashrc so that you don’t have to type them again next time you log in.

Another things that are commonly saved in .bashrc are short versions of long commands. This is done by creating aliases of them:

$ alias ls='ls --color -F' # color different types of files in the output of ls
$ alias la='ls -a' # list hidden files using new command la
$ alias l='la -l' # list details of all files using new command l

To write those settings to .bashrc, you need a text editor that can run in a terminal. If you don’t know any text editor, try to use nano, which is installed in most of the Linux servers and very easy to learn for beginners:

$ nano ~/.bashrc # edit .bashrc in your home directory (~/) using nano

Type in your settings, type Ctrl+o to save and Ctrl+x to quit. Use the following command to make your settings take effect immediately:

$ source ~/.bashrc # starts to use your new settings in ~/.bashrc

It is a bit confusing when we think about the roles that a termnial and a shell play. Simply put, a terminal provides a frame, where a shell exists. The size of the terminal, the font used to display the shell prompt, commands and output, as well as the mouse clicks are handled by the terminal. Key strokes will be processed first by the terminal and then sent to the shell.

The middle click of a mouse in most of the Linux terminals would actually do the job of paste, the same as the Ctrl+v combination in Windows. Copy can be done by simply selecting some text in the terminal while holding the left button. Double left click on a word will select and copy the whole word. Triple click will select the whole line where the mouse is pointing to. Right click will most probably bring up a menu provided by the terminal.


Blogs on physics research and educational activities of Jing LIU, an assistant professor in physics at the University of South Dakota. He is an experimental physicist developing novel particle detectors for astroparticle physics and civil use.


Some of the activities mentions in this site are supported by the following grants: