![]() |
Intro to Scientific Computing |
![]() |
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.
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.
Also,
Notice that if the last argument to
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
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
The effect of these commands is as follows:
moves (or renames) file1 to file2
To move a file from one place to another, you also use the
It can also be used to rename a file, by moving the file to the same directory, but "moving" it a different name.
We are now going to move the file testfile3 to testfile4.
First, change directories to your newdir/ directory
ls
to see what files are there. Do you see testfile3 that you made by copying testfile2 above?Type
mv testfile3 testfile4
Now type
We will now move the file testfile4 back into your unixplay/ directory.
Do anNow 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.
Amazingly, you can "mv" a directory--either in the sense of renaming or moving it.
Do an
Now do
mv newdir dirTHREE
mv dirTHREE unixplay
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
Do
rm file_unixplay.txt
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.
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. The command cat testfile
You can also do
Try it with the three files in the unixplay/ dir. Do
cat testfile anotherFile yetanother
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.
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 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.
The head command writes the first ten lines of a file to the screen. First clear the screen then head ints.dat Then type head -5 ints.dat What difference did the -5 option make to the head command? This generalizes to
-n 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? 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 (
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 is one of many standard UNIX utilities. It searches files
for specified words or patterns.
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 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 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 wc scifi_list.txt
which shows:
No problem--use sort!
First have a look at the file using less. The columns are
If you simply do
sort scifi_list.txt
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
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
We could also alphabetize the file on the author's names:
sort -k 3 scifi_list.txt
Try:
sort -k 3 -r scifi_list.txt
sort is VERY handy!
Next: Controlling Data Flow
Use mv on a whole directory
Was newdir been renamed to dirTHREE?
Still in you home dir, do
There should also be a dirONE and dirTWO there as well.
Removing files and directories
rm and rmdir
ls
Exercise 2b
Displaying the contents of a file on the screen
clear
cat
If the file is bigger than will fit in an xterm window, we will use the less command. More on this below.
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
head
First find the file ints.dat and view it with less. It contains the first 100 integers.
Now type tail
Searching the contents of a file
Simple searching using less
grep
This is a REALLY useful command.
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.
-n precede each matching line with the line number
-c print only the total count of matched lines
wc
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!
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.
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
...
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
...
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
...
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
...
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