Linux Literacy Session 3: Enough Commandline to be Dangerous

Standard

version 1.0

The Basics

  • The two most important keys: the tab key (for autocompletion) and the up arrow key (for repeating commands)

  • The cd command is used to move around:

    • cd : go to your home folder (usually /home/<username>)
    • cd .. : go up a level
    • cd /usr/share/doc/ : go to the /usr/share/doc folder.
  • The ls command lists the contents of folders

    • ls : Show files and folders in the current directory
    • ls -l : Show detailed information for contents of current
      directory, including file sizes and permissions
    • ls -a : Show “hidden files” too (those that begin with a .)
    • ls -ltc : Show files that were most recently changed at the top

    There are lots more flags, and they can be combined: ls -aF

  • sudo : Run a command as the root user. For example: sudo nano
    /etc/apt/sources.list

  • less : View a textfile, or the output of a command page by page. Examples

    • less /usr/share/doc/less/copyright.gz : Show the contents of the
      copyright.gz file.
    • dmesg | less : Run the dmesg command and output one page at a time.
  • <programname> & : Launch a graphical program. For example:

    • sudo gedit /etc/apt/sources.list & : Launch a visual editor
      to edit the /etc/apt/sources.list file.

    You can also omit the & at the end, but then you can’t run more commands in that window until the graphical program has finished.

The commandline is called a “shell”. The default shell in Ubuntu is “bash”.

Files and Directories

The files on your Linux system are organized into nested folders. Folders can contain other folders.

A synonym for “folder” in the Linux world is “directory”.

The topmost folder is called the root folder. It is symbolized by “/” . All other folders are nested in this one (== “all other folders are subfolders of this one”). Contrast this to drive letters (c:\, e:\) in Windows.

For example, /usr/share/doc/less/ means:

  • The root folder contains a folder called “usr”
  • The “usr” folder contains a folder called “share”
  • The “share” folder contains a folder called “doc”
  • The “doc” folder contains a folder called “less”

Inside these folders may be many folders and many files.

Notice that Linux uses “/” to separate folders, and Windows uses “\”.

Permissions

Files and folders in Linux have permissions, which specify which users can access the file/folder, and what they can do. Here is an
example obtained by running the command ls -ld /etc/cups:

drwxr-xr-x 4 root lp 4096 2009-12-01 03:55 /etc/cups 
  • Every file has an owner and a group . The owner of this folder is “root” and the group for this file is “lp”.

  • Files and folders are distinguished by the first letter in the listing. You can tell that “/etc/cups” is a folder because it begins with the letter “d” for “directory”. A regular file begins with “-“.

In addition to owners, groups and types, files and folders have
permission sets:

  • read permission, indicated by “r”. For files, this indicates who can look at the contents of the file. For folders, this indicates who can list the contents of the folder (see what other files and folders it contains).

  • write permission, indicated by “w”. This indicates who can change the file, or add contents to the folder.

  • execute permission, indicated by “x”. For files, this indicates that the file can be run like a program (which is a property often used when writing scripts in Linux). For folders, this indicates who can enter the folder.

You can get weird combinations, such as being able to enter a folder but not list its contents.

Permission sets are specified for three different kinds of users:

  • The owner of the file.
  • The group of the file.
  • Everybody else, indicated as other.

So the folder /etc/cups has the following permissions:

  • read, write, and execute permissions for the owner “root”
  • read and execute permissions for the group “lp”
  • read and execute permissions for everybody else

What this means is that everybody can enter the folder and display its contents, but nobody except root can add new files.

Some Important Folders

  • /home : User folders — where your personal files are kept /home/<username>/Desktop is your desktop. The character ~ is a synonym for your home folder: /home/<username>

  • /etc : System-wide configuration files. Some examples:

    • /etc/apt : Repository information
    • /etc/fstab : How hard drives and other peripherals get mounted

    Personal (per-user) configurations are kept in “dotfiles” in your home folder. e.g. /home/ubuntu-user/.config

  • /media : Where USB keys and CD-ROMs get mounted

  • /var/log : System logs

  • /usr/bin : Most program files

  • /usr/share/doc : Documentation included for individual packages

  • /proc : System information.

  • /root : The home directory of the root (administrative) user. Don’t get this confused with / , which is confusingly called the “root folder”.

  • . and .. : These are not specific folders, but rather shortcuts. . is a shortcut for “the current folder”. .. is a shortcut for the parent folder — the folder that contains this one.

Text Editors

nano

nano is a commandline text editor that Ubuntu installs as the default commandline editor. It is not a great editor for editing configuration files, but it is fairly easy to use.

The commands are at the bottom of the screen. The ^ character means <ctrl> . For example, to save your file, hit ^O, which means <ctrl> + o

gedit

gedit is a graphical text editor. It is fine to use this for editing files as long as you launch it with the appropriate permissions.

For example: sudo gedit <filename> &

Other editors

nano is not a great editor for editing configuration files. gedit is good but it requires a graphical user interface.

There are many many alternatives available: vim (included by default on most Linux distros), jed, jove…

Use whatever editor makes you feel most comfortable.

Using OpenOffice.org to edit text files is not a good idea, because unless you are careful they save in formats that insert a lot of formatting codes you don’t want in text files.

Homework

  1. What permissions does the file /var/log/messages have?

  2. What command allows you to look at the content of /var/log/messages ?

  3. How many folders deep is the file /var/log/messages ?

  4. What are the contents of the file /etc/timezone ?

  5. Using the graphical interface, navigate to the folder /var/log and click on the messages file. Then look at the file’s properties and look at its permissions. Do they correspond to what you see on the command line?

  6. Use a text editor of your choice to create and edit the file
    ~/Desktop/linuxfeelings.txt . In this file, type out some thoughts, feelings, and experiences you have had about using Linux so far. Save the file and close it. Then edit the file again and add the date you edited the file.

  7. Find one example of instructions online that require you to type something on the commandline.

  8. List two topics you would be interested in learning about for Session 5. These can be any topics that are related to Linux or Free Software.

Creative Commons Licence
This work by KW Freeskool is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Advertisements

Comments are closed.