UNIX / LINUX Tools and Commands Working with Directories When You Log in on One of the Machines, You Will Be Placed in Your Home Directory
Total Page:16
File Type:pdf, Size:1020Kb
UNIX / LINUX Tools and Commands Working with Directories When you log in on one of the machines, you will be placed in your home directory. The directory structure in UNIX is a hierarchical tree structure. The highest level of this structure is the / (root). To determine the current directory, type pwd. This command stands for print working directory. This is your home directory. You will always be placed here when you log in. To determine what files are in this directory, type ls or ls -al to get a long listing of all the files in the directory. The a option stands for all and the l option stands for long. Files that begin with a dot, such as .profile or .login, are considered hidden files. They are not shown in a normal ls listing. The first field of a long directory listing tells you what kind of file this is, and the read, write, and execute permissions for that file. If the first character is a d, this is a directory. A hyphen as the first entry in this field signifies an ordinary file. You shouldn't have to worry about other types of files at this point. The rest of this field is read, write, and execute permissions for the owner, the group, and others, respectively. An r signifies read privileges for that person, a w signifies write privileges, and an x signifies execute privileges. The only other fields that you should be concerned with right now are the last five. These are the filesize, the date of the last modification to the file, and the name of the file, respectively. To move to the root directory, type cd /. Do a ls -l listing to get an idea of the directory structure coming down from the root. There should be a bin, etc, usr, dev, and tmp on almost all UNIX systems. Let's try changing directories to bin, with the command cd bin. There are two ways to change directories. One method is using a relative path name. This will change your working directory relative to your current directory. The cd bin that you just typed is an example of this. The other form is using an absolute path name. An example of this would be cd /bin. Here you are changing your working directory relative to the root directory. Creating directories is performed with the mkdir command. To return to your home directory, type cd with no arguments. Type pwd again just to make sure that you are back. Now create a test directory by typing mkdir test. Do a ls -l listing to be sure that this created the directory. Now cd test to put yourself into the test directory. If you do a ls -al, you will notice that there are two files in this directory. Every directory has the files . and .. when they are first created. The . is that directory and the .. is the parent directory. This means that if you would like to move up one directory, you can type cd .. to bring you back to your HOME directory. Now the test directory can be removed by typing rmdir test. Working with Files This section covers copying files, moving files, and deleting unwanted files. To copy files, use cp [source] [destination]. An example would be cp .profile .profile2. This would put a copy of .profile in .profile2, without changing .profile. If you wanted to have the .profile2 removed after copying it, you would use mv .profile2 .profile. This would copy .profile2 into .profile and remove .profile2 when finished -- mv is similar to command ren in DOS. If you just want to remove a file, that can be done with rm [filename], for example, rm a.out. This will remove the file a.out, if you have write permissions to that directory and file. If you are using the common desktop environment you can drag the files to where you want them by selecting them and dragging them to the destination folder. You can accomplish this by opening two windows with the application manager. EDITING At some point in time, you will probably want to create or edit either text files or source code files. To do this you will need to use an editor. The two most popular editors for the UNIX system are emacs and vi. Vi is typically standard on all UNIX systems and emacs is installed on almost all systems. Both are installed on all of our systems. Emacs To invoke emacs, just type emacs [filename], and the editor will load in that file, or create a new file if it does not already exist. To learn how to use emacs, just type emacs, and then when emacs loads up, press Ctrl-h then t. This will load up the interactive tutorial, which should fully explain to you how emacs works. Anytime that you wish to leave emacs, just type Ctrl-x Ctrl-c, which means press x while holding down the control key, and then press c while holding down the control key. This convention is used throughout the emacs manual, which can be found on the shelf in the lab or online Gnu Emacs Manual Vi Vi is invoked by typing vi [filename]. The screen will clear shortly and the vi working screen will show up. Vi works in two modes, command mode and text entry mode. You will start in command mode. You will need to type certain commands to enter text entry mode. If you type i, this will allow you to insert text starting before the current cursor position. To return to command mode, press the escape key Esc. Other useful commands are o (opens a new line below the cursor), O (opens a new line above the cursor), a (append after the cursor location), A (append at the end of the line), and dd (delete the current line). These should be enough to get you started. Once you are ready to leave vi, type :wq while in the command mode (You can also use Shift-ZZ). This will save your file and exit. If you do not wish to save the file, type :q! in command mode and you will exit vi without saving any changes that you have made. This is just a very tiny overview of vi. If you would like to learn more about it, we have a few books in the lab or online Vi Documentation HELP FACILITIES There are many ways that you can obtain help with any problems or questions that you may run into. There are manuals both online and offline. First this section will discuss the many online help facilities and then it will determine which offline manuals may be best suited to answer your questions. Online Help The most frequently used online help is the man command. To use this to find information about, say the ls command, type man ls. If you would like to find more information about the man command itself, you may type man man. The Unix stations have many added online help features. One is the ability to type man -k [keyword]. This will find all the commands relating to that keyword. An example would be man -k directory. The response will be a listing of the commands that refer to directories. This is useful if you want to do something, but do not know what command to use. You can then man the command to find out more information about it. Two more useful commands on the Unix stations are whatis and whereis. Typing whatis [command] will look up the command, and return the header from the man file, thus giving you a one line description of what the command does. Using whereis [program], will locate the source, binary and manual sections for the given program. For example, if you name your program "test", it won't run, but instead Unix will run the system command "test" by default. You could use the command whereis test to discover this name conflict. OTHER USEFUL UNIX COMMANDS Unix has a VERY large number of useful commands, so only a few of the important ones will be covered here. You can find out who else is logged into the system by typing who. This will give a listing of who is logged onto the machine that you are on and what time that they logged in. Another useful command is the ps command. This will give you a listing of the processes running on your machine. If you would like to view the contents of a file, you could use cat [filename]. To have the file paginated so it doesn't fly right past you, use more [filename]. You will then have to hit space after each page, or q to quit. On our machines, there is an even better command than more, called less, and it allows you to move the text backwards as well as forwards. To move backwards, hit b. COMMUNICATING Using Electronic Mail CS Mail is no longer supported. It is suggested that you use your University System Mail account to send mail which is of the format [email protected]. How to Change your Prompt and Environment: First you need to determine what shell you are by default, this is done by issuing the command echo $0. You should get the response -tcsh, -bash, -sh, -ksh or -csh, depending on how your account is set up. After you've determined what shell you are using you need to edit the file, which is in your home directory, according to the chart below, -tcsh .login -csh .login -bash .profile or .bash_profile -ksh .profile -sh .profile The default scripts are commented and self-explanatory.