Files and Directories

Contents

Introduction

  • UNIX lets you store "stuff" inside files.
  • "Stuff" can be a wide variety of things:

    • email
    • things downloaded off the network
    • letters and memos
    • spreadsheets, databases, etc.
    • configuration information for programs you run
    • program source code
    • executable code generated by a compiler
    • audio files
    • ... And so on.

  • Standard example of how to think of a file is a normal office folder.
  • UNIX also provides directories to let you manage files.
  • Usually make a directory to keep related things in, e.g. you might:
    • Keep all your email saved in a directory named Mail.
    • keep all files related to a certain project in a directory with that project's name.
    • Keep all files downloaded off the net in a directory named download.
    • Make a directory named tmp for temporary files.
    • Keep all your own programs in a directory named bin.
  • When sys-admin creates a user account, each user is given their own home directory.
  • When you log in, you will be in your home directory.
  • The command pwd will show you what directory you are in. It stands for 'present working directory'.

    
    	% pwd
    	/home/visitors/kns
    	% 
    
    

  • You can move to a different directory with the cd command, but you will need somewhere to go first.
  • You can see what files and directories are in your current directory with the ls command.
  • Create a directory with the mkdir (make directory) command:

    
    	% pwd
    	/home/visitors/kns
    	% ls
    	% mkdir tmp
    	% ls
    	tmp
    	% cd tmp
    	% pwd
    	/home/visitors/kns/tmp
    	% 
    
    

  • Beginners sometimes find themselves lost after using cd to move around, losing track of where they are.
  • Always keep the pwd command in mind.
  • Using cd with no "arguments" will always return you to your home directory:

    
    	% pwd
    	/home/visitors/kns/tmp
    	% cd
    	% pwd
    	/home/visitors/kns
    	% 
    
    

  • The UNIX spec provides many more commands to work with files and directories.
  • The command to remove directories is rmdir (remove directory):

    
    	% ls
    	tmp
    	% rmdir tmp
    	% ls
    	% 
    
    

  • This only works if the directory is empty. If there are any files or other directories inside of tmp then rmdir will not work.
  • As with most UNIX commands, if you have more than one directory to remove, you can give more than one name in the same rmdir command line.
  • UNIX organizes files and directories in an upside-down tree structure.
  • The top of the "filesystem" is called the root.
    • The root is written as "/"
    • The root is often verbally called "slash"
  • Directories make up the "branches" of the tree.
  • Files make up the "leaves".
  • Refer to files and directories with "pathnames".
  • If a name begins with "/" it is a "full pathname" or "fully-qualified pathname".
    • When UNIX searches for the file, it begins at the root.
  • In full pathnames, directory names are separated by "/" characters.
  • The pwd command above showed a full pathname.
    • /home/visitors/kns is the full pathname to my home directory.
  • If a name does not begin with "/", it is a relative pathname. When UNIX searches for the file, it begins at the directory you are currently in.
    • At any given time, the directory you are in is called your current working directory.
  • You will usually use relative pathnames, working with files in your home directory or directories you create.
  • The previous example of making and then removing the directory tmp used a relative pathname.
  • Several commands are available to help you manage files.
  • The cp command copies files:

    
    	% ls
    	% cp /etc/motd motd
    	% ls
    	motd
    	% 
    
    

  • The mv command moves files:

    
    	% ls
    	motd
    	% mv motd message-of-the-day
    	% ls
    	message-of-the-day
    	% 
    
    

  • Both cp and mv expect the first argument to be the "source" to copy or move from and the last argument to be the "target" to copy or move to.
  • If the last argument is a directory name, the file will be named the same thing but in that directory.
  • In this case, you can have more than one source argument (you can cp or mv more than one file at a time if the target is a directory)

    
    	% ls
    	message-of-the-day
    	% mkdir tmp
    	% ls
    	message-of-the-day  tmp
    	% cp /etc/motd message-of-the-day tmp
    	% ls
    	message-of-the-day  tmp
    	% ls tmp
    	message-of-the-day  motd
    	% 
    
    

  • rm command removes files:

    
    	% ls
    	message-of-the-day  tmp
    	% rm message-of-the-day
    	% ls
    	tmp
    	% 
    
    

  • You need to use rmdir on directories, and they must be empty:

    
    	% ls
    	tmp
    	% rmdir tmp
    	rmdir: directory "tmp": Directory not empty
    	% ls tmp
    	message-of-the-day  motd
    	% rm tmp/message-of-the-day tmp/motd
    	% rmdir tmp
    	% 
    
    

  • You can look at what is inside a file with the cat command.
  • cat will send the whole file to your screen without stopping between screenfuls. cat winds up being more useful for other things.
  • A better command for viewing files is more. It will stop after one screenful with a prompt that looks like this:

    
    	--More--(81%)
    
    

  • Here you can type:
    • [space character] to see another screenful
    • [carriage return] to see one more line
    • "b" to go [b]ack one page for most versions of more
    • "q" to [q]uit showing you the file
  • The number is the percentage of the file you have seen so far.
  • Another command many sites have that does the same thing is less. But less is not available in all versions of UNIX.
  • There are two special directory names:
    • The directory you are currently in is called "." (pronounced "dot").
    • The directory immediately above is called ".." (pronounced "dot dot").
  • To illustrate:

    
    	% mkdir tmp
    	% pwd
    	/home/visitors/kns
    	% cd .
    	% pwd
    	/home/visitors/kns
    	% cd tmp
    	% pwd
    	/home/visitors/kns/tmp
    	% cd .
    	% pwd
    	/home/visitors/kns/tmp
    	% cd ..
    	% pwd
    	/home/visitors/kns
    	% 
    
    

Wildcards, Meta-Characters

  • Filenames are used often as command arguments.
  • shell wildcards make listing filenames easier
  • the character "?" will match any single character in a filename
    
    	% ls
    	1.txt   11.txt  2.txt   4.txt   6.txt   8.txt
    	10.txt  12.txt  3.txt   5.txt   7.txt   9.txt
    	% rm ?.txt
    	% ls
    	10.txt  11.txt  12.txt
    	% 
    
    
    
  • the character "*" will match any sequence of characters in a filename
    
    	% ls
    	a1.txt  a3.txt  a5.txt  b1.txt  b3.txt  b5.txt
    	a2.txt  a4.txt  a6.txt  b2.txt  b4.txt  b6.txt
    	% rm a*
    	% ls
    	b1.txt  b2.txt  b3.txt  b4.txt  b5.txt  b6.txt
    	% 
    
    
  • use the "\" character before a "?" or "*" if you don't want it to act as a wildcard
  • "?" and "*" are sometimes called meta-characters because the shell treats them (and a few others) as special cases
  • note in above example "*" does include matching a "." in a filename
    • in DOS you needed "*.*" to match any filename because DOS broke filenames up into the filename and extension
    • UNIX doesn't break up filenames that way, "." is not treated special in filenames
    • "*" will match any filename
  • one other meta-character is very useful at this stage
    • character "~" alone or as the only thing before a "/" expands out to your home directory
    • character "~" before a username will expand out to that user's home directory
  • if you want to change directory into my home directory you could do cd ~kns
  • if you have a directory named work you could move there with cd ~/work

File Attributes

  • use l switch to ls command to show most of the user-level information UNIX keeps about a file
    
    	% ls -l
    	total 2
    	-rw-r--r--   1 kns      visitors      1203 Mar  1 23:49 motd
    	% 
    
    
  • the name of the file appears on the far right
  • the date and time is when the file was last modified
  • 1203 is the size of the file in bytes
  • kns is the username of the file's owner
  • visitors is the group the file is in
  • the 1 in front of kns is the link count, that will get covered later
  • the group of ten characters at the beginning of the line is the mode of the file
  • the first character indicates the file's type
    • "-" means it is a plain file
    • "d" means it is a directory
    • "p" means it is a pipe
    • "l" means it is a symbolic link
    • "b" means it is a block special file
    • "c" means it is a character special file
  • the other nine characters indicate permission settings
  • UNIX has three basic operations it will check for a file
    • read : contents of the file can be read (looked at with cat, more, copied with cp)
    • write : contents of the file can be written
    • execute : file can be used as a command
  • UNIX also has three classes of users it will check these permissions for
    • owner of the file
    • users in the same group as the file
    • anyone else
  • in the mode "-rw-r--r--" above:
    • first dash means it is a plain file
    • next three characters are read/write/execute permissions for the owner of the file ("-" in one of those positions means that permission is denied, so the owner can not execute this file)
    • next three characters are read/write/execute permissions for users in the same group as the file
    • last three characters are read/write/execute permissions for everyone else
  • with the above mode
    • the owner of the file may read from or write to the file but not execute it as a command
    • users in the visitors group may read the file but not write to the file or execute it as a command
    • the same for anyone else
  • the command to change permissions is chmod
  • chmod lets you grant or deny permissions to each class of user
  • the simplest forms of chmod are:

      chmod c+p file [file ...]
      chmod c-p file [file ...]

  • the first form will grant (add) permissions, the second form will deny (subtract) permissions
  • for both forms c specifies the class
    • "u" for user (your) permissions
    • "g" for group permissions
    • "o" for "other" permissions (everyone else)
    • "a" for effecting all three classes at the same time
  • the p specifies the permission(s) to add or subtract
    • "r" for read permission
    • "w" for write permission
    • "x" for execute permission
    • can give more than one permission here
  • replace file with the name of the file you want to change the permissions on
  • [file ...] is a standard way of saying more filename(s) may be given
  • first example denies "other" read permissions:
    
    	% ls -l motd
    	-rw-r--r--   1 kns      visitors      1203 Mar  1 23:49 motd
    	% chmod o-r motd
    	% ls -l motd
    	-rw-r-----   1 kns      visitors      1203 Mar  1 23:49 motd
    	% 
    
    
  • to grant group write permission:
    
    	% chmod g+w motd
    	% ls -l motd
    	-rw-rw----   1 kns      visitors      1203 Mar  1 23:49 motd
    	% 
    
    
  • to grant "other" read and write permission:
    
    	% chmod o+rw motd
    	% ls -l motd
    	-rw-rw-rw-   1 kns      visitors      1203 Mar  1 23:49 motd
    	% 
    
    
  • to deny everyone write permission:
    
    	% chmod a-w motd
    	% ls -l motd
    	-r--r--r--   1 kns      visitors      1203 Mar  1 23:49 motd
    	% 
    
    
  • it can be useful at times to deny yourself write permission
    • it will prevent you from accidentally editing files you don't want changed
    • rm command will ask for confirmation before removing a file with write permission turned off
      
      	% rm motd
      	rm: motd: override protection 444 (yes/no)? no
      	% ls
      	motd
      	% 
      
      
  • permissions often get represented as octal digits
  • here the three groups of permissions (the characters rwxrwxrwx) get converted to three numbers, each number representing the permissions for one class
  • under this scheme "r" is worth 4, "w" is worth 2, and "x" is worth 1
  • add up the numbers representing the letters to get the number for this octal representation
  • permissions of "rw-r-----" would be 640
  • permissions of "r--r--r--" would be 444
  • permissions of "rwxrw-r--" would be 764
  • chmod will take an octal number for a mode setting as well
    
    	% ls -l motd
    	-r--------   1 kns      visitors      1203 Mar  1 23:49 motd
    	% chmod 644 motd
    	% ls -l motd
    	-rw-r--r--   1 kns      visitors      1203 Mar  1 23:49 motd
    	% 
    
    
  • unless you know octal number schemes fairly well it is best to stick with the other method of using chmod
  • permissions have slightly different meanings for directories
  • read permission on a directory means you can get a listing of the files with ls
  • write permission on a directory means you may make new files there or remove files in the directory
  • execute permission means you can access files inside the directory but only if you know their name, you can't get a listing with ls
  • usually unless you are in a special group for sharing information you will set group permissions to be the same as "other" for your files and directories
  • sys-admin picks a group to put you in when your account is made
  • can also add you to "supplemental" groups if necessary for you to share files with others
  • command groups will show you what groups you are in
    
    	% groups
    	visitors lab
    	% 
    
    
  • here primary group is "visitors" and supplemental group is "inven"
  • command chgrp will change a file to a different group
    
    	% ls -l motd
    	-rw-r--r--   1 kns      visitors      1203 Mar  1 23:49 motd
    	% chgrp lab motd
    	% ls -l motd
    	-rw-r--r--   1 kns      lab           1203 Mar  1 23:49 motd
    	% 
    
    
  • now permissions could be set so only people in group "lab" may access "motd"
  • sys-admin must set up groups
  • quickly mentioned modification date of file earlier
  • for some things modification time of file is very important
  • when using mv modification date will not be changed
  • by default when using cp modification date will be the present time for the target file, source file will be unchanged
  • can have cp set modification date of target to be the same as the source with "p" option
    
    	% date
    	Tue Mar  7 23:26:03 EST 2000
    	% ls -l motd
    	-rw-r--r--   1 kns      lab         1203 Mar  1 23:49 motd
    	% cp motd motd_1
    	% ls -l
    	total 8
    	-rw-r--r--   1 kns      lab         1203 Mar  1 23:49 motd
    	-rw-------   1 kns      visitors    1203 Mar  7 23:26 motd_1
    	% cp -p motd motd_2
    	% ls -l
    	total 12
    	-rw-r--r--   1 kns      lab         1203 Mar  1 23:49 motd
    	-rw-------   1 kns      visitors    1203 Mar  7 23:26 motd_1
    	-rw-r--r--   1 kns      lab         1203 Mar  1 23:49 motd_2
    	% 
    
    
  • touch command will set last modification time to the current time if file exists, will create the file if it does not exist
  • file created by touch will be zero length
    
    	% date
    	Tue Mar  7 23:29:11 EST 2000
    	% ls -l
    	total 4
    	-rw-r--r--   1 kns      lab         1203 Mar  1 23:49 motd
    	% touch motd foo
    	% ls -l
    	total 4
    	-rw-------   1 kns      visitors       0 Mar  7 23:29 foo
    	-rw-r--r--   1 kns      lab         1203 Mar  7 23:29 motd
    	%