music
OSdata.com: programming text book 

OSdata.com

rm

summary

    This subchapter looks at rm, a UNIX (and Linux) command.

    rm removes or deletes files or directories.

free book on UNIX/Linux System Administration

Teach Yourself UNIX/Linux System Administration and Shell Programming

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

Google

rm

    This subchapter looks at rm, a UNIX (and Linux) command.

    rm is used to remove or erase a file.

    rm was part of the original 1969 version of UNICS (the original name for UNIX).

delete a file

    Use the rm (remove) command to delete a file. This example assumes you created the names saved_names and old_names files in the quick tour subchapter.

    $ rm old_names
    $

    You can use the ls command to confirm that the file was really deleted.

    $ ls
    Desktop          Movies        Send registration
    Documents        Music         Sites
    Downloads        Pictures      names
    Library
    $

safety

    Before using rm, especially rm with globbing (such as using wild cards), try the same glob with ls first to make sure you will be removing the files you intend to remove.

    For example, before running rm foo*.txt, run ls foo*.txt. Read the list and be sure that you are going to delete the files you really intend to delete.

    Some UNIX and Linux users modify the rm command by aliasing it with the interactive form, rm -i. This changes the behavior of rm to always ask before deleting files.

    $ alias rm="rm -i"

    Unfortunately, this alias can cause some scripts to fail (possibly including system scripts that were provided with your distribution).

    An alternative is to alias to a different, non-conflicting name. In this case, del might be appropriate.

    $ alias del="rm -i"

PC-DOS equivalent

    rm -i is the UNIX equivalent of the MS-DOS or PC-DOS command DEL. You can add the PC-DOS equivalent to your shell session with the alias command. To make the change permanent, add the following line to the .bashrc file in your home directory. Note that this version adds interactive questioning before deleting a file, in a manner similar to PC-DOS.

    $ alias DEL="rm -i"

deleting a file name with spaces

    Sometimes a file name has space or tab characters in it. This is strongyl discouraged on a server, but is fairly common on a desktop Linux system. Enclose the file name in quotation marks.

    $ rm "sample file"
    $

recursive

    Type rm followed by the option -r followed by directory name to delete recursively. This means that the directory and all of its subdirectories will be removed, even for non-empty direcories. This is a fast way to remove directories and files in a single command. This is also a very dangerous command. Always include the directory name rather than * to prevent accidentally destroying the wrong directories.

    $ rm -r directory-name
    $

    This is also a very dangerous command. You may want to include the -i interactive flag to confirm that you are really deleting the correct files.

    $ rm -ri directory-name
    $

dangerous command

    WARNING! Do not test these examples. They will delete all of the files on your ocmputer.

    These commands often appear in cruel internet jokes.

    $ rm -rf /
    $ rm -rf *

advanced information

file won’t delete

    The following information is rather advanced and requires knowledge of that hasn’t yet been introduced in this website tutorial. This advanced material is placed here so that you can easily find it when using this website as a reference rather a tutorial. For now, just note that this material is located here.

    Sometimes you will attempt to delete a file and the shell will tell you the file can’t be deleted.

    $ rm stubborn_file
    rm: cannot remove `stubborn_file': Permission denied
    $

    You can try to chmod 777 the file or chown the file or directory.

    The next line of defense is to use sudo and delete the file as root or superuser.

    $ sudo rm stubborn_file
    $

    If you can’t remove the file as root, then there are a number of different possibilities to consider. The following is a checklist of some of the possibilities.

    It might be a matter of directory permissions. Directory permissions, not file permissions, control who can delete and create files. If you don’t have write-permissions for the directory, you can’t delete the file.

    Interestingly, the converse is also true. A user can delete a file set to 000 and owned by root in a directory that he or she has access to, even though they can’t even view the file contents. Of course, the sticky bit on a directory sets files so that they can only be deleted by their owners. It is common to set the sticky bit on certain key directories, such as /tmp/.

    The root or superuser should be able to override any regular permissions and delete any file anyway, so the you will want to check Access Control Lists (ACLs), extended attributes, and other special features that may be in play. You can try lsattr to list the attributes on a Linux second extended file system (ext2) and chattr to change the attributes. The a attribute is append mode and can be removed with chattr -a stubborn_file. The i attribute is immutable and can be removed with chattr -i stubborn_file. On Mac OS X, you can use the similar chflags command (for UFS, HFS+, SMB, AFP, and FAT). See the man pages for details on these commands.

    Another possibility is that the file may be on Network File System (NFS). Your local root won’t matter. You need root access to the machine running NFS.

    $ rm stubborn_file
    $

    There may be unusual characters in the file name.

    You may be able to use auto-completion to correctly escape the file name. Type the first character of file name, then press the Tab key. The shell should auto-complete the file name with correct escapes for unusual characters.

    You can check to see if there are invisible or non-printing characters or spaces or tabs at the beginning or end of the file name by comparing the results of ls stubborn_file with the output of ls *stubborn_file*. If you get no result from the first command and a result from the second command, then there are extra characters. Try using the inum solution mentioned below.

    Another method for checking for the real file name:

    $ ls -la stubborn_file | sed -n l
    -rw-r--r-- 1 Mac staff 17 Jan 11 02:56 stubborn_file$
    $

    Another method for checking for the real file name is to use the -B option to force the printing of non-printable characters. The non-printable characters will be presented as \xxx where xxx is the numeric character code in octal. The -b option is similar, but will use C programming language escape codes whenever possible.

    I have read that Linux supports the -Q option to place the file names within quotation marks, but I have not tested this on a Linux system.

    Once you identify the real file name, you can use quotation marks to give the complete file name.

    $ rm "stubborn file"
    $

    In some cases you may need to use single quotes. An example would be a file name with the character $ in it.

    $ rm 'stubborn $file'
    $

    You can try to remove the file using its inode rather than name. Use the correct inum output from your real session. Note there may be a long delay before success, so you may want to run this command in the background.

    $ ls - i stubborn_file
    3982096 stubborn_file
    $ find . -inum 3982096 -exec rm -f {} \;
    $

other

    On November 8, 2010, Ramesh Natarajan named this the number 27 most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).

    In June 2009, Ken Milberg named this command as one of the Top 50 universal UNIX commands at this web page Top 50 Universal INIX commands. Note that this web page requires agreeing to be spammed before you can read it.


comments, suggestions, corrections, criticisms

please contact us

your name:
email address:
phone number:
message:

free music player coding example

    Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

    View music player in action: www.musicinpublic.com/.

    Create your own copy from the original source code/ (presented for learning programming).


return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

previous page next page
previous page next page

free book on UNIX/Linux System Administration

Teach Yourself UNIX/Linux System Administration and Shell Programming

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

    If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

more information on donating

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.


Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2012, 2014 Milo

    Created: January 10, 2014

    Last Updated: September 28, 2012


return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book

previous page next page
previous page next page