Basic Linux Usage

NoteWhat we will learn in this section
 

In this section we will first learn how to get information about core commands (using man and info) and then cover the usage of basic commands such as

  • pwd print your current working directory

  • cd change your current directory

  • ls list the contents of the current directory

As we move through the steps in this section you will build your own web page as a running example.

This section covers basic Linux commands and how to efficiently combine them to perform more powerful operations. Examples are shown in a format like this:


$ pwd
/home/mcehl
The lines starting with $ are executable commands all other lines are the output of running this command. The $ symbol stands for the prompt you see in your terminal window. To do the exercise, cut-and-paste the text after the $ symbol into your terminal window. For example, when you cut-and-paste the command pwd from the example above, you will get the current directory as a reply. Lines starting with # are comments, explaining what the commands are doing, and can be ignored. Try variants of the commands as shown in the introduction to understand what's happening in each step. Notes at various points will refer you to a more detailed treatment of individual topics.

Warning

If in the above example you got an error like


bash: $: command not found
you also pasted the $ symbol. Only paste the text after the $ symbol into your terminal window.

To get started, log in to one of the Linux Lab machines, using your account and password. You'll see a familiar graphical desktop environment. Go to the main menu (bottom left) and in the list of sub-menus, go to System Tools and in there to Terminal. Alternatively, right-click on the background and choose Open Terminal. You'll now see a terminal window, and you can execute the commands in this section within this window.

TipLinux QuickStart on MACS Linux Lab machines
 

For step-by-step guidance for logging in and starting a terminal window, see this the Section called Step-by-step Login Information and this Video .

NoteMore detail on basic Linux commands
 

The link list in the Section called Other Linux tutorials has many links to more detailed discussions. In particular, check out this discussion of basic Linux commands on the Linux Introduction pages of guru99.

At this point you should have a terminal window running, showing the shell prompt that awaits input from the keyboard.

NoteLinux Quick Reference
 

This tutorial covers only the most commonly used commands, to get started. Follow these links for an alphabetical list of Linux commands (O'Reilly) or this this Linux quick-reference (O'Reilly).

Preliminaries and Getting Help

First some preliminaries. The commands described here represent Linux/GNU system, i.e. the Linux kernel for interacting with the hardware, and several sets of basic user-level commands provided by the GNU project. Each of the commands typically implements an -? or --help option, which describes how to invoke it, e.g. ls --help. Each user-level command should have a manual page that can be viewed by the man command, e.g. man ls. In most cases these commands are binary executables or scripts that reside somewhere on the file system. To find the exact location use the which, e.g. which ls. You can use the file command to determine the type of the file. The pwd command prints the current directory. The whoami command prints your username (used for log-in). The hostname command prints the name of the machine you are working on. The uptime command prints for how long the machine has been running, since the last reboot. Some commands aren't executables but aliases to other command. Use alias to print them all. Some examples:


$ ls --help 
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
...
$ man ls
... shows the man page ...
$ pinfo ls
... shows the more detailed info page (use q to exit) ...
$ which ls
/bin/ls
$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.33, BuildID[sha1]=0x88bca03aed9a306510a0bbcde1ea165e1d54c1ce, stripped

Note

Both the man and pinfo command will open a text base document viewer in your terminal to read the man/info pages. Press the q key to exit.

NoteOnline man pages
 

Several web pages provide browsable, online versions of Linux man pages, e.g. Linux man pages . However, you need to check that the version of the command matches the one documented on these pages. Most commands provide a --version option for this purpose.

You are now ready to execute simple commands. Don't be scared. Follow the examples in the next section to view the contents of your home directory and to navigate in the file system.

First Steps: Files, Directories and Navigation

NoteLinux quick-reference:
 

I recommend you open this Linux quick-reference in a separate window, to have an explanation for the most common commands at hand.

In most operating systems, documents are organised in a tree structure with directories as nodes in the tree and files as leaves in the tree. This means that each directory contains an arbitrary number of files and (sub-)directories. After login and launching a terminal, you'll be in your home directory. The command pwd will print the current directory. Use the ls command to list all files in the directory, add the options -la (-l for long and -a for all) to give more information for all files. Each line shows whether this is a directory (d) and the permissions in the first column, username and groupname of the owner in columns 3 and 4. Note that option -a shows all files, including hidden files that start with a dot. Without this option hidden files won't be shown. An important hidden file is .bashrc, which contains shell code that is executed whenever you open a new shell. For an explanation of the letters encoding permissions (first column) see the the Section called Security.


$ ls 
Desktop  Documents  Downloads  Music  Pictures  Public  shares  snap  Templates  Videos
$ ls -la 
total 104
drwx------ 20 mcehl domain users 4096 Sep  2 14:30 .
drwxr-xr-x  6 root  root         4096 Aug 28 13:02 ..
-rw-------  1 mcehl domain users 1835 Aug 28 14:40 .bash_history
-rw-------  1 mcehl domain users  220 Jun 24 19:39 .bash_logout
-rw-------  1 mcehl domain users 3771 Jun 24 19:39 .bashrc
drwx------ 13 mcehl domain users 4096 Aug 28 14:40 .cache
drwx------ 16 mcehl domain users 4096 Aug 27 21:58 .config
drwx------  2 mcehl domain users 4096 Jun 24 19:40 .cups
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Desktop
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Documents
drwxr-xr-x  4 mcehl domain users 4096 Aug 27 22:12 Downloads
drwx------  2 mcehl domain users 4096 Jul 23 16:20 .gnupg
drwxr-xr-x  2 mcehl domain users 4096 Aug 27 22:00 home_dir
-rw-------  1 mcehl domain users   20 Aug 27 22:02 .lesshst
drwx------  3 mcehl domain users 4096 Jun 24 19:39 .local
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Music
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Pictures
-rw-------  1 mcehl domain users  807 Jun 24 19:39 .profile
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Public
drwx--x--x  4 mcehl domain users 4096 Sep  2 14:30 shares
drwx------  3 mcehl domain users 4096 Jun 24 19:45 snap
drwxr-xr-x  6 mcehl domain users 4096 Jun 24 19:51 .sos
drwx------  2 mcehl domain users 4096 Aug 27 22:01 .ssh
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Templates
drwxr-xr-x  2 mcehl domain users 4096 Jun 24 19:39 Videos
-rw-------  1 mcehl domain users   59 Sep  2 14:30 .Xauthority

Note

The exact contents that you see in your home directory, as well as the name of the home directory will differ from the output above. You should see the same structure, though.

WarningShared directories
 

The files and directories that you see are local to the machine and you won't see the same ones if you switch to another machine. However, you will see some shared directories in the shares directory. In particular, there is directory shares/hdrive that you should use for storing files, visible on all machines.

The next step is to generate a directory (folder) in the shared filespace that you can access from all machines. For that we need some basic Linux commands to get started:

In the sequence of commands below we first navigate into the shared folder (shares/hdrive). As an optional step give the owner of that folder write permission with the command chmod u+w . (see the Section called Security for more details on how to set permissions). Then we create a directory for the LinuxIntro and use this as the working directory for the rest of this section. Check with the command pwd that you are in the right directory.

$ cd shares/hdrive
$ chmod u+w .
$ mkdir LinuxIntro
$ cd LinuxIntro 
$ pwd
/home/mcehl/shares/hdrive/LinuxIntro

NoteSide remark: Filename completion
 

To save typing, in particular if you need to refer to long filenames or deep pathes, use filename completion: start typing the filename, e.g. ind, and then hit TAB, which will complete the filename if it is unique, or show all possible matches. You can also use it to show all commands starting with the typed letters, e.g. ma TAB, will print all commands starting with ma.

For frequently changing between directories, it is often useful to keep a stack of recently visited directories while navigating through the filesystem. To do that use pushd, which behaves like cd but remembers the directory you go to. Later, popd can be used to go back to that directory, or more precisely it jumps to the directory that's top of the stack of remebered directories. To show all directories on that stack, type dirs. For example, the following jumps to your Desktop directory, containing all files that you see on your graphical desktop, then lists all files (most recent last), prints the directory stack and then jumps back to the directory you started from. Note that ~ stands for your home directory, and you can use it jump directly to your home directory doing cd ~.


$ pushd ~/Desktop
~/Desktop ~
$ ls -ltr
total 3
-rwxr-xr-x 1 live live 1444 Jun  1 19:52 register.desktop*
-rwxr-xr-x 1 live live 1234 Jun  1 19:52 draklive-install.desktop*
$ dirs
~/Desktop ~
$ popd

NoteLinux Practical
 

If this is confusing to you, work through this practical before you continue with the Linux Introduction: Linux Practical.

At this point you should be able to run basic commands from the command line.

NoteExercises
 

To exercise this knowledge, go to your home directory and show all files. Answer these questions:

  • How many files do you have in the current directory?

  • Which of these files are directories?

  • What's the name of the most recent file?

  • Which combination of options would you use, to list all files in a directory, sorted by size with largest file last, showing full information for each file?

NoteMore on file access:
 

A detailed discussion of the most important commands for accessing files and directories is given in the early sections of Chapter four in Sobell's textbook. Most online tutorials on Linux cover these commands in quite some detail as well (e.g. UNIX Tutorial for Beginners).

Our Running Example: Build your Own Web Page

As a simple example, let's create your own home page. This is done by downloading and editing files in the LinuxIntro directory that we generated above. We then need to publish the files by copying them over to a file server. Then you can see the files in a web browser.

First, check that you are in LinuxIntro directory.


      $ pwd
      /home/mcehl/shares/hdrive/LinuxIntro
    
If you see a path not ending in LinuxIntro you can go to this directory directly by typing:

      $ cd ~/shares/hdrive/LinuxIntro
    

NoteAbsolute and relative paths
 

When changing directory with cd, or copying files, you can specify an full, absolute path, starting from the root like this /home/mcehl/shares/hdrive/LinuxIntro. A ~ symbol in the path refers to your home directory. So ~/shares refers to the shares directory in your home dir. You can also use relative paths, where the symbol . refers to the current directory, and the symbol .. refers to the parent directory.

Finally, copy over the template files from a central location into the current directory. The command scp performs a secure copy over the network, using a format of: uaername@machinename:directory. For a local copy of files you would use the command cp.

In the next step, we download a set of template files that make up a minimal web page. Download a file with the template for the web page using a web browser: download this file. This will download the file into your ~/Downloads folder (unless you changed the settings in your browser). From there, move it to the current directory and unpack it as follows:


$ # Make sure to download the file above before doing this step
$ mv ~/Downloads/LinuxIntro_samples.tgz .
$ tar xfz LinuxIntro_samples.tgz
This should result in template files in the folder public/LinuxIntro, and you can continue with the steps below.

Warning

Note the . ('dot') at the end of the mv command above! If you see an error message like this


 $ mv: missing destination file operand after '/home/mcehl/Downloads/LinuxIntro_samples.tgz'
 
you have forgotten the dot at the end of the line.

Now you have a file structure, starting with public, in your working dir. Navigate down the directory hierarchy to get the files the make up your web page.


$ cd public/LinuxIntro/www/samples/redandgreen

To make the file visible to a web server, you need to set its access permissions appropriately using the chmod command: in this case it gives all users (a) read (r) access to this file. See the Section called Security for more details on how to set permissions.


$ # make the files accessible to the web server
$ chmod a+r index.html 
$ chmod go-w index.html 

Finally, launch a web browser, such as firefox, and point it to the local file.


$ # view the web page (see alternatives below)
$ firefox file://`pwd`/index.html

In the final step you need to publish the files in the working directory to a dedicated space on the www4 web server. To do so, use the scp command below, which copies all files in the current directory, into a directory on the web server set aside for your account (that part `whoami` will expand to your username, something like xyz12; you can replace it by your username but you don't have to):


      $ scp -r * `whoami`@www4.hw.ac.uk:/var/www/html/Intro/`whoami`/.
      
If asked: accept the fingerprint, and use your HWU credentials for login.

If you are working on one of the Linux Lab machines, and have copied all files to the web server with the scp command above. you can type the URL into the address field of a running browser, or point the web browser to your home page when you launch it, typing


$ firefox http://www4.hw.ac.uk/Intro/`whoami`
You should now see your home page, using a red-and-green style like this.

Congratulations, you now have a home page, visible to everyone on the web!

At this point you should have a web browser running and see a red-and-green web page, showing the sample page you just created.

Note

After all steps of editing below, you need to run the same scp command as above to publish your changed files.

Editing Files

Now, you will want to fill in some contents. To do so, you will need to edit the file index.html. The editor of choice on most Linux system is emacs, so you can start editing by typing (use the option -nw to start the editor in the terminal rather than in an own window):


$ emacs index.html 

NoteEditing Files: Emacs cheat sheet
 

You can find an emacs cheat sheet, summarising its most important commands, here.

Now, in the editor modify the index.html file and change title and top-level heading to indicate that this is your web page: search for the text This is my home page and modify it. Add a 1 paragraph description about yourself, replacing the text Some text. You need only very basic knowledge about HTML to do this. Essentially, you just have to fill in the blanks of the sample page.

NoteHTML cheat sheet
 

If you want to do more complex changes, look up this HTML cheat sheet.

Now, go back to your web browser, the firefox window, and reload the page. You'll see that the text has changed, corresponding to your edits.

A note on editors: There are many more editors available on standard Linux systems: gedit (a graphical editor that is part of the Gnome infrastructure), vim (a very flexible text editor), pico (a small and simple text editor), nano (a pico clone and GNU software). You can call each editor from the command-line, and some can be used in batch mode for repeated processing of text files. Also, check the Accessoires section in the top-level menu of your window-manager (typically, bottom left menu) to get list of available editors.

Tip

In most cases, a text editor is all you need to edit text files using ASCII encoding. In some cases, such as Coursework 1 of F21CN, you need to edit individual bytes in a non-ASCII files. There are specialised editors, hex-editors, to do this. You can start the shed hex-editor directly from the command line:


$ shed sample.bmp 

At this point, you should have your own web page, created from the red-and-green sample page. If your user name is xyz12, you can view this page by typing the following URL in your web browser:


http://www4.hw.ac.uk/Intro/xyz12

Some Exercises

NoteBasic Exercises
 

Answer the following questions by using and interpreting the appropriate command on the commandline:

  • How many files and directories do you have in your home directory (top level only)?

  • Which files in your top directory can be read by any other user on the system?

  • Which directories in your top directory can be entered (with the cd command) by any other user on the system?

  • How much disk space is available on the current partition and on the partition that hosts your home directory?