Skip to content

Day 2 - Basic navigation

INTRO

Most computer users outside of the Linux and Unix world don’t spend much time at the command-line now, but as a Linux sysadmin this is your default working environment - so you need to be skilled in it.

When you use a graphic desktop such as Windows or Apple’s macOS (or even the latest Linux flavors), then increasingly you are presented with simple “places” where your stuff is stored - “Pictures” “Music” etc but if you’re even moderately technical then you’ll realize that underneath all this is a hierarchical “directory structure” of “folders” (e.g. C:\Users\Steve\Desktop on Windows or /Users/Steve/Desktop on macOS - and on a Desktop Linux system /home/steve/Desktop)

From now on, the course will point you to a range of good online resources for a topic, and then set you a simple set of tasks to achieve. It’s perfectly fine to google for other online resources, refer to any books you have etc - and in fact a fundamental element of the design of this course is to force you to do a bit of your own research. Even the most experienced sysadmins will do an online search to find advice for how to use commands - so the sooner you too get into that habit the better!

YOUR TASKS TODAY

  • Find the documentation for the commands we used so far - demo
  • Navigate between directories, then create, list, move and delete files - demo

RTFM

This is a good time to mention that one of the many advantages of Linux is that it’s designed to let you know the system, to let you learn how to use it. The documentation available in form of text manuals, guides and forums is where you will spend most of your time during this journey.

Whereas proprietary systems have some free documentation, you see much more frequently the use of paid customer support to fix issues or find how a particular task can be executed. Although you can also do this with Linux (Canonical, RedHat and SuSE are examples of companies that offer support in the same fashion), this is most likely not the case. And you are here to learn, so…

Which leads us to the famous acronym RTFM. Reading the manual is the first thing you should do when you’re learning a command. We will go through the many ways to obtain that information but if at the end of that search you need more insight, you can always ask a well written question in forums and other communities.

Starting with the man command. Each application installed comes with its own page in this manual, so that you can look at the page for pwd to see the full detail on the syntax like this:

man pwd

You might also try:

 man cp
 man mv
 man grep
 man ls
 man man

As you’ll see, these are excellent for the detailed syntax of a command, but many are extremely terse, and for others the amount of detail can be somewhat daunting!

And that’s why tldr is such a powerful tool! You can easily install it with sudo apt install tldr or follow this demo.

$ tldr pwd
pwd
Print name of current/working directory.More information: https://www.gnu.org/software/coreutils/pwd.

 - Print the current directory:
   pwd

 - Print the current directory, and resolve all symlinks (i.e. show the "physical" path):
   pwd -P

If you know a keyword or some description of what the command is supposed to do, you can try apropos or man -k like this:

$ apropos "working directory"
git-stash (1)        - Stash the changes in a dirty working directory away
pwd (1)              - print name of current/working directory
pwdx (1)             - report current working directory of a process

$ man -k "working directory"
git-stash (1)        - Stash the changes in a dirty working directory away
pwd (1)              - print name of current/working directory
pwdx (1)             - report current working directory of a process

But you’ll soon find out that not every command has a manual that you can read with man. Those commands are contained within the shell itself and we call them builtin commands.

There are some overlaping (i.e. builtin commands that also have a man page) but if man does not work, we use help to display information about them.

$ man export
No manual entry for export

$ help export
export: export [-fn] [name[=value] ...] or export -p
    Set export attribute for shell variables.

    Marks each NAME for automatic export to the environment of subsequently
    executed commands.  If VALUE is supplied, assign VALUE before exporting.

    Options:
      -f        refer to shell functions
      -n        remove the export property from each NAME
      -p        display a list of all exported variables and functions

    An argument of `--' disables further option processing.

    Exit Status:
    Returns success unless an invalid option is given or NAME is invalid.

The best way to know if a command is a builtin command, is to check its type:

$ type export
export is a shell builtin

And lastly, info reads the documentation stored in info format.

  • Start by reading the manual: man hier
  • / is the “root” of a branching tree of folders (also known as directories)
  • At all times you are “in” one part of the system - the command pwd (“print working directory”) will show you where you are
  • Generally your prompt is also configured to give you at least some of this information, so if I’m “in” the /etc directory then the prompt might be [email protected]:/etc$ or simply /etc: $
  • cd moves to different areas - so cd /var/log will take you into the /var/log folder - do this and then check with pwd - and look to see if your prompt changes to reflect your location.
  • You can move “up” the structure by typing cd .. ( “cee dee dot dot “) try this out by first cd‘ing to /var/log/ then cd .. and then cd .. again - watching your prompt carefully, or typing pwd each time, to clarify your present working directory.
  • A “relative” location is based on your present working directory - e.g. if you first cd /var then pwd will confirm that you are “in” /var, and you can move to /var/log in two ways - either by providing the full path with cd /var/log or simply the “relative” path with the command cd log
  • A simple cd will always return you to your own defined “home directory”, also referred to as ~ (the “tilde” character) [NB: this differs from DOS/Windows]
  • What files are in a folder? The ls (list) command will give you a list of the files, and sub folders. Like many Linux commands, there are options (known as “switches”) to alter the meaning of the command or the output format. Try a simple ls, then ls -l -t and then try ls -l -t -r -a
  • By convention, files with a starting character of “.” are considered hidden and the ls, and many other commands, will ignore them. The -a switch includes them. You should see a number of hidden files in your home directory.
  • A note on switches: Generally most Linux command will accept one or more “parameters”, and one or more “switches”. So, when we say ls -l /var/log the “-l” is a switch to say “long format” and the “/var/log” is the “parameter”. Many commands accept a large number of switches, and these can generally be combined (so from now on, use ls -ltra, rather than ls -l -t -r -a
  • In your home directory type ls -ltra and look at the far left hand column - those entries with a “d” as the first character on the line are directories (folders) rather than files. They may also be shown in a different color or font - if not, then adding the “–color=auto” switch should do this (i.e. ls -ltra --color=auto)

BASIC DIRECTORY MANIPULATION

  • You can make a new folder/directory with the mkdir command, so move to your home directory, type pwd to check that you are indeed in the correct place, and then create a directory, for example to create one called “test”, simply type mkdir test. Now use the ls command to see the result.
  • You can create even more directories, nesting inside directories, and then navigate between them with the cd command.
  • When you want to move that directory inside another directory, you use mv and specify the path to move.
  • To delete (or remove) a directory, use rmdir if the directory is empty or rm -r if there still any files or other directories inside of it.

BASIC FILE MANIPULATION

  • You can make new empty files with the touch command, so you can explore a little more of the ls command.
  • When you want to move that file to another directory, you use mv and specify the path to move.
  • To delete (or remove) a file, use rm.

WRAP

Being able to move confidently around the directory structure at the command line is important, so don’t think you can skip it! However, these skills are something that you’ll be constantly using over the twenty days of the course, so don’t despair if this doesn’t immediately “click”.

EXTENSION

If this is already something that you’re very familiar with, then:

  • Learn about pushd and popd to navigate around multiple directories easily. Running pushd /var/log moves you to to the /var/log, but keeps track of where you were. You can pushd more than one directory at a time. Try it out: pushd /var/log, pushd /dev, pushd /etc, pushd, popd, popd. Note how pushd with no arguments switches between the last two pushed directories but more complex navigation is also possible. Finally, cd - also moves you the last visited directory.

RESOURCES

Some rights reserved. Check the license terms here