Working With Files
Included on this page:- File Names
- Create a File With a Text Editor
- List Your Files
- Copy a File
- Change the Name of a File
- View a File
- Delete a File
One of the most important skills in any computer system is knowing how to work with files. A file is a collection of characters that the operating system treats as a unit. Files can store any information you want to save. For example, files can contain text for letters and email messages, code for programs, or data for calculations. After you create a file, you can add material to it, delete material from it, or remove it from the system.
File Names
For every file you create, you must supply a name. In Unix, the following characters have special meaning, so you should avoid using them in file names:/ \ " ` ' * | ! ? ~ $ < > &
File names may be between 1 and 255 characters long, but you will find that short, descriptive names are easiest to use. File names should not contain spaces. Instead of spaces, use underscores or periods to separate names made of multiple words, as follows:
mail.Jan annual_report unix_tips
Also, Unix distinguishes between uppercase and lowercase letters. For example, Unix would treat these as three different files:
project1 Project1 PROJECT1
Create a File With a Text Editor
Text editors let you enter, edit, and re-arrange text in files. Unix computers often support several text editors including Pico
, vi, and others.
If you are a beginning Unix user, you might prefer Pico
to vi for the following reasons:
- Pico is easier to learn and use.
- Pico contains online help text and lists the most commonly used commands at the bottom of each screen.
- Pico is designed for worry-free exploration.
To create a new file using the Pico text editor, enter:
pico file
Note: the p in the pico command is lowercase.
This starts the Pico text editor and opens a blank file. Pico assigns to that file the name you specify. Use your keyboard to add text to the file.
To save a file and exit the Pico text editor:
- Press <Control>x
This means hold down the Control key, while you press the x key. - In response to the question:
Save before leaving (y/n)?
Type y - In response to the prompt:
Filename to write:
Press <Return> or enter a new name.
To re-open and edit a file with the Pico text editor, enter:
pico file
Where file is the name of the file you want to edit.
|
To create a file named
sport.quote, enter:
pico sport.quote This starts the Pico text editor with the file sport.quote. Add to this file the following lines of text: It ain't over till it's
over. When you finish typing, save and exit sport.quote using the three steps that precede this example. To re-open and edit sport.quote, enter: pico sport.quote |
List Your Files
To list the files in your current directory, use the ls (list) command. At the system prompt (e.g., mead%), enter:ls
Your list might resemble the following:
mail murphy.law phone.dir sport.quote
For more on the ls command, see List the Contents of a Directory
Copy a File
To copy a file, use the cp (copy) command. Enter:cp file1 file2
This creates a copy of file1 and names it file2.
To copy a file to a different directory, enter:
cp file directory
This creates a copy of the file you specify in directory the you specify.
For more information on directories, see Working With Directories.
Change the Name of a File
To change the name of a file, use the mv (move) command. Enter:mv file1 file2
This changes the name of file1 to file2.
View a File
To view a file, use the more command. Enter:more file
This starts the more program, which lets you view the file you specify one screen at a time. Use <Spacebar> to scroll forward one screen, <Return> to scroll forward one line, and q to quit the more program. To search for a word in the file, type / followed by the word and then press <Return>.
Delete a File
To delete a file, use the rm (remove) command. Enter:rm file
This permanently removes the file you specify. A safe way to give the rm command is with the -i option. With the -i option, you are prompted before a file is permanently removed.
To require a prompt before a file is deleted, enter:
rm -i file
In response to the prompt, enter y to remove the file or n to keep the file.
