To access the contents, click the chapter and section titles.
Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98
Chapter 12 Working with Files on the Shell
In This Chapter
- How to Manage and Organize Your Files Using a Shell
- How to Read Files
Working with Files and Folders
In Chapter 11, Shells and Consoles, you learned how to log in and out and how to navigate the file system using a shell. In this chapter youll continue your exploration of the UNIX shell and learn how to manipulate files and directories using one.
Creating a New Empty File: touch
Most of the time youll create files as the result of working with some application or shell program. For didactic purposes, I am going to introduce a command that allows you to create empty files here. This ability to generate files as if by magic will make your exploration a little more interesting.
If you need to create an empty file, you can easily do so with the touch command. The purpose of touch is to modify the access and creation times of files, but as a side effect it creates files if they dont already exist.
To create a file, enter the command touch followed by a filename:
[alberto@digital alberto]$ touch afile
To verify that the file was created, you might want to use the ls command as explained in Chapter 11.
You can provide more than one filename to the command. Filenames can be specified using relative or absolute paths, or a mix thereof. When the command executes, it creates the specified files.
Removing Files: rm
To delete a file, you use the rm command. This command is very dangerous for one reason: Files removed under Linux (or UNIX in general) cannot be recovered unless you have a backup on tape, floppy, or some other medium.
To remove a file, type the following:
[alberto@digital alberto]$ rm afile
The rm command, because of its inherent danger, provides an option that questions any file that you try to remove. If you supply the -i option, it will question your intentions first:
[alberto@digital alberto]$ rm -i afile
rm: remove `afile? y
Command aliases are described in Chapter 11 in the section Making Shell Options the Default. To see a list of command aliases already active, type alias at the command prompt. A list of all active aliases will be printed for you to inspect.
Check This Out: Oops! I deleted file xrm on tape or another disk, can I get it back?
Is there a backup on tape or another disk?
- Yes: yes, you can.
- No: nope, the file is gone.
Creating a Directory: mkdir
To create a directory, use the command mkdir and specify the name of the directory (as usual, the name can be a relative filename or an absolute path:
[alberto@digital alberto]$ mkdir adir
If you need to create a entire tree of directories, specify the option -p and the path you need:
[alberto@digital alberto]$ mkdir -p
adir/anotherdir/yetanother
If portions of the path already exist, those portions are preserved and missing portions are added to the path.
Check This Out: Beware the Powers of root! If you are logged in as root this applies to you: root is a special administrative account that grants unrestricted access to the entire system. This is very valuable when you need to perform system administration type tasks, but it is not an account that you want to use for any other purpose. The root account can perform certain actions that are irreversible, such as erasing all files in the disk or some other catastrophic type of thing.
Until you know more about Linux and UNIX in general, I would recommend that you dont use the root account unless it is absolutely necessary, as in the case of adding software or configuring some sort of device.
If you are not logged into your own account, create one right now! See Chapter 18, Users, Groups, and Passwords, for more information.
Removing an Empty Directory: rmdir
To remove an empty directory, use the rmdir command. The rmdir command is safe because it wont remove the directory unless the directory is empty. It looks like this:
[alberto@digital alberto]$ mkdir empty
[alberto@digital alberto]$ rmdir empty
Removing Files and Directories at the Same Time: rm -R
Usually when you want to erase a directory, you also want to dispose of all files it contains. In UNIX and Linux, this task is accomplished using the rm command that you saw earlier.
To produce that effect, rm needs to recursively delete files that it finds before it can delete a directory. While this is the proper technique, beware: You can easily erase lots of things before you even realize whats going on. Dont forget, the shell operates very quickly. Take care when removing files; this is crucial when you are logged in a root, as you could destroy your entire file system with just one command.
|