Linux Shell Basics

Read time: 36 minutes (9059 words)

If you have never worked on a command line, you need a quick overview of the basic commands you need to be able to use to get your work done. This quick guide will get you started, and a better overview can be found here:

The Prompt

When you first open up a Terminal program, you are presented with a basic black screen with white test. You see a line that looks something like this:

rblack@ubuntu:~$

That last character, “$”, is the actual prompt. You will type in a command after that character as you work.

The first part of the line identifies the user (“rblack”) and the machine name (“ubuntu”) to help you figure out what machine you are working on at the moment. Trust me, as you start working with servers on the Internet, it is easy to lose track of what machine you are working on!

I have a problem with this setup, especially in a classroom where I project the screen. Seeing the text on that black background is difficult. You can change the screen by doing this:

$ setterm -term linux -back white -fore black -clear

You can use other colors for the foreground and background colors:

  • black
  • blue
  • green
  • cyan
  • red
  • magenta
  • yellow
  • white
  • default

Creating Directories

The command for building a new directory is this:

  • mkdir <path> - where <path> names the new directory

Here are some examples:

  • mkdir temp - create a new directory named temp in the current working directory
  • mkdir ../temp - create temp in the directory above the current working directory
  • mkdir /home/rblack/COSC2425 - create the named directory

Permissions

Every file and directory is “owned” by some user on the system, and has a set of permission controls that define what any user can do with them. You must have proper permission to read a file, create a file or directory, or run a program file. On Linux/Mac these permissions are controlled by a set of “permission bits” and there are bits defined for three sets of users:

  • Owner - the user who created this file or directory
  • Group - several users can be placed in a group to form a team
  • Others - all other users not in the other two groups

You can see the permissions on any file of directory on Linux/Mac by using this command:

  • ls -l - display full info n things in this directory

Linux/Mac use several characters in this display:

  • d - this is a directory
  • r - read permission is set for this group
  • w - write permission is set for this group
  • x - execute permission is set for this group
  • - - this bit is not set

There will be three sets of three “bits” for the owner, group, and other set of users.

Here is an example:

ls -l /usr/bin/git
-rwxr-xr-x 1 root wheel 14160 Sep 29 00:38 /usr/bin/git

This is a simple file that contains the program. The owner is “root” and that user can do anything with it (“rwx”). The group “wheel” can read and execute, but not write the file, as can all other users. We also see how big the file is in bytes, and when it was created.

Deleting Things

The command to delete a file or directory on Linux/Mac is this:

  • rm file_name - delete the named file
  • rm -rf directory_name - delete this directory and everything under it!

Warning

Be careful with that last command, you can delete a lot of files with it by accident! And, there is no recovery from this.

Note

There is one user, called the “super user” or “root” who can get around these permission controls.

Viewing Files

If you want to see the contents of a text file, there are a few ways to do that:

  • cat file_name - send the text file to the console
  • more file_name - show the file a “page” at a time

Redirection

The Terminal program displays text that simple programs output. Any required input is provided by you typing on the keyboard. There two streams of characters are called standard output and standrd input. You can “redirect” both of these streams of text by using redirection characters on the command line. Here is a common example:

* `prog_name < input_file > save_file`

This command runs the program prog_name. Any input needed by that program will come from a text file named input_file, and any output generated by the program ends up captured in save_file. If you leave off either, the input/output go to the default standard devices (screen for output keyboard for input).

You can send the output of one program to the input of another program as follows:

  • prog1 | prog2 > save_file

This example runs prog1 using standard input for any input needed. The output from this program is fed to prog1, and the output from that program ends up in save_file. This kind of control can be handy, and is the foundation of the Unix way of doing things. There are tons of simple programs that you can use to ‘filter” text as it moves from program to program.

Running Your Programs

You will be compiling programs in a directory you create. That directory will not be registered on the system PATH, so running it needs a slightly different command.

On Mac and Linux, program file names have no extension, just a name. If the program is sitting in your current working directory and it named test_prog, we can run it as follows:

$ ./test_prog

The dot-slash tells the operating system to look for the program in the current directory. It will not find it any other way! Windows does not have this issue, it looks automatically in the current directory.

There is a lot more to learning how to comtrol Linux from the command-line. We will show you more during lab sessions, as new things are needed.