Jump To Content

The Shell - Navigation and Creation

Before Starting this Lesson

You should have Cygwin installed on Windows, or have a bash shell available on other platforms.

Navigating the File System

The easiest way to learn basic Unix commands is to see them in action. For example, type pwd (short for "print working directory”) to find out where you are.
$ pwd
/home/hpotter/swc

Then type ls (for “listing”) to see what's in the current directory.
$ ls
LICENSE.txt conf data docs index.swc license.swc
Makefile config.mk depend.mk img lec press

To see what's in the data directory, type ls data.
$ ls data
bio elements haiku.txt morse.txt pdb solarsystem.txt

Equivalently, you can type cd data to “go into” the data directory, then type ls. Type cd .. to go back to where you started
$ cd data
$ pwd
/home/hpotter/swc/data
$ ls
bio elements haiku.txt morse.txt pdb solarsystem.txt
$ cd ..
$ pwd
/home/hpotter/swc

Execution Cycle

When you type a command like ls, the OS:
• Reads characters from the keyboard
• Passes them to the shell (because it's the currently active window)

The shell:
• Breaks the line of text it receives into words
Looks for a program with the same name as the first word
• Runs that program

That program's output goes back to the shell…which gives it to the OS…which displays it on the screen.

running_program.png


All well-designed software systems work this way. They break the task down into pieces, write a tool that solves each sub-problem, and then just "hook them up". This allows you to:
• Develop and test components independently
• Replace or re-use components incrementally
• Add new components as you go along

Providing Options

You can make ls produce more informative output by giving it some flags. By convention, flags for Unix tools start with "-" , as in "-c" or "-l". Also, some flags take arguments (such as filenames).

Show directories with trailing slash:
$ ls -F
LICENSE.txt conf/ data/ docs/ index.swc license.swc
Makefile config.mk depend.mk img/ lec/ press/

Show all files and directories, including those whose names begin with "." (by default, ls doesn't show things whose names begin with ".". This means . and .. don't always show up. (We'll see what the .svn directory is for later)
$ ls -a
. .svn Makefile config.mk depend.mk img lec
.. LICENSE.txt conf data docs index.swc


Flags can be combined
$ ls -a -F
. .svn/ Makefile config.mk depend.mk img/ lec/
.. LICENSE.txt conf/ data/ docs/ index.swc

Creating Files and Directories

Rather than messing with the course files, let's create a temporary directory and play around in there:
$ mkdir tmp

(Note: this produces no output, but -v (“verbose”) would tell mkdir to print a confirmation message)
Now go into that directory - there are no files there yet.
$ cd tmp
$ ls

Use the editor of your choice to create a file called earth.txt with the following contents:
 Name: Earth
 Period: 365.26 days
 Inclination: 0.00
 Eccentricity: 0.02

You can use Notepad (on Windows), which runs in a window of its own. You can also use Pico (on Unix), which takes over the shell window temporarily. The easiest way to create a similar file venus.txt is to copy earth.txt and edit it:
$ cp earth.txt venus.txt
$ edit venus.txt
$ ls -t
venus.txt earth.txt
Note: -t tells ls to list by modification time, instead of alphabetically

Looking at Files

You can check the contents of the file using cat (short for “concatenate”), which prints the contents of a file to the screen.
$ cat venus.txt
Name: Venus
Period: 224.70 days
Inclination: 3.39
Eccentricity: 0.01

You can also compare the sizes of the two files using: ls -l (“-l” meaning “long form”)
Note the fifth column is size in bytes:
$ ls -l
total 2
-rwxr-xr-x 1 gvwilson None 73 Jan 4 15:58 earth.txt
-rwxr-xr-x 1 gvwilson None 73 Jan 4 15:58 venus.txt

Another way to compare is using wc (for “word count”). The columns show lines, words, and characters:
$ wc earth.txt venus.txt
4 9 73 earth.txt
4 9 73 venus.txt
8 18 146 total

Basic Tools

manDocumentation for commands.
catConcatenate and display text files.
cdChange working directory.
clearClear the screen.
cpCopy files and directories.
dateDisplay the current date and time.
diffShow differences between two text files.
echoPrint arguments.
headDisplay the first few lines of a file.
lsList files and directories.
mkdirMake directories.
morePage through a text file.
mvMove (rename) files and directories.
odDisplay the bytes in a file.
passwdChange your password.
pwdPrint current working directory.
rmRemove files.
rmdirRemove directories.
sortSort lines.
tailDisplay the last few lines of a file.
uniqRemove adjacent duplicate lines.
wcCount lines, words, and characters in a file.

viverson
  • Authority 187
Post Body
viverson said:

a table would be nice under basic tools

  • Quote
  • Posted 11 months ago.
anteaya
  • Authority 237
Post Body
anteaya said:

thank you, I was looking for some basics for linux commands. this helps me.

  • Quote
  • Posted 9 months ago.
  • Your comment will be modifiable for 10 minutes after posted.

Page Author

Avatar
viverson
Name
viverson

From Here You Can…

Information

© 2008 Vicki Iverson, All Rights Reserved.