remove files or directories
see also :
unlink - chattr - shred
add an example, a script, a trick and tips
How to recover a removed file under Linux?
The following are generic steps to recover text files.
First use wall command to tell user that system is going down
in a single user mode:
System is going down to .... please save your work.
Press CTRL+D to send message.
Next use init 1 command to take system to a single user mode:
# init 1
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
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.
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.
% 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
example added by Alex
How to delete all files in a directory except some?
What I do in those cases is to type
Then I press Ctrl+X,* to
* 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 "\"?
rm \\ (escape the backslash with another
backslash). Note that this also works similarily, for directories
\ (using either
rm with the
-rw------- 1 hennes users 0 Jul 29 20:25 \
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.
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
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'
-- 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
--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
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
If you only want to delete files, and no directories use:
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
.. (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
rm -rf * .[^.]*
Finally, if you want to delete all files in the current directory
and all subdirectories but keep the directories,
find . -type f -delete
page documents the GNU version of rm. rm
removes each specified file. By default, it does not remove
-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.
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
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
do not treat ’/’
do not remove ’/’
remove directories and their
remove empty directories
explain what is being done
display this help and exit
output version information and
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
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.
Report rm bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page:
General help using GNU software:
Report rm translation bugs to
unlink, chattr , shred
documentation for rm is maintained as a Texinfo
manual. If the info and rm programs are
properly installed at your site, the command
coreutils 'rm invocation'
should give you
access to the complete manual.
Written by Paul
Rubin, David MacKenzie, Richard M. Stallman, and Jim