File Commands

Working With Unix
[Word Counting |Heading a File |Tailing a File |Sorting a File |File Permissions |Exercises ]

Word Counting

To get the number of lines, words, or characters in a file, you can use the wc command. wc has the following syntax:

wc [ -lwc ] [ filename ... ]
You can have any combination of options. wc takes a filename, or filenames, and counts them. If the "-l" option is present, it will display the number of lines in the file. If the "-w" option is present, it will display the number of lines in the file. A "-c" displays the number of characters. With no options, wc displays all three in the order of lines, words and then characters. Other wise they are in the order of the options.

larry% ls
hi                              mp.and.the.holy.grail.txt
larry% wc mp.and.the.holy.grail.txt
    1417   10380   60531 mp.and.the.holy.grail.txt
larry% wc -l mp.and.the.holy.grail.txt
    1417 mp.and.the.holy.grail.txt
larry% wc -w mp.and.the.holy.grail.txt
   10380 mp.and.the.holy.grail.txt
larry% wc -c mp.and.the.holy.grail.txt
   60531 mp.and.the.holy.grail.txt
larry% wc -cwl mp.and.the.holy.grail.txt
   60531   10380    1417 mp.and.the.holy.grail.txt
larry% 

Heading a File

If you have a very long text file, say, like the script to Monty Python and the Holy Grail, and you only want to look at the beginning of it, how do you do it? The easy way to do it is with the head command.

head [ -n ] [ filename ...]
head displays the first n lines of each filename. If -n is not specified, it defaults to 10.

larry% ls
hi                              mp.and.the.holy.grail.txt
larry% head hi
Hi there. How are you?
larry% head mp.and.the.holy.grail.txt
 
"The Script, of The Soundtrack, of The Movie, of the Python Presentation, of
The Retelling of the Great Classic, of The Myth, of The Legend, of The
Unproven Hearsay, of The See Webster's Dictionary for the Definiton of the
Above - Better Known as...
 
                     (Alias) (Wik) *(Da Duh Da Daaaaaa)*
                     MONTY PYTHON AND THE MOST HOLY GRAIL
                           (and we really mean it)
                               (also also wik)
larry% head -2 mp.and.the.holy.grail.txt
 
"The Script, of The Soundtrack, of The Movie, of the Python Presentation, of
larry% 

Tailing a File

Head's counter part is tail. Tail allows you to look at the last part of a file. The syntax is:

tail [ +|-n ] [ filename ]
Tail will display the last 10 lines of the file unless there is a +n or -n. If there is a +n, tail displays all of the lines after the nth line. If it is a -n, tail displays the last n lines.

larry% ls
hi                              mp.and.the.holy.grail.txt
larry% tail mp.and.the.holy.grail.txt
             (Alias) (Also also also wik) *(Da Duh Da Daaaaaa.)*
                     MONTY PYTHON AND THE MOST HOLY GRAIL
                           (and we really mean it)
                  (Why not try a holiday in Sweden this year)
                        (See many large furry animals)
                        (Including the majestic Moose)
         (No, really!  My sister got bitten by a moose once.   AAAAGH!)
               (The Management would like to apologize and say:)
         (1:  The previous writer of the parenthesis has been sacked.)
                                (2:  THE END)
larry% tail -2 mp.and.the.holy.grail.txt
         (1:  The previous writer of the parenthesis has been sacked.)
                                (2:  THE END)
larry% wc -l  mp.and.the.holy.grail.txt
    1417 mp.and.the.holy.grail.txt
larry% tail +1412 mp.and.the.holy.grail.txt
                        (See many large furry animals)
                        (Including the majestic Moose)
         (No, really!  My sister got bitten by a moose once.   AAAAGH!)
               (The Management would like to apologize and say:)
         (1:  The previous writer of the parenthesis has been sacked.)
                                (2:  THE END)
larry% 

Sorting Files

When keeping lists, it is often useful to sort the a list. The sort command achieves this:

sort [-d, -f, -n, -o filename, -r ] [ filename ... ]
Sort takes it's input from standard input unless a file or filenames are specified. It then sorts the file by line, and returns it to standard output, usually the screen. The different options cause sort to sort the data in different ways.

The "-d" option sorts the file in a "dictionary" way. Only characters, numbers and black space are used to determine the order.

The "-f" causes sort to sort in a case insensitive manner.

An "-n" option causes sort to sort in a numerical fashion.

An "-o" option causes sort to write to a file specified after the option instead of to the screen.

Finally, a "-r" option sorts in reverse order.

larry% cat breakfast
toast
eggs
rock
pancake
Cereal
cereal killer
mushroom
milk
orange juice
sun
syrup
bomb
larry% sort breakfast
Cereal
bomb
cereal killer
eggs
milk
mushroom
orange juice
pancake
rock
sun
syrup
toast
larry% sort -f breakfast
bomb
Cereal
cereal killer
eggs
milk
mushroom
orange juice
pancake
rock
sun
syrup
toast
larry% sort -r breakfast
toast
syrup
sun
rock
pancake
orange juice
mushroom
milk
eggs
cereal killer
bomb
Cereal
larry% 

File Permissions

File permissions are an important part of UNIX. They decide what users can do with a certain file. To get the permissions for a file, use ls -l.

larry% ls -l
total 86
-rwxr-xr-x  1 schallee    24576 Jul 13 09:54 a.out
-rw-r--r--  1 schallee       87 Jul 13 09:38 breakfast
-rw-r--r--  1 schallee       23 Jul 11 15:51 hi
-rw-r--r--  1 schallee    60531 Jul 11 15:51 mp.and.the.holy.grail.txt
larry% 
The first field of every line is the file permissions. The file permissions consist of sets of thee characters. If there is a "-" instead of the right character, that permission is denied.

The first character is the read permission. It will have a "r" if it is granted.

Next comes the write permission. This decides if a user can change the file. If it is a "w" then the user can change the file.

The last permission is the execute permission. This decides if a user can execute the file. This is usually only set on files that are programs. The character for it is "x".

The first of these three groups, starting with the second character of the line, is the permissions for the owner of the file. The second is for the members of the group the file belongs to, and the third is the permissions for everyone else.

To set the permissions of a file, you use the chmod command.

chmod ugo,+-,rwx filenames ...
The first option determines what permissions you are giving the following files. First comes which group of people you are dealing with. "u" for the owner, you, "g" for the members of the group, and "o" for everyone else. Then a plus sign if you are adding permissions, or a minus sign if you are subtracting permissions. Then what permissions in the form of "r" for read, "w" for write, and "x" for execute.

So, if I want to let anyone add items to my list of breakfast items, I would give everyone in my group and everyone else, write permission to the file:

larry% ls -l breakfast
-rw-r--r--  1 schallee       87 Jul 13 09:38 breakfast
larry% chmod g+w breakfast
larry% chmod o+w breakfast
larry% ls -l breakfast
-rw-rw-rw-  1 schallee       87 Jul 13 09:38 breakfast
larry% 
And if I wanted to deny execute permission to "a.out" to every one except those in my group and myself, I would:

larry% ls -l a.out
-rwxr-xr-x  1 schallee    24576 Jul 13 09:54 a.out
larry% chmod o-x a.out
larry% ls -l a.out
-rwxr-xr--  1 schallee    24576 Jul 13 09:54 a.out
larry% 

Exercises

  1. Count the number of lines, characters, and words in a file.
    larry% wc mp.and.the.holy.grail.txt
    
  2. Display the first 20 lines of a file.
    larry% head -20 mp.and.the.holy.grail.txt
    
  3. Display the last 20 lines of a file.
    larry% tail -20 mp.and.the.holy.grail.txt
    
  4. Sort a file in a case insensitive manner, and display it on the screen.
    larry% sort -f breakfast
    
  5. Sort a file in reverse, and save it to a different file.
    larry% sort -r -o lunch breakfast
    
  6. Give everybody write permission to a file.
    larry% chmod g+w lunch
    larry% chmod o+w lunch
    
  7. Verify that the file's permissions were changed.
    larry% ls -l lunch
    

[Back |Contents |Next ]

Edward B. Schaller (schallee@earthlink.net)