browser icon
You are using an insecure version of your web browser. Please update your browser!
Using an outdated browser makes your computer unsafe. For a safer, faster, more enjoyable user experience, please update your browser today or try a newer browser.

Unix / Linux: Intermediate

Posted by on May 19, 2010

    In this section we will be looking at a host of items concerning Unix.  The first section will deal some more with command line sytax and usage, and quoting characters.  The second section will deal with setting file permissions on files and directories and a little bit of relative paths and links.  The third section will deal with more utilities that you can use in your daily workings with Unix.   Be sure to review the first basic lessons of file management and basic command line syntax.  Now, let’s get started.

– Section 1 –

Command Line Syntax

    Just as we used the command line in the last lesson to do some basic things like making directories, files, and changing passwords, we have not covered the correct form of the command line.  We briefly touched on the use of commands ( utilities ) and arguments ( options ) to do things like listing the contents of a file, now we need to examine some more concrete definitions of what this all means.

    The first thing we need to distinguish between are commands and command lines.  When we do an ls ( list ) we are using a command.  When we look at the entire line of text below using ls and -a, we are using a command line ( commands are the actual utilities while the command line is just the line of text ). 

$ ls -a

    Command line sytax is just the order in which the commands and the argumenst are written by you the user and how they are read by the Shell ( Unix ).   Below is the common form for command line sytax.

$ command [argument] [argument]

    This is the same as

$ ls -ax

    where ls is the command and the arguments are ( options ) a ( all ) and x ( listed in horizontal columns ).  Remember to use the proper argument in the proper way.  Some commands require arguments and will not work without them.  An example of this is the copy utility ( cp ).  This is used to copy one existing file and renaming it to another file name.  For example:

$ cp somefile newfile

    The above command line essentially copied the existing file called "somefile" and made a copy of that file naming it  "newfile".   If we had simply used cp alone without any arguments, we would have recieved an error like the one below.

$ cp
usage: cp [-fhip] [–] source_file destination file
    or:  cp [-fhip] [–] source file … destination directory
    or:  cp [-fhip] [-R | -r] [–] [source_file | source directory]
… destination directory

    By makine an error in the usage of the command, Unix tells us where we made a mistake and what the correct usage is.  If you are ever unsure about the proper command line syntax, you can use the man ( manual ) command followed by the name of the utility.  For example:

$ man cp

    This would display the manual for the cp utility explaining its usage, syntax, and offer examples.

Quoting Characters and Special Characters

    Often times it is useful to use what are called special characters to cut down on the amount of time needed to type in a command line.  These special characters are often called wildcards.  Let’s look at them.

The ?

    The question mark is a wild card / special character that is used to repalce a single character.  For example, if I had the following files

bill    bill1    bill12     bill3    bill45   

I could use the ls command to list them all.  But what if I only wanted the files with one character after the word bill?
  I would use the following syntax

$ ls bill?

to get

bill1    bill3

You can use the ? in just about any setting with a file or directory name to narrow your search of your directories.  You can also use ? in comparing file contents.   We will examine that later in utilities.  We can use the ? anywhere in our file / directory name to achieve the results we want.  The following are all valid.

?ill    ?bi?ll    ?bil?1

The *

The asterix is much like the question mark insofar as it serves as a wild card.   The major difference is that the asterix searches for any number of characters after its placement.  For example, if we used the same list as above, but used ls with an asterix, we would get the following resutls.

$ ls b*

bill    bill1    bill12     bill3    bill45

    Each of the files conforms to the search criteria.  Each file begins with the letter b!  Let’s look at another example

$ ls *1*

bill1    bill12

    In this case, we instructed the shell to look for any file that had the number 1 in it somewhere, returning two results from our list of files.  The asterix can also be used in compring files and in any part of a file / directory name.

The []

    Brackets are yet another set of wild cards, but serve to restrict and define your command.  When used, only the characters present in the brackets will be used when searching.  For example, again using our list from the first example

$ ls bill[12]

bill1

In this case we instructed ls to find all the files named bill with the numbers 1 or 2…not 1 and or 2.  If we wanted to do that we would need to do the following

$ls bill[1]*

Now let’s say that we wanted to produce a list of files that all had a number in them from 0 to 9.  To do this we would use the following syntax

$ ls bill[0-9]

bill1    bill3

This may seem a bit confusing, but if you practice, it will make sense very quickly.   You can mix and match any of the wild cards to take a great deal of control over how you want results to be displayed.

Section 2 – File and Directory Permissions

    Each file and directory in Unix has permissions set on who can read, write, and excute the files.  The owner ( or an administrator) typically has control over why can access the files, write to the files, and if a program, execute the file.   If used in a directory the execute command means the ability to browse the directory.  Below you will see a list of permissions on several files.  Note that (r) is for read, (w) is for write, and (x) is for execute.

$ ls -lg*
-rw——- 1 jason fairnet 5 Sep 6 13:14 bill
-rw——- 1 jason fairnet 1 Sep 6 13:15 bill1
-rw——- 1 jason fairnet 1 Sep 6 13:15 bill12
-rw——- 1 jason fairnet 1 Sep 6 13:15 bill3
-rw——- 1 jason fairnet 1 Sep 6 13:15 bill45

***Note*** the -lg arguemnts for ls are (l)ong (g)roup permissions***

The list above gives us the following information, working from left to right.

-the type of file is represented by the first character
-the file permissions are the next nine characters

These nine dash marks () are grouped as follows from left to right:

First three (-rwx——) The owner of a file can
                                1) read
                                2) write
                                3) execute

        Second three (—-rwx—) The member of the owner’s group can try to
                                1) read
                                2) write
                                3) execute

The Last three (——-rwx) Anyone else can try to:
                                1) read
                                2) write
                                3) execute

-the number of links to the file
-the owner of the file
-the group that has access to the file
-the size of the file in bytes
-the last time the file was created or modified
-the name of the file

So you can create files, move them, rename them, and delete them.  What about sharing them?  If you want or need to give persons on your network access to certain files, you can modify the permissions on a file ( or directory ) using the chmod utility.   Let’s take a few moments to look at it.

The first thing tha I want to do is to change the group permissions for the file bill to allow for others in my group to be able to read it.  To accomplish this I would do the following.

$ chmod g+r bill
$ ls -lg *
-rw-r—– 1 john fairnet 5 Sep 6 13:14 bill

Here we have used chmod to add (+) the read (r) permission to the group (g).   I could give everyone permission to read the file by doing the following.

$ chmod a+r bill
$ ls -lg bill
-rw-r–r– 1 fsjte student 5 Sep 6 13:14 bill

As you can see, everyone now has read permission.  We can even combine several options into one line.  In this case I am going to remove read permissions from others ( the third set) and add write to the group permissions.

$ chmod o-r bill
$ chmod g+w bill
$ ls -lg bill
-rw-rw—- 1 fsjte student 5 Sep 6 13:14 bill

You should pick up that to add the permission you use + and to remove it you use .  Fairly simple.  Now let’s combine the permission sets on several files.  In this case it will be all of the files that have the word bill in them.  I will list them first so that you can see the original permission sets.

$ ls -lg *
-rw——- 1 fsjte student 5 Sep 6 13:14 bill
-rw——- 1 fsjte student 1 Sep 6 13:15 bill1
-rw——- 1 fsjte student 1 Sep 6 13:15 bill12
-rw——- 1 fsjte student 1 Sep 6 13:15 bill3
-rw——- 1 fsjte student 1 Sep 6 13:15 bill45

$ chmod g+rw bill*
$ ls -lg bill*
-rw-rw—- 1 fsjte student 5 Sep 6 13:14 bill
-rw-rw—- 1 fsjte student 1 Sep 6 13:15 bill1
-rw-rw—- 1 fsjte student 1 Sep 6 13:15 bill12
-rw-rw—- 1 fsjte student 1 Sep 6 13:15 bill3
-rw-rw—- 1 fsjte student 1 Sep 6 13:15 bill45

Fairly simple stuff.  Ok, now what about directories?  The permissions are exaclty the same, noting that the execute permission is not executing a file, but rather allowing persons to look through the directory.  Practice changing the permissions on a dummy directory and file until you are compfortable with it.

 

More Utilities

    Below you will find a chart of some more utilites and their arguments.  Practice with each one until you are fairly compfortable with them and have a good working knowledge of the arguments.

 

Utility Name Function Arguments
finger Dispays detailed information on users

finger [argument] name

-l  Long – detailed info in long form
-m Match – matches user names against full names
-q  quick – displays only login name, terminal device number, and time user logged in
-s short – short report
w Displays information on system users

w [argument] name

-s short – short report
mesg Enable and Disable messages

mesg[y or n]

y is yes…receive messages
n is no, don’t recieve messages
Mail Email

mail [argument][subject] recipients

-H – displays the headers of the mail
-s sets the subject field of your mail
learn Learn to use aspects of Unix

learn [name of lesson]

Leave learn blank if you would like a list of topics
man Displays the manual for a utility

man[utility name]

 
echo Displays a message typed in by the user

echo [argument] message

 
date Displays the date -u – Universal – shows time in Greenwich Mean Time
cp Copy one or more files

cp [options] source-file destination-file
cp [options] source-file-list destination directory

-i – Interactive – causes cp to prompt user if overwrite of a file is possible
-p – preserve – sets permissions of destination file to be the same as the source
-r – recursive – to be used when the destination is a directory.  Copies directories and subdirectories
lpr Print files

lpr [argument][file]

-h – skip printing a header page
-i – indents the printing
-m – mails you when printing is done
-p – paginate files
-r – removes file after it has been put into the queue
grep Searches for a pattern in files

grep [argument] pattern [file-list]

-c – grep displays the number of lines in each file that contain  a match
-i – grep ignores case
-l – grep displays the name of each file that conatins a match
-v – grep displays files that do not match the criteria
head Dispalys the beginning ( head ) of a file

head [-number] [file-list]

sames as tail
tail Displays the last part ( tail ) of a file -counts by lines

tail [argument] [+ or – number] filename

-b – Blocks causes tail to count by blocks
-c – characters – causes tail to count by characters
-l – Lines – default – causes tail to count by tail
-f – follow – After printing the last line of the file, tail keeps running in a loop so that you can track changes and progress.  MUST BE STOPPED WITH THE INTERUPT OR KILL COMMAND!
-r -Reverse – Displays lines in reverse order

+ is used to start at a specific line ie (tail +8 filename) gives lines from 8 on
– is used to list from a certian point ie ( tail -3 filename) gives the last three lines

diff Displays the diffrences between two files or directories

diff [argument] file(directory}1 file(directory)2

-b – Blanks – diff ignores blank spaces.
-c[x] – displays the sections of the two files that are different where [x] is the number of lines displayed.  Default is three.

This is only a small list of the utilities.  A full list will be posted eventually. Please come back again for the advanced section of Unix!

Share on Facebook

Leave a Reply