Intro to Scientific Computing
PHYS 27/193
Physics Department
University of the Pacific


Copy, Move, Delete, and Examine Unix Files


Once you have extracted the files from somefiles.tar on the previous page, you should have a file structure that looks like this

Let's play with these files and directories a bit.


Copying Files

cp

cp file1 file2

is the command which makes a copy of file1 and calls it file2.
In this case, both files would be in the current working directory.

Other forms are:

cp file1 dir    Place a copy of file1 in directory dir.

Also,

cp file1 file2 file3 dir    places file1 file2 file3 into directory dir.

Notice that if the last argument to cp is an existing directory, all preceding files in the argument list will be placed there.

Both files and directories can have full pathnames if we want to copy files to different places in the file system.

Time for an example.

In your unixplay dir, one of the subdirectories created when you unpacked the tar file, you should find a file named testfile
verify this using ls.
Now make a copy of testfile called testfile2.

cp testfile testfile2

Verify that you now have 2 files: testfile and testfile2.


Now let's take a file stored in unixplay and use the cp command toput a copy in your newdir directory.

First, cd into your newdir directory. It's below your Home directory.

cd ~/newdir

Then at the prompt, type,

cp ~/unixplay/file_unixplay.txt .

Note: Don't forget the dot . at the end. Remember, in Unix, the dot means the current directory. Also notice that we used full pathname of the original (source) file including the shortcut "~" for your HOME directory.

The above command means copy the file file_unixplay.txt, which is in unixplay (itself a sub-directory of your HOME directory) to the current directory ("."), keeping the name the same.

The effect of these commands is as follows:


Exercise 2a

Find the file testfile2 in your unixplay/ directory.

Put a copy of testfile2 called testfile3, in your newdir/ directory



Moving files

mv

mv file1 file2

moves (or renames)   file1 to file2

To move a file from one place to another, you also use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.

It can also be used to rename a file, by moving the file to the same directory, but "moving" it a different name.


Use mv to rename a file

We are now going to move the file testfile3 to testfile4.

First, change directories to your newdir/ directory (remember how?). Then type

ls

to see what files are there. Do you see testfile3 that you made by copying testfile2 above?

Type

mv testfile3 testfile4

Now type ls to see if testfile3 is gone and replaced by testfile4  (i.e. that testfile3 was renamed to testfile4).


Use mv to move a file to somewhere else

We will now move the file testfile4 back into your unixplay/ directory.

Do an ls to verify that testfile4 is still there in your newdir directory.

Now do

mv testfile4 ../unixplay

You can see in this command that the destination directory is up one level (..) and then down into the unixplay/ directory.

Verify that the move worked--that testfile4 is not in the current directory (newdir/) and is now in unixplay/.



Use mv on a whole directory

Amazingly, you can "mv" a directory--either in the sense of renaming or moving it.

cd to your home directory.

Do an ls -F to see that the directories unixplay/ and newdir/ are there. (Remember what the -F does?)

Now do

mv newdir dirTHREE

Do you see that this is using mv to rename the directory?
Verify that it worked.
Was newdir been renamed to dirTHREE?


Still in you home dir, do

mv dirTHREE unixplay

This will move the newly renamed dirTHREE into your unixplay directory.

Verify that dirTHREE is now a subdirectory of unixplay.
There should also be a dirONE and dirTWO there as well.



Removing files and directories

rm and rmdir

To delete (or remove) a file, use the rm command. As an example, we are going to create a copy of the pope.txt file then delete it.

Inside your unixplay directory, type

ls

Is the file file_unixplay.txt there? We don't need this one anymore.

Do

rm file_unixplay.txt
ls

The file_unixplay.txt should have been listed before you did the rm command, but then be gone in the second listing, since you rm'ed it.

You can use the rmdir command to remove a directory (make sure it is empty first). Try to remove the unixplay directory. You will not be able to since Unix will not let you remove a non-empty directory.


Exercise 2b

Create a directory called tempstuff using mkdir,

then remove it using the rmdir command.


Displaying the contents of a file on the screen

clear

Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.

At the prompt, type

clear

This will clear all text and leave you with the prompt at the top of the window.

 

cat

The command cat (for concatenate) can be used to display the contents of a file on the screen. Type:

cat testfile

This is fine since the file contains a small amount of text which all fits on the screen.
If the file is bigger than will fit in an xterm window, we will use the less command. More on this below.

You can also do cat file1 file2 file3. This will concatenate (add together) all three files into one long file and spew it to the screen. Unless the file is small enough to fit whole on the screen, cat is most often used to pass the contents of a file to another program.

Try it with the three files in the unixplay/ dir. Do

cat testfile anotherFile yetanother

This should produce
This is a text file, called testfile.
This is another file.
This is yet another file.
The cat command printed the contents of each file, one after the other.


less

The command less writes the contents of a file onto the screen a page at a time, which is much more suitable for actually reading a file.

In your unixplay/ dir, you will find a a subdir called dirONE which contains the file "KublaKhan.txt".

Cd to dirONE, then do

less KublaKhan.txt

Press the [space-bar] if you want to see the next page, and type [q] when you want to quit reading.

As you can see, less is much better than cat for long files.

less is a modern replacement to the original Unix paging file viewer called "more". The more program printed a page at a time, but offered little other functionality. It's a bit of Unix geek humor that the replacement for the more program would be named "less".

less offers many features for searching and moving. More on this below. For example, while viewing a file, after at least the first page, hitting the [b] key takes you backward by one page. You can search for test by hitting the [/] key, followed by the text you are searching for, then hitting [Enter].

You can see more of the features and how to use them by typing [h] while viewing a file with less.

 

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then cd to your unixdir/ directory.
First find the file ints.dat and view it with less. It contains the first 100 integers. Now type

head ints.dat

Then type

head -5 ints.dat

What difference did the -5 option make to the head command? This generalizes to -n

, where n is the number of lines you want to print.

 

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

tail ints.dat

Q. How can you view the last 15 lines of the file?

 

Searching the contents of a file

Simple searching using less

Using less, you can search though a text file for a keyword (pattern). For example, to search through KublaKhan.txt for the word "pleasure", type

less KublaKhan.txt

(You should be able to find this file by now, even if you forgot where it is).

Now, while still viewing the file in less, type a forward slash [/] followed by the word you wish to search for

/pleasure

As you can see, less finds and highlights the keyword. Type the "n" key to search for the next occurrence of the word.

 

grep

This is a REALLY useful command. (don't ask why it is called grep. Ok, if you must know, the answer's here)

grep is one of many standard UNIX utilities. It searches files for specified words or patterns.
First clear the screen, cd into unixplay/, do ls and find the file scifi_list.txt. This is a list of the top Science Fiction books of all time, in random order.
Have a look with less.

Then type

grep time scifi_list.txt

As you can see, grep has printed out each line containg the word time.

Or has it ????

Try typing

grep Time scifi_list.txt

The grep command is case sensitive; it distinguishes between time and Time.

To ignore upper/lower case distinctions, use the -i option, i.e. type

grep -i time scifi_list.txt

To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for the phrase Time Machine, type

grep 'Time Machine' scifi_list.txt

Some of the other options of grep are:

-v display only those lines that do NOT match the expression
-n precede each matching line with the line number
-c print only the total count of matched lines

Try some of them and see the different results. Don't forget, you can use more than one option at a time. For example, the number of lines without the words time or Time is

grep -ivc time scifi_list.txt

 

wc

A handy little utility is the wc command, short for Word Count. To do a word count on scifi_list.txt, type

wc -w scifi_list.txt

To find out how many lines the file has, type

wc -l scifi_list.txt

With no options

wc scifi_list.txt

wc prints: 100 746 4173 scifi_list.txt

which shows: lines words bytes filename.


Sort the contents of a file

The file scifi_list.txt is nice; it contains the top 100 Science Fiction books. However, it would be nice if they weren't in random order!

No problem--use sort!

First have a look at the file using less. The columns are

1995    53      Stephenson, Neal        The Diamond Age
1956    36      Bester, Alfred  The Stars My Destination
2000    89      Reynolds, Alastair      Revelation Space 
...
or Year  Rank  Author  Title.

If you simply do

sort scifi_list.txt

you will order the file alphabetically on the first column:
1818    52      Shelley, Mary   Frankenstein
1864    57      Verne, Jules    Journey to the Center of the Earth
1870    32      Verne, Jules    20,000 Leagues Under the Sea
1884    92      Abbott, Edwin A Flatland
1889    86      Twain, Mark     A Connecticut Yankee in KA's Court
1895    16      Wells, H G      The Time Machine
1897    77      Wells, H G      The Invisible Man
1898    18      Wells, H G      The War of the Worlds
...

Notice that "alphabetical" on the year, ends up ordering the years. This is because alphabetical order is 0123...89ABCD...XYZabcd...xyz.

Nice! Now we see the top books, ordered by year.

How about rank (in column two)?

Do:

sort -k 2 scifi_list.txt

which means "sort alphabetically on column 2".
You should see:
1965    1       Herbert, Frank  Dune 
1968    10      Clarke, Arthur C        2001: A Space Odyssey
1974    100     Lem, Stanislaw   The Cyberiad
1970    11      Niven, Larry    Ringworld
1959    12      Heinlein, Robert A      Starship Troopers
1968    13      Dick, Philip K  Do Androids Dream of Electric Sheep?
1932    14      Huxley, Aldous  Brave New World
1973    15      Clarke, Arthur C        Rendezvous With Rama
1895    16      Wells, H G      The Time Machine
1966    17      Heinlein, Robert A      The Moon is a Harsh Mistress
1898    18      Wells, H G      The War of the Worlds
1989    19      Simmons, Dan    Hyperion 
1985    2       Card, Orson Scott       Ender's Game 
1954    20      Clarke, Arthur C        Childhood's End
1950    21      Bradbury, Ray    The Martian Chronicles
...

You can see that the books are ordered by the second column now, but the oreder is strange. It's alphabetical, but we really wanted "numerical" ordering. This time do:

sort -k 2 -n scifi_list.txt

This time we get it the way we wanted:
1965    1       Herbert, Frank  Dune 
1985    2       Card, Orson Scott       Ender's Game 
1951    3       Asimov, Isaac   Foundation 
1979    4       Adams, Douglas  Hitch Hiker's Guide to the Galaxy 
1949    5       Orwell, George  1984
1961    6       Heinlein, Robert A      Stranger in a Strange Land
1954    7       Bradbury, Ray   Fahrenheit 451
1984    8       Gibson, William Neuromancer
1950    9       Asimov, Isaac    I, Robot
1968    10      Clarke, Arthur C        2001: A Space Odyssey
1970    11      Niven, Larry    Ringworld
1959    12      Heinlein, Robert A      Starship Troopers
1968    13      Dick, Philip K  Do Androids Dream of Electric Sheep?
1932    14      Huxley, Aldous  Brave New World
1973    15      Clarke, Arthur C        Rendezvous With Rama
...

We could also alphabetize the file on the author's names:

sort -k 3 scifi_list.txt

gives us:
1884    92      Abbott, Edwin A Flatland
1979    4       Adams, Douglas  Hitch Hiker's Guide to the Galaxy 
1950    9       Asimov, Isaac    I, Robot
1951    3       Asimov, Isaac   Foundation 
1954    29      Asimov, Isaac   The Caves of Steel
1955    49      Asimov, Isaac   The End Of Eternity
1972    43      Asimov, Isaac   The Gods Themselves
1985    68      Atwood, Margaret        The Handmaid's Tale
1988    56      Banks, Iain M   Player Of Games 
1990    65      Banks, Iain M   Use of Weapons 
1985    97      Bear, Greg      Blood Music
1985    60      Bear, Greg      Eon
1953    64      Bester, Alfred  The Demolished Man
1956    36      Bester, Alfred  The Stars My Destination
...

Try:

sort -k 3 -r scifi_list.txt

What did the -r option do?

sort is VERY handy!


Summary

Command Meaning
cp file1 file2 make a copy of file1 called file2
cp file1 dir put a copy of file1 in dir
mv file1 file2 move or rename file1 to file2
mv file1 dir move file1 into dir
rm file remove a file or files
rmdir directory remove an (empty) directory
cat file display a file
less file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file
sort file sort lines of a file

Next: Controlling Data Flow