Tuesday, June 10, 2014

Unix ---> chmod

Your files and directories have a number of attributes that are set when they are created. Listing the files with the -la flag, i.e., ls -la, displays the attributes of each file and directory in the working directory. Here is an example listing:

    total 3
    drwxr-xr-x  2 taylor        512 Aug  2 08:41 .
    drwxrwxr-x 12 taylor       1024 Aug  2 08:41 ..
    -rw-r--r--  1 taylor          5 Aug  2 08:41 blue
    -rw-r--r--  1 taylor         12 Aug  2 08:41 green
    -rw-r--r--  1 taylor          7 Aug  2 08:41 yellow

To the far left of each file or directory name are ten characters which show the attributes. The first column indicates whether the entry is a directory (d) or not (-). The other nine characters are organized into three groups of the three. The first group pertains to the owner (that would be you for your files). The second group pertains to people in your group. The third group pertains to everyone else. Within each group of three are three characters. The first indicates read (r) permission. The second indicates write (w) permission. The third indicates execute (x) permission. If the permission is not present, a "-" will replace the rw, or x.

The chmod (change mode) command lets you change the attributes on a file or directory. There are a number of forms, but I have chosen to cover the following syntax because of its similarity with umask. The syntax is as follows:

    chmod mode filename

where mode is a three digit octal number. The first digit pertains to the owners privileges. The second pertains to the groups privileges, and the third pertains to everyone elses privileges. Each octal digit is composed of the addition of three components. The read component is worth 4, the write component worth 2, and the execute component worth 1. Suppose I wanted the owner to have read, write, and execute privileges, the group to have read and write privileges, and everyone else to have read privileges only. The octal number I would use withchmod would be 764.

Unix ---> alias

The alias command allows you to define shortcuts to save yourself time. In a sense, alias creates a link between a requested set of keystrokes and another set of keystrokes. For example, to use the rm command in interactive mode I would type

    rm -i

By typing

    alias rm 'rm -i'

the alias command would allow me to avoid typing the interactive flag, -i, every time a called the rm command.

Unix ---> Copying

The cp (copy) command lets you duplicate a file of choice. Here is an explanation by examples:
cp cocoon butterfly

makes a duplicate of the file cocoon and gives it the name butterfly. Note that the filenames can include pathnames as well.
cp /home/cernan/taylor/tex/contract ../contract.bak

makes a copy of the file contract found in the /home/cernan/taylor/tex directory and places it one directory level above the working directory in a file called contract.bak.

If /home/cernan/taylor/tex is a directory, then
cp report /home/cernan/taylor/tex

will place a copy of report in the /home/cernan/taylor/tex directory with the name report.
cp /home/cernan/taylor/tex/headlines .
will copy the file headlines in the /home/cernan/taylor/tex directory into the working directory. The name will remain unchanged.
cp /home/cernan/taylor/tex/* .
will copy all the files (but not the subdirectories) in /home/cernan/taylor/tex into the working directory. You can copy all the subdirectories in the directory and files contained in them by using the -r (recursive) flag as follows:
cp -r /home/cernan/taylor/tex/* .

Another useful flag is the -i (interactive) flag which prompts you if you are about to overwrite an existing file.

Vi ---> Repeat

Often times you may desire to repeat the last command performed. This can be done with the "." command. Place the cursor in the appropriate position and press "." to repeat the most recent command. Suppose I had a C program in which I wished to switch a variable name fromno_way to yes_way in two different places. One way I could accomplish this would be to place my cursor on the beginning of the first no_way and type cw (change word) and then type yes_way <esc>. This would accomplish my task for the first case. Now all I would need to do to change the second no_way would be to place my cursor at the beginning of it and type "." to repeat the last command.

Vi ---> Undo

The undo command, u, is another feature that has saved me many times. Pressing u undoes the last command you told vi to perform. Another form of the undo command is U which undoes all the changes made to the current line since you moved there.