3.1. Moving Around in the Unix World¶
3.1.1. Video for this Section¶
Video for this section is not available yet.
3.2. Get Some Files¶
In order to learn about unix files and directories, let’s get some files and directories that we can explore together.
To do this we will use the tar
command. Tar
is an archiving
tool for packing up a set of files and directories for portable
“shipment”, and you will often see a, so-called, tarball (an archive
file in the tar format) for download on the web.
A tar archive is really just all the files “glued” together, one
after the other, with some header information. Usually, this file is
then compressed in size using the gzip method. A similar way to do
both of these tasks (archiving several files into one and compressing
the archive) is the zip format, which creates files that end in
.zip
.
You can download the tar file for our course by copying this link, pasting it into a shell terminal window at the prompt, and hitting ENTER.
curl -O dirac.physics.pacific.edu/phys/jhetrick/www/PHYS27/unixplay.tar.gz
Once you have downloaded the file, do a directory listing and see if you see the file unixplay.tar.gz.
If so, you can unpack the archive by typing (or cutting and pasting) the following command at the prompt:
tar zxvf unixplay.tar.gz
Now do a listing and verify that this command has created a new directory called unixplay/
.
We’ll come back to these files shortly.
3.3. The Unix File System¶
Now that you have your Fedora system running and have some files to explore, let’s learn to move around a bit. We’ve got to get used to doing things through a text based shell window. While that might seem extremely limiting at first, it’s actually much more efficient, especially when you are connecting to another computer (like a supercomputer on the other side of the country). A shell terminal uses very little bandwidth since it only needs to transfer characters instead of pictures and mouse movements.
You are probably used to the Point-and-Click (GUI, or G*raphical *U*ser *I*nterface) environment of Windows and Macs: when you want to do something, you (double) click on its **icon*, possibly enter some parameters (a file to edit for example), and then work within that appliction. At the end you might save your work to a file, or print it. The other big difference is that to find your files, you use a Graphical File Browser , like the one below. (We’ll come back to the boxes marked with an arrow in just a minute)
in Windows, or
on a Mac.
To find files and folders, you navigate your way around by clicking on icons, like this
Note
In Unix, folders are called directories.
That’s why the Unix command to move to a new directory is called cd, for Change Directory . This command moves you to another folder–which I will henceforth call by its proper name, a directory.
Using a Graphical File Browser, you usually see all the files and subfolders as little icons which often represent the type of file. You probably know how to change this behavior.
In your Host OS–either Windows (PC) or OSX (Mac), open some Folder on your computer with many files.
Click the red box marked on the images above, on the top menu bar.
Choose to simply List the files, or show the Details. Also you see that Folders are shown with a small Folder icon so that you can distinguish these from regular files.
Notice that with Details you get size information, type, and date of last modification.
Now change the setting back to what it was before (or a new one if you like).
We will do something very similar to this in a Unix shell window, listing files from the prompt in a text-based shell window. You will soon find it is almost as easy as the GUI way, and in many cases more powerful.
Open an shell/terminal window.
There is a default directory for each user, called the Home directory.
You may recall from the last chapter that the “full name” of your Home directory is ‘’/home/YOURUSERNAME’’ .
When you start a shell , or if you login to a computer where you have a unix account, the shell will give you a prompt, and place you in your Home directory. This is where your init files are, like ‘’.bashrc’’ and others.
In fact, the way I have set up your system, the prompt tells you this.
A short hand symbol for your HOME dir is the symbol “~” (the tilda). Notice that in your prompt there is the following: [~], meaning that you are working in your HOME dir (I will often say “dir” for directory).
3.3.1. Changing to a different directory¶
We’ve already learned about this, but mention it here again for completeness.
3.3.1.1. cd
(change directory)¶
The command cd means change the current working directory to ‘directory’. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.
Let’s change to the unixplay/
directory that you have just made
by unpacking the tar file.
Note
I’m referring to the directory as unixplay/
with a /
at the
end. This is a unix-guru way of saying that unixplay is a
directory (by writing it with an ending /
. However you don’t
have to add that /
when typing it in the the cd
command.
Type the following:
sci[~]>cd unixplay
sci[unixplay]>
Type ls
(or better, the alias l
) to see the contents.
You should see
sci[~/unixplay]>l
anotherFile dirTWO/ gammaData/ scifi_list.txt yetanother
dirONE/ file_unixplay.txt ints.dat testfile
3.4. The Directories .
and ..
¶
Still in the unixplay/
directory, type
la
(alias for ls -a
)
As you can now see, in the unixplay/ directory (and in all other
directories), there are two special directories called (.
)
and (..
)
In UNIX, “.
” means the current directory, so typing
cd .
means stay where you are (the unixplay/ directory).
This may not seem very useful at first, but using “.
” as the name
of the current directory will save a lot of typing, as we shall see
later. We also need the .
when trying to copy or move files to
the current directory.
“..
” means the parent of the current directory, so typing
cd ..
will take you one up one level in the directory tree.
Try it now.
In your unixplay/
directory, type:
cd ..
This should move you back to your home directory and you prompt should show this by showing [~].
Now cd
back down to the unixplay
directory.
You should see [unixplay] in the prompt, helping you to remember where you are.
3.4.1. Going Home¶
Typing cd
with no directory argument always returns you to your
Home directory. This is very useful if you get lost in the file
system.
Try it
If you are in your unixplay/
dir, cd
down one more level
into dirONE/
.
Note
In Unix, names are case-sensitive, meaning that the directory
dirONE
and dirone
are different.
Now, type cd
by itself. This takes you back to your home
directory.
Note
Wherever you are, typing a bare cd
command will take you Home.
Let’s see how far up in the unix file system we can go.
Type cd
to start from your home directory. Does your prompt say [~] ?
Now type: cd ..
to move up one level. Your prompt should have
[home] in it. All users’ home directories are subdirectories
of a dir called home/
.
Type cd ..
to go up from there. You should see [/] in the
prompt.
Type cd ..
again. Did anything happen? No–you should still see
the same [/] in the prompt.
That’s because you have gone up as far as you can go. The top level
directory in a Unix file system is called, simply, /
. Below that
are a number of dirs, one of which is home/
, and below that is
your home dir, e.g. /home/jhetrick/
.
Now type cd
to go home.
3.5. Pathnames¶
Type the following command at the prompt.
pwd
(print working directory)
Pathnames enable you to work out where you are in relation to the
whole file-system. For example, to find out the full pathname of
your dirONE/
directory in the unixplay/
directory.
navigate to dirONE/
directory using cd
, then type
pwd
sci[~]>cd unixplay
sci[~/unixplay]>cd dirONE
sci[~/unixplay/dirONE]>pwd
/home/jhetrick/unixplay/dirONE
The full pathname to the dirONE
directory is
/home/jhetrick/unixplay/dirONE
3.6. Unix commands for files and directories¶
First let’s review the ls
command that we’ve already seen.
Let’s review:
When you first login, your prompt is placed in your home directory. Your home directory has the same name as your user-name, for example, jhetrick (or ” ~” for short).
To find out what files and directories are in your current directory, you type
ls
The ls command lists the contents of your current working directory.
sci[~]>ls
Desktop Downloads Pictures Templates winDesktop
Documents Music Public Videos winDocs
If there were no files visible in your home directory, the prompt would simply be returned.
3.6.1. Command Options (aka Switches)¶
As we have seen, it turns out that ls
does not, actually, list
all the files in your home directory only non-hidden files. In
unix it’s easy to make a hidden file, simply start its name with a dot
(.)
Files beginning with a dot (such as .bashrc
, .emacs
, etc.) are
known as hidden files and usually contain program configuration
information.
However you can list ALL the files in your home dir by giving ls a switch to modify its behavior.
To list all files in your home directory including those whose names begin with a dot, type
3.6.1.1. ls -a
¶
This now produces
sci[~]>ls -a
. Downloads Pictures
.. .emacs Public
.bash_history .esd_auth Templates
.bash_logout .gnome2 .vboxclient-clipboard.pid
.bash_profile .gnome2_private .vboxclient-display.pid
.bashrc .gnupg .vboxclient-draganddrop.pid
.cache .gtkrc-2.0-kde4 .vboxclient-seamless.pid
.config .kde Videos
Desktop .local winDesktop
.dmrc .mozilla winDocs
Documents Music .xsession-errors
As you can see, ls -a
lists many files that are normally hidden.
-a
is an example of an option (sometimes called a switch). The
options change the behaviour of the command. There are online
manual pages that tell you which options a particular command can
take, and how each option modifies the behaviour of the command.
Note
We setup several aliases for ls
with various switches in your
.bashrc
file in the last chapter. You have an alias for ls
-aF
in your .bashrc
file. The alias is la
, so typing
la
is exactly the same as typing ls -aF
.
3.6.1.2. ls -F
¶
Alias: l
Another option I like to add to your toolkit is the -F (Format) switch for ls. This option puts “/”s at the end of each directory, which makes it easier to distinguish files from subdirectories. It also puts a “*” at the end of each file which is executable. These files are programs or “scripts that can be run (like a .exe file in Windows), and an “@” at the end of links.
sci[~]>ls -F
Desktop/ Downloads/ Pictures/ Templates/ winDesktop@
Documents/ Music/ Public/ Videos/ winDocs@
As you can see this option makes it easier to see which files are just files and which are subdirectories. It’s particularley useful when you are working in a terminal which doesn’t support color coding.
Question
Which files are directories? What type are the other files?
3.6.2. Man
(manual) pages¶
A wonderful feature of the unix shell are the man pages. At the prompt, type
man ls
This will bring up the Manual Page(s) for the ls
command. The
man page is a text file which is opened in the less
pager that
we met in the last chapter. less
show you one terminal window
full of the file at a time, and lets you move forward and backward
through the manual with PageUp and PageDown (or SpaceBar and “b”).
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort
entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
with -l, print the author of each file
-b, --escape
print C-style escapes for nongraphic characters
[More]
The man page is GREAT for learning about all the possible switches and is useful
when you want to do something and think ls
might have a switch that does what you
want.
For example, scrolling through the man page for ls
, you can see that one of the
switches is -1
(minus one). This switch lists one file per line. I have used that
switch many times when I need to produce a single column list of files to pass to
another program.
3.6.2.1. Apropos (man -k)¶
If you don’t know the name of a command, but want to search all possible shell commands, you can type:
apropos
SEARCH-WORD
For example, suppose you didn’t know that there was a unix command to list the contents
of your directory (but you did know about the apropos
command). You could type:
apropos list
This produces:
sci[~]>apropos list
uffixes (7) - list of file suffixes
acl (5) - Access Control Lists
appres (1) - list X application resource database
argz (3) - functions to handle an argz list
argz_add (3) - functions to handle an argz list
argz_add_sep (3) - functions to handle an argz list
...
ls (1) - list directory contents
...
XwcTextPropertyToTextList (3) - convert text lists and text property structures
zipinfo (1) - list detailed information about a ZIP archive
zonetab2pot.py (1) - Converts a timezone list to a PO file template.
showing all the commands and Manual entries with the word “list” in the description.
From this list you would learn that there is the ls
command, and could learn
more about it by typing man ls
.
Try doing: man man
3.6.3. Making Directories¶
3.6.3.1. mkdir
(make directory)¶
We will often need to make a subdirectory in your current directory to hold new files, or particularly, when you start a new project.
TIP:
Keep different projects in separate directories and subdirectories, and
give files names that help identify them, like: comptonExptTheta35deg.dat
To make a subdirectory called newdir in your home directory type the following:
mkdir newdir
sci[~]>mkdir newdir
sci[~]>
The shell returns the prompt. However it has just created the newdir directory.
To see the directory you have just created, type l
sci[~]>mkdir newdir
sci[~]>l
Desktop/ Downloads/ newdir/ Public/ unixplay/ Videos/ winDocs@
Documents/ Music/ Pictures/ Templates/ unixplay.tar.gz winDesktop@
3.7. Understanding pathnames¶
First type cd
to get back to your home-directory, then type
l newdir
to list the conents of your newdir directory.
Now type
l subdir
You will get a message like this -
subdir: No such file or directory
The reason is, subdir is not in your current working directory. To use
a command on a file (or directory) not in the current working
directory (the directory you are currently in), you must either cd
to
the correct directory, or specify its “full pathname”. To list the
contents of your subdir directory, you must type
l newdir/subdir
Home directories can also be referred to by the tilde ~
character. It
can be used to specify paths starting at your home directory. So
typing
ls ~/newdir
will list the contents of your newdir directory, no matter where you currently are in the file system.
Try this:
cd
down into subdir/
Try to list the contents of newdir/
by doing ls newdir
.
This fails since newdir/ is not in subdir.
Now use the full pathname with the ~ shortcut:
ls ~/newdir
This time it works.
What do you think
ls ~
would list?
What do you think
ls ~/..
would list?
A handy command to know about is the tree
command. Try it in your home directory.
Type:
tree
at the prompt. It should show you a “tree” (with branches) of your directories and files.
If you just want to see directories, type:
tree -d
with the -d
switch.
If you want to see sizes of files and directories, use the -h
switch (Try it).
3.8. Command Summary¶
Command | Meaning |
---|---|
ls | list files and directories |
ls -a | list all files and directories |
mkdir dir | make a directory called dir |
cd dir | directory change to named directory |
cd | change to home directory |
cd ~ | change to home directory |
cd .. | change to parent directory |
pwd | display the path of the current directory |
man cmd | page through manual entries for cmd |
tar zxvf file.tar.gz | unpack a .tar.gz file |
tree | show directory tree structure |
aliases | equivalent to | meaning |
---|---|---|
l | ls -F | list (formatted) |
la | ls -aF | list all (incl. hidden) |
ll | ls -lF | long list |
lt | ls -t | head -20 | list most recent |
m | less | file pager |
fin file | find . -name file | find a file |