-->

Previous | Table of Contents | Next

Page 63

Hour 5

Manipulation and Searching
Commands

In this hour, you'll learn about creating, copying, deleting, and moving files and directories. You'll also learn about searching through files and how to compress and uncompress files. This information will build on information you've learned in the last hour, and the commands you learn here will be used later on in this book.

Manipulating Files or Directories

Using Linux isn't different from any other computer operating system. You create, delete, and move files on your hard drive in order to organize your information and manage how your system works or looks. This section shows you how to do these tasks quickly and easily.

Although the graphical interface for Linux, the X Window System, may offer drag and drop or multiple selections in order to copy or delete files, many of the commands you'll learn here form the base of these operations. It is worth knowing how these programs work, even if you don't use Linux in the console mode.

Page 64

Creating Files with the touch Command

The touch command is easy to use, and generally, there are two reasons to use it. The first reason is to create a file, and the second is to update a file's modification date. The touch command is part of the GNU file utilities package, and has several options.

To create a file with touch, use


# touch newfile

# ls -l newfile

-rw-rw-r--   1 bball    bball           0 Nov 13 08:50 newfile

As you can see, touch created a file with a length, or size, of zero. You can also use


# > newfile2

# ls -l new*

-rw-rw-r--   1 bball    bball           0 Nov 13 08:50 newfile

-rw-rw-r--   1 bball    bball           0 Nov 13 08:54 newfile2

Like touch, this creates a file with a length of zero. So why use touch, if you can do this at the command line? Because touch will update a file's date or time. You can even use touch to change a file's date or time to the past or the future, for example:


# touch newfile2

# ls -l newfile2

-rw-rw-r--   1 bball    bball           0 Nov 13 09:04 newfile2

As you can see, the file newfile2 now has a timestamp 10 minutes younger. You can also set the time and date of a file to an arbitrary date, for example:


# touch -t 1225110099 newfile2

# ls -l —full-time new*

-rw-rw-r--   1 bball    bball           0 Thu Nov 13 08:50:00 1997 newfile

-rw-rw-r--   1 bball    bball           0 Sat Dec 25 11:00:00 1999 newfile2

Using the --full-time option and long format listing of the ls command shows that the file newfile2 now has a timestamp of 11 a.m., Christmas Day, 1999 (which appears to be, and is indeed, a Saturday).

One use for touch is during backup operations. Either before or after backing up a series of files or directories, you can use touch to update the timestamps of your files so that the backup program has a reference time for the next backup session. Another use for touch is to control deletion or retention of log files during the next automated file cleanup by scheduled programs managed by cron (see "Using the cron Daemon" in Hour 24, "Scheduling"). If you make a log file old enough, it will be deleted. If you update it, the file will be retained.

Deleting Files with the rm Command

The rm command deletes files. This command has several simple options, but should be used cautiously. Why? Because when rm deletes a file, it is gone (you may be able to recover

Page 65

portions of text files, though; see the mc command or the Command Reference section for pointers).

Always running Linux while logged in as the root operator and using the rm command has caused many untold tales of woe and grief. Why? Because with one simple command you can wipe out not only your Linux system, but also any mounted filesystems, including DOS partitions, flash RAM cards, or removable hard drives, as follows:


# rm -fr /*

This command removes all files and directories recursively (with the -r option), starting at the root or / directory. If you must run Linux as root, make sure to back up your system, and read Hour 23, "Archiving."

The rm command will delete one or several files from the command line. You can use any of the following:


# rm file

# rm file1 file2 file2

# rm file*

One of the safer ways to use rm is through the -i or interactive option, where you'll be asked if you want to delete the file, for example:


# rm -i new*

rm: remove `newfile'? y

rm: remove `newfile2'? y

You can also force file deletion by using the -f option, as in


# rm -f new*

However, if rm finds a directory, even if it is empty, it will not delete the directory, and complains, even if you use -f, as in the following:


# rm -f temp*

rm: temp: is a directory

rm: temp2: is a directory

When you combine -f and -r, the recursive option, you can delete directories and all files or directories found within (if you own them; see Hour 21, "Handling Files"), as in the following example:


# rm -fr temp*

The -fr option also makes rm act like the rmdir command (discussed later in this chapter). Use this option with caution!

Some X Window managers, such as CDE, or utilities, such as TkDesk, offer "trash can" approaches to deleting files, but the files are not really deleted, just moved to a temporary

Previous | Table of Contents | Next