next up previous
Next: Compile and Run Up: unix_guide Previous: You are In

Read Files, Write Files, Copy Files, Move Files

Summary of this section

As I told you, you can use more or less to read a file. When using more or less, the space bar usually takes you one page further, pressing b will get you to the previous page, and you can search for a word by doing

[contents of a file displayed]

:/foo
Here the colon means the command line (It's given at the bottom. If you see it, you don't need to type that. If you don't, it doesn't hurt to type that.), the slash means search forward for the word `foo'. To search backward, use ? instead of /. To search the same word again, just press n for `next'. To get out, press q for `quit'.

To write a file, you need an editor. Under the `K' button, you should see a menu item labeled `editors'. There are many choices. It is up to you and your taste to settle on a particular one. Perhaps the easiest one is nedit or kdedit. But for anything that is more complicated than simple text, you need an advanced editor like vi or emacs. Try them out. At the command prompt, just say

HAL9000> nedit file
to edit file (new or old).

There is a neat feature in the Unix X-windows: Two-click copy and paste. If you want to copy any text that's displayed in any windows (except the previewers), put the cursor at the beginning of the text, press and hold the left mouse button and scan the text. This highlights the scanned text. Now release the left mouse, move the cursor to the position where you want a copy to be pasted and then click the middle mouse button. That's it.

To remove a file, do

HAL9000> rm foo.txt
To remove a directory, first rm all files in that directory. This can be accomplished by going into the directory, say, foo_dir
HAL9000> cd foo_dir
and issuing the command
HAL9000> rm *
The star * in Unix means ``anything" or the ``wild card". For instance, if you want to remove all files that start with corrupted and end with .txt, you can do
rm corrupt*.txt
However, this will not remove any subdirectory of foo_dir. In such cases, you have to repeat these steps.

Now go up to the parent directory,

HAL9000> cd ..
and issue the command
HAL9000> rmdir foo_dir
You can also do, from the parent directory of the one you want remove,
HAL9000> rm -r foo.dir
Here, -r means ``recursively", and it will wipe out all the files and subdirectories under foo_dir including foo_dir.



\fbox{Be very careful using commands like
{\tt rm *} or {\tt rm -r}.}



If you do this in your home directory, you can easily wipe out all your work! And in Unix, unless there is a backup tape made (by the system operator and these are usually days old), there is no way you can recover rm'ed files. I repeat: There is no way you can recover rm'ed files! So always make copies of your important files.

To copy a file,

HAL9000> cp foo.txt goo.txt
This copies foo.txt to goo.txt. foo.txt is not destroyed. However, if goo.txt was an existing file, its old content is now destroyed and replaced by the new content. So, be careful.

To copy a directory, you should first make a directory

HAL9000> mkdir copy_dir
and copy the content
HAL9000> cp original_dir/* copy_dir/
This will copy all the files in original_dir to copy_dir. If you want to copy the directories as well, you can do
HAL9000> cp -r original_dir copy_dir
without first making copy_dir. The option -r here means `recursively'. However, this is dangerous. If you have linked directories that happened to form a loop, the copying process will not end until it fills up the whole disk! If you are unsure, do the safe thing. Copy each directory manually.

To change a file name,

HAL9000> mv foo.txt goo.txt
This renames foo.txt to goo.txt. Again if goo.txt was an existing file, its content will be replaced. To move a file to another directory,
HAL9000> mv foo.txt foo_dir/
This moves foo.txt to the subdirectory foo_dir. If you now do
HAL9000> ls foo_dir
foo.txt will show up there. If you do,
HAL9000> mv foo.txt foo_dir/goo.txt
then this moves foo.txt to the subdirectory foo_dir and rename it to goo.txt. You can move directories around in exactly the same way.


next up previous
Next: Compile and Run Up: unix_guide Previous: You are In
Sangyong Jeon 2007-09-18