Unix: Operations on files

Copying and moving files

Download file

The exercises in this chapter makes use of a file named “unixpast.txt”. You can download a copy of this file from here. (Right click, select “Save Link As…” from the menu, and make sure you save it in your exercises directory.)

Copy file (cp)

The command cp file1 file2 makes a copy of file1 and writes it to file2.

$ mv unixpast.txt unixpast.copy

Move file or directory (mv)

The command 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.

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 unixpast.copy to your backup directory.

First, change working directory to your exercises directory. Then, inside the exercises directory, type

$ mv unixpast.copy repository

Type ls and ls repository to see if it has worked.

Exercise

Create a backup of your unixpast.txt file by copying it to a file called unixpast.copy

Removing files and directories

Remove rm (), rmdir (remove directory)

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

Inside your exercises directory, type the following sequence of commands:

$ cp unixpast.txt tempfile.txt
$ ls
$ rm tempfile.txt
$ ls

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

Exercise

Create a directory called emphemera in your exercises directory, using the command mkdir. Check that it exists. Then remove it using the command rmdir. Check that it is gone

Displaying the contents of a file on the screen

cat (concatenate)

The command cat can be used to display the contents of a text file on the screen. Type:

$ cat unixpast.txt

As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.

less

The command less writes the contents of a file onto the screen a page at a time. Type

$ less unixpast.txt

Press [space] if you want to see another page, and q if you want to quit reading. As you can see, less is more convenient than cat for viewing long files.

Using less, you can search though a text file for a keyword (pattern). For example, to search through unixpast.txt for the word “open”, type:

$ less unixpast.txt

Then, still in less, type / (slash) followed by the word to search:

/open

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

head

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

First clear the screen then type:

$ head unixpast.txt

Then type:

$ head -5 unixpast.txt

What difference did the -5 do 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 unixpast.txt

Exercise

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

File and directory conventions

Unix allows any character except slash (/) and NULL (\000) to be part of a file or directory name. The maximum length for a Unix file name is 255 bytes.

However, it is not a good idea to have unprintable characters in a file or directory name. Some other characters that have special meanings such as / : * ? & $ should be avoided unless you enjoy quoting them. Also, whitespaces within names are a pain on the command line. The safest way to name a Unix file is to use only ASCII alphanumeric characters, (letters and numbers), together with -(hyphen), _ (underscore) and . (dot).

Bad filenames Good filenames Why bad?
notes-17/10/2012.txt notes-2012-10-17.txt contains special characters
load page.php load_page.php contains space

A valid file name usually starts with an ASCII-letter, digit, dot, or underscore, and may end with a dot followed by a group of letters indicating the type of the file. Files consisting of PHP code may for instance be named with the ending .php (e.g. index.php). Unix does not normally care what file types you use (some others programs may care), but following this convention let you use simple commands such as ls *.php to list all PHP files in the current directory.

Directories and executable files (commands) are usually without a file type extension.

In Unix, a directory is also a file. So the rules and conventions for files apply also to directories.

Summary

Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a 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