Some Essential Unix Commands GETTING AROUND DIRECTORIES & FILES ---------------------------------- Like DOS, Unix organizes information by putting *files* in *directories*. Breaking information down into directories helps to make a large body of information more manageable. A directory is like a folder, which can contain either information (a file), or more folders (called subdirectories). Each directory can contain any number of files or subdirectories, and every file and every directory has a name, made up of letters and numbers. The full name of a file includes its "path" -- in other words, the complete directory structure, with the directories and subdirectories separated by slashes (forward slashes, "/", rather than the backslash, "\", of DOS). A file called "file1" in the directory /home4/jqpublic would have the full path /home4/jqpublic/file1. The "working directory" is the directory you are in at the moment. When you are in a working directory such as /home4/jqublic, most commands by default apply only to that directory. To work with other files, it's often easiest to change into another directory (see "cd," below). pwd Print Working Directory. At any time you can find out the current working directory by typing "pwd" on its own. ls Display a list of files and directories. Typing just "ls" will give you a list of all the files and subdirectories in the current directory. (Technically it will show you only the "visible" files: those whose names begin with a character other than a period.) You can also get a list of files in another directory by specifying the directory after ls: ls /usr/local/gopher/Courses A few flags might be useful: ls -a Show all files, including the invisible ones (those whose names begin with a period). ls -g Show all files and their protection levels. ls -l Use the Long format, including date, file owners, &c. ls | more will provide small portions of the folder, prompting you for more (hit space bar for more). cd Change Directory. In Unix, each account has a *location,* and therefore moving from one location to another means that you need to know where you are and what directories are adjacent to you. Your own position is usually posted in the unix prompt itself. Thus, if your prompt says, for example, (dept)/home1/jlynch it means that you are in the (dept) machine, in the /home1 directory, and, within that /home1 directory, you are in the "jlynch" directory. With "cd" you can change the current directory to any directory on the system. The problem is knowing what directories are available to move into or out of. Therefore, you should use the "ls" (list) command to see what directories exist in the place where you are. So, "cd Teaching" would take you into a Directory called "Teaching", provided that one existed where you were in your unix account. "cd .." will take you *OUT* one directory. So, if you were in (dept)/home1/jlynch, then typing "cd .." would take you out one to (dept)/home1. You can even move multiple spaces. If your present location was (dept)/home2, by typing "cd ../home1," you would move out one to the (dept) location, and then into the "home1" directory. So, if you were at (dept)/home1/jlynch, you could, by typing "cd ../../home2," go out two directories and into the home2 directory. THIS IS EXTREMELY IMPORTANT BECAUSE IT WILL DICTATE HOW YOU MOVE OR COPY FILES, SINCE YOU MUST KNOW WHERE YOU WANT TO PUT THE FILE YOU'RE MOVING OR COPYING. (SEE BELOW FOR HOW TO MOVE OR COPY FILES) "cd" alone will change back to your home directory. cp Copy a file. You can copy a file to another name or to another directory without deleting the original file. The "source" file comes first, followed by the "target" file: in other words, cp file1 file2 will take file1 and create a new file called file2 that contains the same information as file1, and is in the same directory. cp file1 Texts will copy file1 into the subdirectory called Texts, keeping the same name: in other words, it will create a new file called Texts/file1. You can also copy into another directory with a different name: cp file1 Texts/file2 will create a new file called Texts/file2 that contains the same information as file1. mv Move or rename a file. mv behaves much like cp, except that it *deletes the original file*. Like cp, you specify the source first, the target second. mv file1 file2 will copy file1 into file2 and *delete file1* -- the practical effect is that you've renamed file1 into file2. mv file1 Texts will copy file1 into Texts/file1 and then delete the original file1. mv file1 Texts/file2 will simultaneously move the file into the directory and rename it to file2. rm Remove a file. "rm file1" will remove the file called file1. You can also specify several files at once: "rm file1 file2 file3" will remove all three. You can also use wildcards, the most common of which is * -- the asterisk means "everything beginning with." You can type "rm file*" to remove every file beginning with the letters file. "rm *" would delete *everything* in a directory. By default, rm will ask if you're sure about each deletion. The -f flag overrides the question and deletes all the files without asking whether you're sure. Use it with great caution; there's no way to get the files back. rm works only with files, not directories. To remove a directory, use rmdir (below). rmdir Remove a Directory. Works like rm, except it removes directories instead of files. Directories must be empty before they can be removed. mkdir Make a Directory. Creates a new directory, a subdirectory of the current directory. If you're in /home1/jqpublic and type mkdir Texts you'll create a directory called /home1/jqpublic/Texts. FILE PERMISSIONS ---------------- Unix is a multi-user operating system, meaning many people are on the same system at once. To ensure security, some files are restricted: only certain people can read or write them. This is known as file permissions. When you use "ls -l" ("ls" for list files, "-l" for "Long format"), the far left will include a pattern of letters like this: -rwxr-xr-- The ten positions here indicate the file permissios. The first (leftmost) position can be either "d" or "-" -- "d" means the entry is a directory, while "-" means it's a file. The remaining positions come in three groups of three. Each group consists of three positions: "r" or "-"; "w" or "-"; "x" or "-." "r" means "Read permission"; "w" means "Write permission"; "x" means "execute permission." The most important are Read and Write permissions: Read permission is what it sounds like: it means the User, Group, or Other can look at the contents of the file. Write permission means the User, Group, or Other can change or delete the file. An "r" means read permission is enabled, while a "-" means it isn't; "w" means write permission is enabled, and "-" means it isn't. Each group of three, then, shows read, write, and execute permission. The three groups are: User, Group, and Other. The User is the person who created (or owns) the file; the Group is a collection of people (defined on the system) who have similar permissions; Other is anyone else on the system. The example above, therefore -- -rwxr-xr-- -- can be broken down this way: The first position is a "-" instead of a "d," so it's a file, not a directory. The User's permission -- the next three characters -- is "rwx," meaning the person who created the file can read, write, and execute the file. The Group permission is "r-x," meaning the Group can read and execute, but not write. The Other permission is "r--," meaning Others can read but not write or execute. You'll want to set permissions carefully. By default, most files you create can be read and written by you alone. If you want others to have access to the files, you'll have to set the file permissions to allow other people -- either those in your group or everyone -- to read or write the files. Be careful not to allow others to tamper with your files. Your personal mail, for instance, should give read and write permission only to you: -rw-------. If you want someone else to be able to see the contents of your file, give read permission to either the group or to other, but keep write permission only for yourself. If you give write permission to other, anyone on the system can change or delete your files. To change file permissions, use the "chmod" command. chmod Change File Mode. To change the file permissions, use "chmod." For each file, there are three kinds of permission and three kinds of users (see "File Permissions"): the User, the Group, and Other can either Read, Write, or Execute. Use the letters u, g, o; r, w, x; and + and - to assign permissions. To give read permission to the group, for instance, type chmod g+r filename "g+r" means "Group Read." "chmod g-r filename" would turn off group read permission. "g+w" means "Group Write"; "o+r" means "Other Read." You can also assign permissions for all three kinds of users with "a" (for "All"): "chmod a+r filename" means the User, Group, and Other can read; "chmod a-w filename" means no one can write to it (including the owner). You can change file permissions only for your own files. LOOKING AT FILES ---------------- You can always use a text editor (such as pico, emacs, or vi) to look at a file. Some other methods are sometimes useful: cat Display an entire file. "cat" -- it stands for "concatenate" for obscure reasons -- will display an entire file on the screen. "cat file1" will display the entire thing. You can also display several files, as with "cat file1 file2" or "cat *". more Display an entire file screen by screen. "more file1" will display an entire file, one screen at a time. You can then press the space bar to go forward a screen, or "q" to quit. head Display the top of a file. "head file1" will display the top ten lines of file1. You can also choose exactly how many lines to display by specifying the number after a hyphen: head -20 file1 will display the top twenty lines. "head" is useful for taking a quick look at a long text file. tail Display the bottom of a file. Works exactly like head, above, but shows the bottom of a file. OTHER FILE OPERATIONS --------------------- grep Search for a String. grep -- it stands for "Get Regular Expression and Print" -- searches through a file or group of files for a string. To find every occurrence of the letters "light" in the file parlost.01, type grep light parlost.01 You can also search every file in a directory: grep light * will go through every file in the current directory and look for the letters "light." To search for more than one word, put the string in quotation marks: grep "he said" * Some useful flags: grep -i Ignore case -- "grep -i light *" will search for "light," "Light," "LIGHT," whatever. By default, grep is case-sensitive. grep -n Display the line number next to each match. grep -c Count the number of occurrences without displaying each one. grep -v The opposite of normal grep: display every line that does *not* contain the string. grep -w Search for whole words only. If you type "grep light," you'll see every ocurrence of "enlightened," "lightning," and so on. The -w flag limits the search to the whole word "light" only. sort Sort a file. "sort" will put the contents of a file into alphabetical order. For instance, you can type sort file1 and get a list of all the lines in file1 sorted in alphabetical order. The output will appear on the screen. A few flags are useful: sort -b Ignore leading tabs and spaces. sort -f Sort upper- and lowercase together. sort -r Sort in reverse order. sort -u Skip duplicates. If two lines are identical, "sort -u" will display only one of them. cut Look at part of each line. "cut" lets you select just part of the information from each line of a file. If, for instance, you have a file called "file1" with data in this format: 0001 This is the first line 0002 This is the second and so on, you can look at just the numbers by typing cut -c1-4 file1 The "-c" flag means "columns"; it will display the first four columns of each line of the file. You can also look at everything but the line numbers: cut -c6-100 file1 will display the sixth through one hundredth column (if the line is less than a hundred characters -- and most will be -- you'll see up to the end of the line). You can also use cut to look at fields instead of columns: for instance, if a file looks like this: curran:Stuart Curran jlynch:Jack Lynch afilreis:Al Filreis loh:Lucy Oh you can use cut to find the full name of each person, even though it's not always in the same place on each line. Type cut -f2 -d: file1 "-f2" means "the second field"; "-d:" means the delimiter (the character that separates the fields) is a colon. To use a space as a delimiter, put it in quotations: cut -f2 -d" " file1 sed Stream Editor. "sed" is a sophisticated tool that allows you to perform large-scale search-and-replace operations on a file. There are many possibilities; the most important are "s" ("Substitute") and "d" ("Delete"). sed "s/colour/color/" filename will take the entire file called "filename" and replace every occurrence of the pattern "colour" with "color", then display the output to the screen. You can also use "s" to remove a pattern of letters: sed "s/quite//" filename will replace "quite" with "" -- in other words, remove it. You can also use sed to delete entire lines: sed "/light/d" parlost.01 will delete every entire line of the file parlost.01 that contains the word "light." REDIRECTION & PIPING -------------------- By default, Unix sends the output of all these commands to the screen. But this isn't necessary. Unix lets you take the output of one command and either save it in a file, or turn it into the input of another command, allowing you to chain several commands together. You can do two things with output: (1) redirect it into a file; and (2) pipe it into another command. REDIRECTION The ">" (greater than) symbol lets you take the output of a command and save it to a file. For instance, "ls" alone will display the contents of the directory on the screen. Typing ls > dirfile will save that output to a file called "dirfile" (it won't appear on the screen). Programs such as cut, sort, grep, and sed can produce very long outputs; you might want to save them to a file. For instance, to translate "colour" to "color" you can use sed, but the output will scroll past you too quickly to see. Use redirection to save it to a file: sed "s/colour/color/" file1 > file1.out will produce a new file called "file1.out" that has changed the spelling of "colour" to "color" in every line. You can use redirection to join two files together with "cat": cat file1 file2 > file3 will take file1 and file2 and combine them into a new file called file3. WARNING: "ls > dirfile" will overwrite "dirfile" if it already exists. If you want to add the contents of the directory to the end of a pre-existing dirfile, type ls >> dirfile -- the ">>" means "add to the end." PIPING The "|" symbol lets you take the output of a command and send it to the input of another command. For instance, you take the output of the "ls" command and sort it in reverse order: ls | sort -r To see just the first five matches of the word "light" in a group of files, type grep "light" * | head -5 If the search for the word "light" produces too many matches, you can see them a screen at a time by typing grep "light" * | more There's no limit on the size of the chain you can put together. To see all the files in a directory created in January, sort them by name, and save them to a file, you can type ls -l | grep " Jan " | cut -c55-100 | sort > janfiles GETTING HELP ------------ man Display the manual pages for a command. "man" is the Unix "help" -- type, for instance, "man grep" and you'll get the complete instruction manual for the grep command. apropos Search for commands that relate to a particular topic. "apropos directory" will display every command that includes the word "directory" in its brief description. You can then use "man" to get a fuller description of the command.