Working with Files
Copying Files
cp
(copy)
cp
file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2.
For our example we will create a file science.txt. Click the down-arrow icon to download the file. Use whatever method you know to place this file into your home directory.
In the space of one hundred and seventy-six years the Lower Mississippi has shortened itself two hundred and forty-two miles. That is an average of a trifle over one mile and a third per year. Therefore, any calm person, who is not blind or idiotic, can see that in the Old Oolitic Silurian Period, just a million years ago next November, the Lower Mississippi River was upwards of one million three hundred thousand miles long, and stuck out over the Gulf of Mexico like a fishing-rod. And by the same token any person can see that seven hundred and forty-two years from now the Lower Mississippi will be only a mile and three-quarters long, and Cairo and New Orleans will have joined their streets together, and be plodding comfortably along under a single mayor and a mutual board of aldermen. There is something fascinating about science. One gets such wholesale returns of conjecture out of such a trifling investment of fact.
- Mark Twain, Life on the Mississippi
% cd ~/unixstuff
Then at the Unix prompt, type,
% cp ../science.txt .
Note: Don’t forget the dot (.) at the end. Remember, in UNIX, the dot means the current directory.
The above command means copy the file science.txt from the parent directory to the current directory, keeping the name the same. To change the name, use
% cp ../science.txt ./newname
Exercise 2A
Create a backup of your science.txt file by copying it to a file called science.bak.
Moving Files
mv
(move)
mv file1 file2
moves (or renames) file1
to file2
. To move a file from one place to another, 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 giving it a different name. We are now going to move the file science.bak to your backup directory. First, change directories to your unixstuff directory (can you remember how?). Then, inside the unixstuff directory, type
% mv science.bak backups/.
Type ls
and ls backups
to see if it has worked.
Removing Files and Directories
rm
(remove), rmdir
(remove directory)
To delete (remove) a file, use the rm
command. As an example, we are going to create a copy of the science.txt file then delete it. Inside your unixstuff directory, type
% cp science.txt tempfile.txt
Confirm the file was created:
% ls
Now delete it:
% rm tempfile.txt
% ls
You can use the rmdir
command to remove a directory, but only if it is empty. Try to remove the backups
directory. You will not be able to since Unix will not let you remove a non-empty directory.
To remove a non-empty directory use
rm -rf directory
You can request confirmation with
% rm -if directory
though this may be tedious. The -i
option (inquire) also works for rm
% rm -i myfile
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
cat
(concatenate)
The cat
command can show a text file’s contents
% cat science.txt
Be sure to use the correct path to the file. Cat can also join two text files, hence its name.
% cat file1 file2 > file3
THe >
sign is a redirection, which we will discuss later.
clear
(clear screen)
Clear the screen.
more
The command more
prints the contents of a file onto the screen a page at a time. Type
% more science.txt
Press the [space bar] if you want to see another page. Type [q] if you want to quit reading.
The more
command is an example of a pager, a program that “pages” through a text file.
head
The head command writes the first ten lines of a file to the screen.
% head science.txt
Now type
% head -5 science.txt
What difference did the -5
make to the head
command?
tail
The tail
command writes the last ten lines of a file to the screen. Clear the screen and type
% tail science.txt
How can you view the last 15 lines of the file?
Searching the Contents of a File
Simple Searching Using more
Using more
, you can search through a text file for a keyword (pattern). For example, to search through science.txt for the word ‘science’, type
% more science.txt
then, still in more
(i.e. don’t press [q] to quit), type a forward slash [/
] followed by the word to search
/science
The more
command finds and highlights the keyword. Type [n] to search for the next occurrence of the word.
grep
(don’t ask why it is called grep)
grep
is one of many standard Unix utilities. It searches files for specified words or patterns. First clear the screen, then type
% grep science science.txt
As you can see, grep has printed out each line containing the word science… or has it? Try typing
% grep Science science.txt
The grep
command is case-sensitive; it distinguishes between Science and science. To ignore upper/lower case distinctions, use the -i option, i.e. type
% grep -i science science.txt
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type
% grep -i 'spinning top' science.txt
Some other options for grep
are: -v
(display those lines that do NOT match); -n
(precede each matching line with the line number); and -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 science
or Science
is
% grep -ivc science science.txt
wc
(word count)
A handy little utility is the wc command, short for word count. To do a word count on science.txt
, type
% wc -w science.txt
To find out how many lines the file has, type
% wc -l science.txt
Summary
Command | Operation |
---|---|
cp file1 file2 | copy file1 and call it file2 |
mv file1 file2 | move or rename file1 to file2 |
rm file | remove file |
rmdir directory | remove directory |
cat file | display file |
more file | display file a page at a time |
head file | display the first few lines of a file |
tail file | display the last few lines of file |
grep ‘keyword’ file | search file for keywords |
wc file` | count number of lines/words/characters in file | |