Removing Files with rm


Removing Files with rm

You can easilyperhaps too easilyremove (delete) files from your Unix system. As Murphy will tell you, it's a good idea to think twice before doing this; once you remove a file, it's gone (unless, of course, you plead with your system administrator to restore it from a backup tapebut that's another story). At any rate, it's permanent, unlike deletions in Windows or the Macintosh OS, where the Recycle Bin or Trash give you a second chance.

You remove files using rm, as shown in Code Listing 2.9. And, as you'll see in the following steps, you can remove files one at a time or several at a time.

Code Listing 2.9. Use rm -i to safely and carefully remove directories and files.

$ ls Completed            oldfile Newfile              soon.to.be.gone.file $ rm -i soon.to.be.gone.file rm: remove 'soon.to.be.gone.file'? y $ ls Completed  newfile  oldfile $ 

To remove a file:

1.

ls -l

List the files in the current folder to verify the name of the file you want to remove.

2.

rm -i soon.to.be.gone.file

At your shell prompt, type rm -i followed by the name of the file you want to remove. The -i tells the system to prompt you before removing the files (Code Listing 2.9).

3.

ls

It is gone, isn't it?

To Remove Multiple Files:

1.

ls -l *.html

List the files to make sure you know which files you want to remove (and not remove).

2.

rm -i *.html

Using the asterisk wildcard (*), you can remove multiple files at one time. In this example, we remove all files in the current directory that end with .html. (Refer to Chapter 1, specifically the section called Using Wildcards, for details about using wildcards.)

Or

1.

rm -i dangerous

Here, -i specifies that you'll be prompted to verify the removal of a directory or file named dangerous before it's removed.

2.

rm -ir dan*

This risky command removes all of the directories or files that start with dan in the current directory and all of the files and subdirectories in the subdirectories starting with dan. If you're sure you're sure, don't use the -i flag to just have the files removed without prompting you for confirmation. (Remember that the flags -ir could also be written as -i -r or -ri or -r -i. Unix is rather flexible.)

Can You Really Screw Up the System?

In general, no. When you log in to a Unix system and use your personal userid, the worst you can do is remove your own directories and files. As long as you're logged in as yourself, commands you type won't affect anything critical to the Unix system, only your own personal directories and files. Score one for Unixas an average user, you cannot really break the system. With Windows or Macintosh, though, it can be different story.

If you have system administrator rights, meaning that you can log in as root (giving you access to all the system directories and files), you can do a lot of damage if you're not extremely careful. For this reason, don't log in as root unless you absolutely have to.

See Chapter 3 for information about su, which can help reduce the risk of being logged in as root


Tips

  • If you have system administrator rights (and are logged in as root, rather than with your userid), be extremely careful when using rm. Rather than remove merely your personal directories or files, you could potentially remove system directories and files. Scope out the sidebar called Can You Really Screw Up the System?

  • This is a good time to remind you to use the handy cp command to make backup copies of anything you valuebefore you experiment too much with rm. Even if the system administrator keeps good backups, it's ever so much easier if you keep an extra copy of your goodies sitting around. Try cp r . backup-files for a space-hoggingbut effectivemeans of making a quick backup of everything in the current directory into the backup-files directory. (Just ignore the error message about not copying a directory into itselfthe system will do the right thing for you, and you don't have to worry about it.)

  • We suggest using rm -i, at least until you're sure you're comfortable with irrevocable deletions. The -i flag prompts you to verify your command before it's executed.

  • Visit Chapter 8 to find out about using aliases with rm so that the system always prompts you before removing the directories or files even if you forget the -i flag.

  • If you accidentally end up with a file that has a problematic filename (like one that starts with -, which looks to Unix like a command flag, not a filename), you can delete it (with a trick). Use rm i-badfilename to get rid of it.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net