UNIX File Structure
The UNIX operating system supports a tree-like hierarchial file
structure that is managed by directories. The highest level is
known as the "root" and below the root is other directories
and files. Your home directory (login directory) is specified
by '~' and '.' is the directory that you are currently in. Also,
the parent directory can be given by '..' for all directories
except root.
A pathname specifies the name of a directory/file and can be expressed
as a complete or relative path. Complete names begin with a '/'
and follow directories from the root. The directory/file names
are also separated by slashes. Relative file names do not begin
with a '/' and can only go down the tree. The 'cd pathname' changes
directories.
Example:
If sally logs in, her home directory is point a
Basic UNIX Commands
- ssh ada.cs.pacificu.edu remote login providing secure encrypted communications over an insecure network between two untrusted hosts
- passwd changes your password (if you want to permanently change your password, you need to log into ada directly and make the change)
- cd .. changes to point b
- cd changes to your home directory no matter where you are
so if you get lost, cd gets you home
- cd / changes to the root directory
- ls ~ OR ls will list first.pas (all files in your home directory)
- ls /usr/games lists zork and aliens (the files in the directory games)
- ls -al lists the long format of the directory contents including . files
- pwd lists /users/CS440/sally (the present working directory)
- mkdir backup will make a directory below sally called backup
P1: Make a directory called csdir in your home directory on ada.
- cp p1.c bp1.c will make a duplicate copy of p1.c and name it bp1.c
P2: Copy the files f1.c and f2.c from ../unixdir
- mv p1.c bp1.c will rename the file p1.c to bp1.c.
P3: Make a backup of f1.c
- rm p1.c will remove the file p1.c in the current directory.
- rmdir backup will remove a directory called back if it's empty
- cat filename lists an entire text file to the screen
- more filename lists an entire screen's worth of the text file and prompts the user for more
- space - gives the next screen of information
- return - gives one more line
- q - quits more
P4: Copy ../unixdir/psfiles and do a more on the file
- lpr filename - will print a file to the HP Laser in the computer lab.
Please do not print anything unless you are going to pick up the printout right away.
- gcc filename.c - will compile the C program filename.c and by default produce an executable named a.out
P5: Compile and execute f1.c
- ps lists your running process id #'s and associated commands
- ps aux detailed listing about all running processes
- grep prints the lines matching a pattern
P6: Execute the command ps aux | grep ryandj
- diff file1.c file2.c compares files line by line and depending on the flags used, will show the differences
P7: Do a diff on f1.c and f2.c
- kill -9 PID kills the running process and all child processes (PID must be a process #)
- tar czf HelloWorld.tar.gz HelloWorld creates a tape archive of the HelloWorld project in zipped form
- scp file1.c user@zeus.cs.pacificu.edu:cs360
is a remote file copy program between hosts on a network. In this case
file1.c in the present working directory is copied remotely into the
directory cs360 on zeus.
- touch filename.h changes the timestamp of a file. This is useful if you want make to recompile certain parts of a project.
- top displays task information that is being managed by the kernel
- ksnapshot is a KDE screenshot tool
- df displays amount of free space on the partitions
- du displays file space usage
- man command (e.x. man rm) will give you the man pages for the command following man.
- vi filename runs the vi editor which allows you to edit a file on any UNIX machine.
More UNIX Features
When developing large software systems, the more
command of the OS you have, the more productive you can become. To help
get you started on some of the more advanced UNIX commands and
features, I will outline some that I use.
- Be familiar with cat, more, less, head, and
tail commands. Use the man pages and look up these commands. Here are a
few other sites that have some tutorial help and more UNIX commands: http://www.debianhelp.co.uk/commands.htm AND http://www.fsid.cvut.cz/cz/U201/LINUX.HTML.
- Redirecting output from one command to the input of another through a pipe is essential (e.g. ls -al | more).
- The
.bash_profile file contains ways for you to customize your development
environment. The name of this file changes depending on your login
shell. A primitive .bash_profile might be the following:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
BASH_ENV=$HOME/.bashrc
USERNAME=""
export USERNAME BASH_ENV PATH
# .bashrc
# User specific aliases and functions
set history="25" #history displays last 25 commands executed
alias l='ls -al' #the key l gives detailed listing
alias rm='rm -i' #prompt user before removing files
alias p='lpr' #the key p prints to the default printer
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
Note: The following in your .bash_profile will search for executables in current directory or bin
PATH=$PATH:/usr/local/bin:.
Source: http://www.tldp.org/LDP/gs/img123.gif
Questions assuming you always begin in the directory bin:
- How would you list the contents of the directory?
- What is the absolute path of the directory from root?
- How could you copy a file named file1.c from this directory to yours?
- List two ways you could move to the directory larry.
Problems:
- Make sure that . is in your path. If it isn't, put it there.
- Add some kind of alias to your .bashrc file.
- Create a simple C hello.c program to print out Hello World. Compile the program and run it.
- Create a directory called cprogs and place hello.c into the newly created directory.