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.
/usr/local/gopher/Courses
cp file1 file2will take file1 and create a new file called file2 that contains the same information as file1, and is in the same directory.
cp file1 Textswill copy file1 into the subdirectory called Texts, keeping the same name: in other words, it will create a new file called Texts/file1.
cp file1 Texts/file2will create a new file called Texts/file2 that contains the same information as file1.
mv file1 file2will copy file1 into file2 and *delete file1* -- the practical effect is that you've renamed file1 into file2.
mv file1 Textswill copy file1 into Texts/file1 and then delete the original file1.
mv file1 Texts/file2will simultaneously move the file into the directory and rename it to file2.
mkdir Textsyou'll create a directory called /home1/jqpublic/Texts.
When you use "ls -l" ("ls" for list files, "-l" for "Long format"), the far left will include a pattern of letters like this:
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 --
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.
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 change file permissions only for your own files.
head -20 file1will display the top twenty lines. "head" is useful for taking a quick look at a long text file.
grep light parlost.01You 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:
"sort" will put the contents of a file into alphabetical order. For instance, you can type
sort file1and get a list of all the lines in file1 sorted in alphabetical order. The output will appear on the screen.
"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 secondand so on, you can look at just the numbers by typing
cut -c1-4 file1The "-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 file1will 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 Ohyou 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" " file1sed -- 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/" filenamewill 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//" filenamewill replace "quite" with "" -- in other words, remove it. You can also use sed to delete entire lines:
sed "/light/d" parlost.01will delete every entire line of the file parlost.01 that contains the word "light."
ls > dirfilewill 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.outwill 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 > file3will 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."
ls | sort -rTo see just the first five matches of the word "light" in a group of files, type
grep "light" * | head -5If the search for the word "light" produces too many matches, you can see them a screen at a time by typing
grep "light" * | moreThere'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
"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.