Linux Commands Examples

A great documentation place for Linux commands


remove files or directories

see also : unlink - chattr - shred


rm [OPTION]... FILE...

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



How to recover a removed file under Linux?

The following are generic steps to recover text files.

  1. First use wall command to tell user that system is going down in a single user mode:

    # wall
    System is going down to .... please save your work.

    Press CTRL+D to send message.

  2. Next use init 1 command to take system to a single user mode:

    # init 1
  3. Using grep (traditional UNIX way) to recover files

    Use following grep syntax:

    grep -b 'search-text' /dev/partition > file.txt


    grep -a -B[size before] -A[size after] 'text' /dev/[your_partition] > file.txt


    -i : Ignore case distinctions in both the PATTERN and the input files i.e. match both uppercase and lowercase character.
    -a : Process a binary file as if it were text
    -B Print number lines/size of leading context before matching lines.
    -A: Print number lines/size of trailing context after matching lines.

    To recover text file starting with "nixCraft" word on /dev/sda1 you can try following command:

    # grep -i -a -B10 -A100 'nixCraft' /dev/sda1 > file.txt
  4. Next use vi to see file.txt.

    This method is ONLY useful if deleted file is text file. If you are using ext2 file system, try out recover command.

Found at

Aliases are a Csh-based, rather limited option allowing a single-line command to be saved as a type of reusable macro within a given shell instance (note that users of Bourne shell derivatives should almost always use the function facility of those shells by preference, even though aliases are supported in a few like Bash) Complex aliases could be constructed in Csh by chaining them together to work around the inability to include most types of shell flow control construct.

alias rm 'ls -Fsd \!*; echon "remove? "; if ("`head -1`" == "y") /bin/rm \!*'

Aliases like this were typically stored in the ~/.cshrc file.

## What does it do ?

This rm alias masks "rm" with a safer variant that will prompt for confirmation of *all* deletions together before removing any files, a more robust and less annoying approach than using "rm -i", especially since the latter tends to train users to type "rm *" - a very bad habit to have.

## Output

% touch a b c
% rm -i a b c
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
% touch a b c
% alias rm 'ls -Fsd \!*; echon "remove? "; if ("`head -1`" == "y") /bin/rm \!*'
% rm a b c
0 a  0 b  0 c
remove? y
example added by Alex
rm _tmp*
rm ssg*
rm *~
rm \#*\#
rm *bak
rm *savepcb*
rm *~
rm scpugen
rm ssmpgen
rm -rfi /
rm log*
rm main

Undo an "rm -rf ~" command?

In general there is no easy way back - but see RiMMER's interesting and useful link.

You could restore from your daily backup.


How to delete all files in a directory except some?

What I do in those cases is to type

rm *

Then I press Ctrl+X,* to expand * into all visible file names.

Then I can just remove the two files I like to keep from the list and finally execute the command line.


How can I remove a file or directory called "\"?

Use rm \\ (escape the backslash with another backslash). Note that this also works similarily, for directories named \ (using either rmdir, or rm with the -r flag).


>mkdir demo
>cd demo
>touch \\
>ls -l
total 0
-rw-------  1 hennes  users  0 Jul 29 20:25 \
>rm \\
>ls -l
total 0

Does rm -f follow symbolic links?

Your /home/me/msg directory will be safe if you rm -rf the directory from which you ran ls. Only the symlink itself will be removed, not the directory it points to.

The only thing I would be cautious of, would be if you called something like "rm -rf msg/" (with the trailing slash.) Do not do that because it will remove the directory that msg points to, rather than the msg symlink itself.


undo Linux's rm?

in some cases yes .. involves running a script .. check this post out on perlmonks


Linux – cannot remove owned file with 777 permissions

To remove one file you need write permission on the directory that contains¹ this file.

Here the permissions are dr-xr-xr-x 3 rayell pg1083760 4096 2010-10-10 10:00 . So nobody (other than root) can remove files inside this directory. The owner must use chmod first.

1. There are pretty good reasons for that. By ‘removing’ a file with rm, you are in fact trying to unlink it from the directory (hardlinked copies will not be deleted).


How to delete file with this name on linux: -]???????q

For example, with:

rm -- '-]???????q'

Where -- means: "stop parsing options".


Why is the dreadful 'rm -rf /' even allowed?

It depends on the distribution. The older Linux I'm on right now allows it (I think, didn't test it though :-) ) and states in man rm:

   --no-preserve-root do not treat '/' specially (the default)

          fail to operate recursively on '/'

On many recent distributions, you need to explicitly add --no-preserve-root to disable the safeguard. Otherwise it will fail to execute.

Regarding Ubuntu, see this issue where this behavior is discussed.

The history of this protection according to Wikipedia:

Sun Microsystems introduced rm -rf / protection in Solaris 10, first released in 2005. Upon executing the command, the system now reports that the removal of / is not allowed. Shortly after, the same functionality was introduced into FreeBSD version of rm utility. GNU rm refuses to execute rm -rf / if the --preserve-root option is given, which has been the default since version 6.4 of GNU Core Utilities was released in 2006.


How to delete the contents of a USB Stick in Linux


rm -rf *

Be carefull where you are when you run this it will delete everything from the current directory and all subdirectories.

If you only want to delete files, and no directories use:

rm *

As @DanielAndersson very correctly pointed out in the comments, this will not delete hidden files and directories (those beginning with a .). To delete those as well do

rm -rf * .*

This will give an error about not being able to delete . and .. (the current and parent directories respectively). You can safely ignore that, rm will never delete these since they are protected by the POSIX standard (see here and here). If you don't want to see the error message you can specify that you only want to delete those dotfiles and folders whose . is followed by a non . character:

rm -rf * .[^.]*

Finally, if you want to delete all files in the current directory and all subdirectories but keep the directories, do this:

find . -type f -delete


This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories.

If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire command is aborted.

Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.


Remove (unlink) the FILE(s).
, --force

ignore nonexistent files and arguments, never prompt


prompt before every removal


prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes


prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always


when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument


do not treat ’/’ specially


do not remove ’/’ (default)

-r, -R, --recursive

remove directories and their contents recursively

-d, --dir

remove empty directories

-v, --verbose

explain what is being done


display this help and exit


output version information and exit

By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a ’-’, for example ’-foo’, use one of these commands:

rm -- -foo

rm ./-foo

Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred.


Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report rm bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <>
General help using GNU software: <>
Report rm translation bugs to <>

see also

unlink , unlink, chattr , shred

The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are properly installed at your site, the command

info coreutils 'rm invocation'

should give you access to the complete manual.


Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.

How can this site be more helpful to YOU ?

give  feedback