1.5. Introducing Gnu/Linux¶
Early in its evolution, the command-line environment of UNIX (its only user interface back then) became dominated by dozens of small utilities and tools which are still in common use today. These tools are small and generally do one thing well. Most read from ‘standard input’ and write to ‘standard output’. The tools are commonly chained together in longer command pipelines, one program passing its output to the next as input, and controlled by a variety of command-line options and arguments.
This is one aspect of UNIX that makes it a powerful environment for processing text-based data, one of its first uses in a corporate environment. Dump some text in one end of a command pipeline and retrieve processed output from the other end. As programmers, this is useful, because at its core, programming involves manipulating text: transforming source code into executable machine code, converting database query results into readable reports, etc.
This section is not intended to make you an expert if you have never used linux before. It is intended to give you enough of a jump start to demystify things somewhat and ease forward progress.
1.5.1. The command line¶
A command line, or terminal, is a text based interface to the system. You enter commands by typing them on the keyboard and feedback is returned as text.
The command line typically presents you with a prompt. As you type, it will be displayed after the prompt. Most of the time you will be issuing commands. Here is an example:
host@user: ls -l /home/user
total 12
-rw-rw---- 1 user student 419 May 29 06:49 main.cpp
-rw-rw---- 1 user student 3320 May 29 06:50 util.h
-rw-r--r-- 1 user student 725 May 29 06:49 vim_tips.md
host@user:
What’s happening?
The first line is the prompt ( host@user ). On this first line, a command was also entered
ls
. Thels
command lists the contents of a directory. In this example, we also supplied some command line arguments (-l /home/user). It is conventional to start command line arguments (or ‘switches’) with a dash.The next 4 lines are output from the ‘ls’ command. Most commands produce output that is written to standard output, which in the case of running on the command line is the terminal itself.
The last line displays the command prompt again. At this point, the system is waiting for the next command.
If no prompt is displayed, this means the command is still running.
1.5.2. Absolute vs relative paths¶
Look at the argument passed to the ls
command in the previous example:
/home/user
Specifically, this indicates the absolute path to the directory /home/user
.
There are 2 types of paths we can use, absolute and relative.
All references to a file or directory use one of these forms.
- Absolute path
Specifies a location starting at the root directory. An absolute path must start with the character ‘/’.
- Relative path
Specifies a location in relation the current working directory. A relative path will never start with the character ‘/’.
When navigating or specifying paths, there are two special files which
reside in every directory: .
and ..
.
- .
The single dot
.
file contains information about the files in the current directory. In many commands, it is synonymous with the current directory.- ..
The two dots
..
file contains information about the files in the directory that is the parent of the current directory. The only directory that does not contain a..
file is the root directory:/
.
In addition, the character ~ (tilde) is an abbreviation for the path to any user home directory.
ls ~alice/public_html
Displays the contents of the public_html folder in her home directory, regardless of where her home directory actually is located.
Self Check
- ../usr/bin
- Correct. ../usr/bin does not start with a '/'.
- /usr/bin
- /usr/bin starts with a slash.
- ./
- Correct. ./ does not start with a '/'.
- /
- / this is the 'root' directory.
- user/files
- Correct. user/files does not start with a '/'.
sc-1-1: Which of the following statements are relative paths? Check all that apply.
- In directory /tmp, ../home/user
- Correct.
- In directory /home, ../home/user
- Correct.
- In directory /home/user, ..
- This lists the contents of /home.
- In directory /home/user/work, ..
- Correct.
- In directory /home/user/work, ../../user
- This attempts to list the contents is /user.
sc-1-2: Which of the following paths list the contents /home/user
? Check all that apply.
1.5.3. Basic commands¶
- pwd
Prints the working directory name.
pwd
prints the full path name of the directory you are currently in.- ls
Print directory contents. With no arguments,
ls
prints the current directory contents, that is the contents of the directory returned bypwd
. For each operand that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, associated information.The
ls
command has a lot of options.- cd
Change directory. With no arguments, (just
cd
), this command will take you to your home directory. A file after the cd commandcd ../lab/solutions
changes the current working directory to the named path.The special name
~
is a shortcut for the users home directory.- mkdir
Make a new directory. At least one argument is required. For example:
host@user: cd host@user: ls -l total 4 -rw-rw---- 1 user student 419 May 29 06:49 main.cpp host@user: mkdir labs host@user: ls -l total 8 drwx------ 1 user student 419 May 29 15:06 labs -rw-rw---- 1 user student 419 May 29 06:49 main.cpp
- man
Display manual pages for a command.
There is an enormous amount of information available from the command line. Conceived of and written before the internet existed, it was intended to function as a comprehensive reference for everything a programmer would need to know to be productive in Unix. For example:
host@user: man ascii ASCII(7) Linux Programmer's Manual ASCII(7) NAME ascii - ASCII character set encoded in octal, decimal, and hexadecimal DESCRIPTION ASCII is the American Standard Code for Information Interchange. It is a 7-bit code. Many 8-bit codes (such as ISO 8859-1, the Linux default character set) contain ASCII as their lower half. The international counterpart of ASCII is known as ISO 646. The following table contains the 128 ASCII characters. C program '\X' escapes are noted. Oct Dec Hex Char Oct Dec Hex Char ──────────────────────────────────────────────────────────────────────── 000 0 00 NUL '\0' 100 64 40 @ 001 1 01 SOH (start of heading) 101 65 41 A 002 2 02 STX (start of text) 102 66 42 B 003 3 03 ETX (end of text) 103 67 43 C 004 4 04 EOT (end of transmission) 104 68 44 D 005 5 05 ENQ (enquiry) 105 69 45 E 006 6 06 ACK (acknowledge) 106 70 46 F 007 7 07 BEL '\a' (bell) 107 71 47 G 010 8 08 BS '\b' (backspace) 110 72 48 H 011 9 09 HT '\t' (horizontal tab) 111 73 49 I 012 10 0A LF '\n' (new line) 112 74 4A J 013 11 0B VT '\v' (vertical tab) 113 75 4B K 014 12 0C FF '\f' (form feed) 114 76 4C L 015 13 0D CR '\r' (carriage ret) 115 77 4D M 016 14 0E SO (shift out) 116 78 4E N 017 15 0F SI (shift in) 117 79 4F O 020 16 10 DLE (data link escape) 120 80 50 P 021 17 11 DC1 (device control 1) 121 81 51 Q 022 18 12 DC2 (device control 2) 122 82 52 R 023 19 13 DC3 (device control 3) 123 83 53 S 024 20 14 DC4 (device control 4) 124 84 54 T 025 21 15 NAK (negative ack.) 125 85 55 U 026 22 16 SYN (synchronous idle) 126 86 56 V 027 23 17 ETB (end of trans. blk) 127 87 57 W 030 24 18 CAN (cancel) 130 88 58 X 031 25 19 EM (end of medium) 131 89 59 Y 032 26 1A SUB (substitute) 132 90 5A Z
(remainder ommitted)
For these last four ‘core’ commands,
use the man
command to learn detailed information about how they function,
if you need it.
- cp
Copy files or directories
- mv
Move files or directories
- rm
Remove files or directories
- passwd
Change your login password
Try This!
Use this example terminal to try the commands described in this section.
It’s a sandbox, feel free to play for a bit.
The top window is a file where you can type and save commands.
The bottom window is a linux shell. Commands typed directly in the shell are not saved.
Try This!
Modify this program to make the output look like the table in the ascii man page.
More to Explore
UNIX Text Processing - one of the best general references for people new to Unix or GNU/Linux.
GNU / Linux tutorial - from debian.org
UNIX Philosophy - from Wikipedia
Learn Enough™ Command-Line to be Dangerous tutorial by Michael Hartl.
Ryans Tutorials: Linux - a decent introduction to the linux command line