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

  1. ssh ada.cs.pacificu.edu remote login providing secure encrypted communications over an insecure network between two untrusted hosts
  2. passwd changes your password (if you want to permanently change your password, you need to log into ada directly and make the change)
  3. cd .. changes to point b
  4. cd changes to your home directory no matter where you are so if you get lost, cd gets you home
  5. cd / changes to the root directory
  6. ls ~ OR ls will list first.pas (all files in your home directory)
  7. ls /usr/games lists zork and aliens (the files in the directory games)
  8. ls -al lists the long format of the directory contents including . files
  9. pwd lists /users/CS440/sally (the present working directory)
  10. mkdir backup will make a directory below sally called backup
    P1: Make a directory called csdir in your home directory on ada.
  11. 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
  12. mv p1.c bp1.c will rename the file p1.c to bp1.c.
    P3: Make a backup of f1.c
  13. rm p1.c will remove the file p1.c in the current directory.
  14. rmdir backup will remove a directory called back if it's empty
  15. cat filename lists an entire text file to the screen
  16. more filename lists an entire screen's worth of the text file and prompts the user for more
    1. space - gives the next screen of information
    2. return - gives one more line
    3. q - quits more
      P4: Copy ../unixdir/psfiles and do a more on the file
  17. 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.
  18. 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
  19. ps lists your running process id #'s and associated commands
  20. ps aux detailed listing about all running processes
  21. grep prints the lines matching a pattern
    P6: Execute the command ps aux | grep ryandj
  22. 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
  23. kill -9 PID kills the running process and all child processes (PID must be a process #)
  24. tar czf HelloWorld.tar.gz HelloWorld creates a tape archive of the HelloWorld project in zipped form
  25. 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.
  26. touch filename.h changes the timestamp of a file. This is useful if you want make to recompile certain parts of a project.
  27. top displays task information that is being managed by the kernel
  28. ksnapshot is a KDE screenshot tool
  29. df displays amount of free space on the partitions
  30. du displays file space usage
  31. man command (e.x. man rm) will give you the man pages for the command following man.
  32. 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.


# .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:

  1. How would you list the contents of the directory?
  2. What is the absolute path of the directory from root?
  3. How could you copy a file named file1.c from this directory to yours?
  4. List two ways you could move to the directory larry.

Problems:

  1. Make sure that . is in your path. If it isn't, put it there.
  2. Add some kind of alias to your .bashrc file.
  3. Create a simple C hello.c program to print out Hello World. Compile the program and run it.
  4. Create a directory called cprogs and place hello.c into the newly created directory.