September 2, 2004 --- Class 2 --- Hierarchical File System, Special Characters Activities: Introduction to UNIX and hierarchical file system UNIX has a hierarical file system. It is a lot like a file cabinet, with subdirectories playing the roles of drawers and file playing the role of file folders. However, in UNIX there are more levels in that any subdirectory can have a file in it that is itself a subdirectory. There is a special directory called the root of the file system and denoted by / . A full path name of a file starts with /. However, when you are typing commands to a "shell", or UNIX interpreter, you are considered to be "in" a particular directory. The command pwd stands for "print working directory" and will tell you where you are. You can refer to a file with a relative path name by starting the name with a subdirectory of your current working directory. You should also know that ~ stands for your home directory in most shells, and that ~username stands for the home directory of user whose login name is username. Thus, ~sg/bin stands for my bin subdirectory. Commands relevant to directories: pwd print working directory cd change directory mkdir make a new directory The ls command stands for list files. Explore what it does by trying the following commands ls ls / ls -a ls -l ls -lt To find out more about ls, use the manual command man ls Special directory names . stands for the current directory .. stands for one level up in the directory hierarchy Special or Metacharacters in UNIX UNIX has a number of characters that are special to the command interpreter, or shell. The following characters are used in filename matching: * ? [ ] - { } , * matches any number of characters, so ls m* lists all files that start with the letter m. UNIX is case sensitive. So ls M* is not the same. ? matches any single character, so ls m? lists only files whose names are two characters long and begin with m. ls m?? would match files whose names are three characters long and begin with m. [ and ] are used to make a list of single characters. [abf] matches any of the characters a, b or f. [a-j] matches the range of characters a, b, ... j. [0-9] matches any digit. For example, ls *.[cf] would match any file that ends in .c or .f, which is usually how we name C and Fortran programs, respectively. { and } are used to make a list of multicharacter matches. For example, {abc,red,green} will match the three strings "abc", "red" or "green". It is also possible to get the ls command to list all files that do not match a certain pattern. We used the man page to try to do this, but it did not seem to work during class. After class, on a Linux machine I found that either of these forms works: ls -I "*.c" ls --ignore="*.c" The double quotes keep the expression with the metacharacter from being expanded. I have also verified that they work on the Suns. There are other special characters in UNIX that do things other than match characters. $ is used to denote a variable name. For example there is a path variable. Try the following examples echo path echo $path In the second line we get the path variable, not just the string path. ; is used to separate two commands on a single line, e.g. echo $path; date \ at the end of the line is used for line coninuation if you have a long command. The carriage return must immediately follow the \. \ is also used to escape the special meaning of a character. Try this echo \$path / we have already seen is used for separating (sub)directory names, and for root ~ is used for home directories ( ) are used in pairs to combine the output of two UNIX commands. This will be explained more when we need it. & at the end of a command line puts the command in the background so that you immediately get back a prompt and can issue another command. The command in the background continues to run, however. | < > are very useful as we will now explain.