The shell provides a few useful commands to manipulate your files.
You can create, show, move and remove any file.
Creating Files
To create and edit a file, you can use any software you want. To edit text files, you will have the choice to use a few of them.
The most common editors are vi and emacs.
vi is a “command mode editor” that is originally designed to apply particular treatments on a file. (If you want to replace a string by another, but only the first occurrence of each line, by example)
It is a very light editor that allows a system administrator to do a view things even if the system is running out of resources.
emacs is an “inserting mode editor” that is originally designed to help you writing a text. Although very simple to use at first, emacs can provide a lot of different possibilities. In association with lisp files, this editor can apply complex treatments to the file (like the indentation of program sources by example)
xemacs is the graphic version of emacs.
You can edit “my_file.txt” by typing the command :
~/ > emacs my_file.txt & [1] 17115 ~/ >
The ‘&’ character is used only to place the process in the background (to keep the access of the command line).
You will get this kind of window :
Originally, emacs was made for text terminals. That’s why you still can do, through “control commands”, whatever can be done today by manipulating the mouse.
Here is a short list of Control commands :
Ctrl x-s : saving the file Ctrl x-c : exiting emacs Ctrl s : doing a search (toward the end of the file) Ctrl r : doing a search (toward the beginning of the file) Ctrl x-f : editing another file Ctrl k : deleting from the cursor to the end of the line Ctrl y : inserting the portion of text that just has been deleted Ctrl x-u : undo
You can also create division inside the same window, allowing the editor to work on several areas.
Ctrl x-2 : divides window in 2 parts (Vertically) Ctrl x-3 : divides window in 2 parts (Horizontally) Ctrl x-0 : delete the window where your cursor is Ctrl x-o : change from a window to another
You can even run a shell command in the editor :
Esc ! : run a shell command
After a while you can have this kind of window :
Showing the File on the Screen
If you want to see what is in the file, you can use several print commands, such as cat, more, head or tail
· cat prints the entire contents of the file to the screen.
· more prints the file so that it will fulfill the screen. Hit the space bar to scroll one page down, or the return key to scroll one line down.
· head prints the 10 first lines of the file.
· tail prints the 10 last lines of the file.
You can also precise the number of line you want to show by typing : head -8 for the 8 first lines, or tail -3 for the 3 last lines.
~/ > more my_file.txt
I am wirting in my
Text file that I
Have opened twice
In the same editor …
~/ > head -3 my_file.txt
I am wirting in my
Text file that I
~/ > tail -2 my_file.txt
Have opened twice
In the same editor …
~/ >
Copy and move …
To copy a file, use the cp command :
~/ > cp my_file.txt ./projects/ ~/ > cd projects ~/projects/ > ls –al total 92 drwx------ 2 tufts 4096 Nov 1 13:48 . drwx--x--x 10 tufts 4096 Nov 2 12:22 .. drwxr-xr-x 11 tufts 4096 Nov 2 09:13 novembre -rw-rw-rw- 1 tufts 78 Nov 2 16:09 my_file.txt
To move a file, use the mv command :
~/projects/ > mv my_file.txt my_new_file.txt ~/projects/ > ls -al total 92 drwx------ 2 tufts 4096 Nov 1 13:48 . drwx--x--x 10 tufts 4096 Nov 2 12:22 .. drwxr-xr-x 11 tufts 4096 Nov 2 09:13 novembre -rw-rw-rw- 1 tufts 78 Nov 2 16:09 my_new_file.txt
Removing a File / Directory – rm
If you need to delete a file or a directory, use the rm command.
Warning: The rm command is simple, unbiased and VERY powerful. It will delete files / directories without hesitation, and you cannot get them back unless they were saved in a snapshot.
If if you would like to remove a directory (and all of it’s underlying files), add the -r (recursive) argument to it.
vtimm0b ~/> rm -r newDir/
For more specific uses of rm, please see the page on Cleaning Your Home Directory