Many commands have several options that can be selected. A command option is specified by giving the command name, followed by a minus sign, followed by the option. For example, some of the possible options of the ls command, described later, include ls -a and ls -l. Options can be combined, either by typing them separately, as in ls -a -l or in certain situations, together, as in ls -al.
To view the contents of your current directory, type ls. You probably won't see anything yet if you haven't created any files, but try typing ls -a to list all files. Now you will see a number of files, each of them beginning with a dot. Any file whose name begins with a dot is considered a hidden file and will only appear in a directory listing when you request all files to be shown.
You might notice two unusual entries, . and .. in this list. The . refers to your current directory and the .. refers to the parent of your current directory. You could type ls /home/users to see the list of files in the parent of your home directory, but a simpler way to do this is to type ls .. from your home directory.
In UNIX, every program, text document, data file, and so on, is saved as a file. Even directories are considered files, themselves. When you use ls, you will see any (non-hidden) files in the directory, including any sub-directories within it. For example, type ls /home/jer/robocup to see the robocup sub-directory in your instructor's account. Unfortunately, this listing does not distinguish between ordinary files and directories. Try ls -F /home/jer/robocup. This time, all of the sub-directories appear with a slash (/) at the end. You will notice that some of the files appear with an asterisk (*) at the end. Any such file is an executable, which means that you can run it, simply by typing its name. If you like this format of file listings, then once you are familiar with one of the editors, you can make this a default by adding the following line to the end of your .cshrc file:
alias ls 'ls -F'Regardless of where you are currently located in the directory tree, you can refer to your home directory by $HOME or the ~ character. You can also refer to someone else's home directory by ~userid. For example, another way to see the files of your instructor's robocup sub-directory would be to type ls ~jer/robocup.
Another useful option of the ls command is the long format listing, obtained with ls -l. This provides you with the file size, ownership, last modification date, and permissions field of each file. See man ls for more details.
To change directories, use the cd command. For example, to go into your instructor's robocup sub-directory, type cd ~jer/robocup. Typing cd by itself will always put you back into your home directory. If you want to know where you are in the directory tree, you can type pwd for "print working directory."
The remaining commands in this section are listed by function with no explanation. The first two commands are explicitly for directories. The remaining commands work on individual files, groups of files, or entire directories. For more details, consult the appropriate man page.
mkdir | make a new directory |
rmdir | remove a directory |
rm | remove file(s) |
mv | move (rename) file(s) |
cp | copy file(s) |
chmod | change permission of file(s) |
A problem with the cat command is that if the file contents do not fit in one screen, then the display will quickly scroll and you will not have a chance to see the beginning of the file. The page, more, and less commands display one page of a file at a time, and offer additional features, such as page skipping and scrolling backwards (less only).
To just view the first few lines or last few lines of a file, you can use the head and tail commands. Both of these commands take an optional argument specifying the number of lines to show, for example, tail -15 file1 will display the last 15 lines of file1.
Sometimes, when you are writing programs or documentation, you may need to determine what has changed from one version to another. You can use the diff command to list the differences between two files. Another useful command is grep, which locates occurrences of any character, word, or string of text in a file. To find out more about these commands, see the appropriate man pages.
You may want to print program listings, data files, and documentation for your own use during the course, and you will certainly need to do so when an assignment requires you to submit a hardcopy. The command to send a file to the printer is called lpr. Again, there are many options for this command, which you can learn about by typing man lpr.
Another important operator is the pipe (|). A pipe allows you to pass the output of one command to the input of a second command. For example, you could type diff file1 file2 | more to pass the output of the diff command through the more viewer, so that the output doesn't scroll off the display.
vi ~jer/unix/vitutoror emacs by typing:
emacsand then selecting the tutorial from the help menu.
Suppose you are writing a program to print a message to the user, and have decided to split the program into two source files. Your main source file might be called "prog.c" and the other source file might be called "sub.c". Additionally, let us suppose that these two source files need to share a data structure definition which is contained in the file "incl.h".
To compile your program, you may simply type:
gcc prog.c sub.cwhich will create an executable program named "a.out." If you prefer, you may specify the name of the executable, for instance, "pgm", as follows:
gcc -o pgm prog.c greet.cIf you are re-compiling your program frequently, this method is inefficient, especially if you are only making changes to one source file at a time. Instead, you should compile the program as follows:
gcc -c prog.c gcc -c sub.c gcc -o pgm prog.o sub.oThe first two lines create object files "prog.o" and "sub.o" and the third line links the objects together into an executable. If you were then to make a change to the "sub.c" file, you could re-compile your program by just typing the last two lines.
This all might seem a little silly for the example above, but if you had ten source files instead of two, the latter method would save you a lot of time. Actually, the entire compilation process can be automated by using a makefile, for example:
# Any line beginning with a `#' sign is a comment and will be # ignored by the "make" command. To generate the executable # programs, simply type "make". # This makefile says that pgm depends on two files prog.o and # sub.o, and that they in turn depend on their corresponding # source files (prog.c and sub.c), in addition to the common header # file, incl.h: # Note that each of the gcc directives MUST be preceded by a # tab character (not a number of spaces) for the make to work. pgm: prog.o sub.o gcc -o pgm prog.o sub.o prog.o: incl.h prog.c gcc -c prog.c sub.o: incl.h sub.c gcc -c sub.c