|
||||||
|
|
A brief guide to using UNIX on UCS computers at Florida State University. If you need additional help, contact the Technology ServicesHelpdesk. Files and Directory PathsThe Unix file system has a hierarchical, tree-shaped structure. Files are organized into directories, each of which can contain lower-level directories (subdirectories). All directories are branches that can ultimately be traced back to a single directory denoted by / and called "root", as shown in the figure below. Every file or directory has a path name that shows its location in the file system. In general, any place an ordinary file name can be used as an argument, a path name that ends in a file can also be used. A full or absolute path name shows how to traverse the tree to reach a file or directory by starting from the root of the file system. It starts with the symbol / and lists all directories between the root directory and the target file or directory, separated by slashes. In the figure below, /u is the path to the subdirectory u of the root directory. The full path to the directory martin is /u/martin, and the path to the file file1 in that directory is /u/martin/file1. A user always has a current working directory. Instead of specifying an absolute path name, it is often more convenient to use a path that starts at the current directory. Any file or directory in the current directory can be referenced by its name alone. A file in another directory can be referenced by providing an appropriate path name. If file1 is contained in the subdirectory subdir of the current directory, it can be referenced from the current directory as subdir/file1. The current working directory can be referred to as . ("dot"), and its parent as .. ("dot dot"). Thus, relative path names can also be used to reference parent directories and their subdirectories. The file or directory ../name is parallel in the tree structure to the current directory. The directory ../.. is the parent of the current directory's parent. In the figure below the relative path from martin to combs is ../combs. You are assigned a home directory when your account is created, and are iitially placed there when you first log in. Directories such as martin and combs in the figure above are users' home directories. The shell variable HOME refers to the home directory. For example, the user martin can substitute $HOME for /u/martin. For martin, the path name $HOME/file1 is equivalent to the full path name /u/martin/file1. Under the C shell and Korn shell (but not the Bourne shell) the character ~ can also be used to reference a home directory. The user martin can refer to /u/martin with ~, and other users can refer to that directory with ~martin.
Changing the Current Directory You can move to other directories by using the command cd (short for "change directory"): % cd pathname Any valid absolute or relative path name can be used as an argument to the cd command. If no path name is specified, cd will move you to your home directory. You must have "execute" permission for a directory in order to cd into it. Some examples: % cd bin Moves to the subdirectory bin of the current directory. % cd /bin Moves to subdirectory bin of the root directory. % cd Moves to the user's home directory. % cd .. Moves to parent directory of the current directory.A user can display the path to his current directory with the command % pwd (short for "print working directory").
File and directory names can be any length between one and 255 characters. All characters except / are legal (filenames can even have nonprinting characters), but most characters other than letters, numbers, and periods should be used with care. Uppercase and lowercase characters are distinct. The following are examples of legal directory or file names: program.f program.F Memo section2 file.12.d chap3+4 item1-10 ref:list.txtWhen a filename with unusual characters is used in a shell command, it may be necessary to enclose it in quotation marks so that any special meanings the characters have will be ignored. See the section on special characters. Wildcard Characters in File Lists Many commands will accept file lists as well as single file names. Wildcard characters can be used to match arbitrary characters in filenames: the character ? will match any single character, and * will match any set of characters. A set of characters that any single character can match can be enclosed in square brackets. Some examples of file lists specified with wildcards: Examples of Wildcard Matches:
Managing Files & DirectoriesCreating and Deleting Directories A new directory can be created with the command % mkdir path where path is any valid absolute or relative path name, and no file or directory with that path name already exists. For example, a new subdirectory data of the current directory can be created with the command % mkdir data You must have "write" permission for the parent directory in order to create a new directory under it. A directory can be deleted with % rmdir name A directory must be empty of all files and subdirectories before it can be deleted with rmdir. However, the command % rm -r directory will delete a directory and its contents (including files and subdirectories).
The ls command lists the names of all files and subdirectories in a specified directory. The form of the command is % ls [options] directory
If no directory is specified, ls lists the names of files and directories in the current directory. The directory name can be either the full or relative path name. If no options are specified, the ls command does not list hidden files (those whose names begin with a dot). Wildcard characters can be used with ls. The command % ls a*
shows all files with names that begin with the letter 'a'.
The ls command has a number of options. For example, -x or -C will provide multicolumn listings. The -a option will list all files, including those whose names begin with a dot, and -l produces a long listing with information about the files. The option -F displays a / to indicate subdirectories and a * to indicate files that can be executed. Various options can be combined; in this example, all files (including "dot" files) are listed in the long format, with directories and executable files marked: % ls -alF total 62 drwxr-xr-x 6 martin fsu000 640 Jan 16 10:31 ./ drwxr-xr-x 78 root root 2528 Jan 11 17:25 ../ -rwxr-xr-x 1 martin fsu000 1154 Jan 16 10:05 .cshrc* -rw-r----- 1 martin fsu000 241 Jan 16 10:05 .history -rwxr-xr-x 1 martin fsu000 580 Jan 16 10:05 .login* -rwxr-xr-x 1 martin fsu000 604 Jan 16 10:05 .profile* drwx------ 2 martin fsu000 288 Sep 7 14:23 chuck/ -rw------- 1 martin fsu000 1075 Feb 28 1989 mbox -rw------- 1 martin fsu000 3372 Aug 1 14:08 plot.fThe output from ls -l shows the following information about each file: whether the file is a directory (directories are denoted with a d), the file's read, write, and execute permissions for the owner, the owner's group, and other users, the link count, the owner's login name and group, the file length in bytes, its date and time of last modification, and, finally, the file name. The file . is the current directory, and the file .. is its parent.
File & Directories Permissions Every file or directory has a set of access permissions for three classes of users:
For a file, the permissions do this:
For a directory, the permissions do this:
File permissions can be added with the chmod command: % chmod usertypes+accesstypes files A - can be used in place of the + if permissions are to be removed. Another way to specify the permissions is to assign the octal values that are represented by the three bits that turn the permission types on and off. Read permission has the value 4, write permission 2, and execute permission 1. The chmod command in the example above could be replaced with % chmod 755 quad since the owner has read/write/execute permission (7=4+2+1) and group and others have read/execute permission (5=4+1). In this example group members and other users are given read and execute access to the program quad: % ls -l quad -rwx------ 1 martin fsu000 335352 Aug 10 09:56 quad % chmod og+rx quad % ls -l quad -rwxr-xr-x 1 martin fsu000 335352 Aug 10 09:56 quadYou can change the default file permissions for files you create with the umask command, which turns off the permissions specified. For example, % umask 022 turns off only write permission for users other than the owner. The umask command can be placed in your .profile or .cshrc file (see the section on Unix shells). Displaying the Contents of Files There are several commands for displaying the contents of files. The cat command produces continuous output of the file or files named: % cat files Each of these commands will display the contents of the file a page at a time: % more file Pressing the space bar scrolls new pages under more, while a carriage return scrolls pages under pg. The head command displays the first part of a file and the tail command the last part. By default, ten lines are displayed by head and tail. You can copy (duplicate) a file with the command % cp filename target where target is the new file name. If target is an existing directory, the file will be copied there with the same file name. You can duplicate an entire directory (including subdirectories, etc.) with % cp -r directory target Files can be moved (renamed) with % mv filename target If the target for cp or mv is an existing directory, the file will be placed in that directory with the same filename. You can use Wildcard characters when listing file names. The command % cp * directory will copy all files in the current directory to the target directory (except directory files and files whose names start with .). Wildcard characters should be used only in lists of files to be copied or moved, not in target filenames or paths. You must have "write" permission for a directory in order to copy or move a file into it. Concatenating and Appending Files The cat command (short for "concatenate") produces continuous output of the file or files named. Two or more files can be concatenated into a single file bigfile with % cat file1 file2 > bigfile A file file1 can be appended to file2 with
% cat file1 >> file2 The symbols > and >> are explained in the section on redirecting output. A file can be removed from a directory with % rm filename The command % rm -i files will prompt for confirmation before deleting each file. The -i option provides a convenient way to delete files whose names contain unusual characters, since files can be deleted selectively without typing the names. You need "write" permission for a file in order to remove it. The grep utility searches files for "regular expressions" (patterns of characters) and types any lines that contain them. The command % grep [options] pattern file
will search the file (or list of files) for the pattern. The option -v can be used to search for lines that do not contain a pattern, and -i will cause grep to ignore uppercase and lowercase distinctions. The find utility can be used to locate a file with a particular name. The command % find . -name filename -print will list all files with the name filename in the directory tree starting at the current working directory. The tar utility can be used to archive files or entire directory trees. "Tar" files provide a convenient way to archive sets of files that are to be moved to other computers. To tar a file or group of files, first change to the directory where the files are located and use the command: % tar -cf tarfile filenames The file tarfile will contain all the archived files. An entire directory can also be archived and the tar file placed in the parent directory (..) with % tar -cf ../tarfile . The file tarfile will contain the entire directory tree starting in the current directory. To obtain a listing of the files in tarfile: % tar -tf tarfile To extract a file from tarfile: % tar -xf tarfile filename where name is the exact path, as shown in the listing. To extract all files from tarfile use the command % tar -xf tarfile If the tar file contains a directory structure, that directory structure will be placed in the current directory as a subdirectory. The most commonly used utilitiy for compressing files for archiving or transfer is compress. A file can be compressed with the command: % compress filename This creates a file called filename.Z, removing >filename. The file filename.Z can then be copied to another disk or transferred via ftp (in binary mode) to another mass storage system. Use of ftp is described below. To retrieve information from a compressed file, use the command: % uncompress filename.Z This creates a file filename, removing file filename.Z. The commands pack and unpack also compress files, but the size reduction from pack may not be as great as that of compress. Files can be transferred between computers on the Internet with ftp, the user interface to the ARPANET standard File Transfer Protocol that allows users to transfer files to and from any site on the Internet. On the local host, the user executes the "ftp client" and specifies the remote host. The form of the command to start ftp is % ftp remote_host where the remote_host can be identified by an IP address, an Internet domain name, or an acceptable alias. In most cases an ftp session can be initiated from either the local host (e.g., Garnet or mailer) or the other host. The remote host account name and password must be entered before any transfers can occur. The basic ftp commands are listed below. There are man pages available for ftp, and help is also available from within the program.
| |||||||||||||||||||||||||||||||||||||||||||||||||
| |||