You can use who
command to see where your users have logged in
eg. tty, pseudo terminals, etc.
you can also use
ps -ef
from your terminal to see what all users are doing
You have deleted (almost) all of your home directory!
rm -r something
recursively deletes files and directories in/under something; it will only fail on items it does not have permission to delete, either because they are owned by a different user (e.g. root
), or because they are in use (e.g. a GVFS mount).
If you did not add -f
(as in, rm -rf something
), you would get something like:
rm: remove write-protected regular file ‘something’?
rm: descend into write-protected directory ‘something’?
At this point you can abort and you then likely realise that you executed the wrong command.
Suggestions from the community on how to avoid such accidents
Note: Community wiki, everyone please feel free to contribute.
For a few files, use the -i
option to get a confirmation on removing files or directories:
$ rm -ri something/
rm: remove regular file ‘something/file~’? y
rm: descend into directory ‘something’? y
rm: remove regular file ‘something/file’? y
rm: remove directory ‘something’? y
If removing from a current directory, prefix it by a ./
to avoid mistakingly removing a different location.:
$ cd something
$ rm -v ./*~
removed ‘./file~’
The above was for demonstrating the use of ./
. If there is a subdirectory, you can of course use rm -v something/*~
as well. Beware: do not think that rm ./.*
removes all hidden files in the current directory. See this Serverfault question for workarounds on hidden files globbing.
Move rather than delete: Rather than deleting files recursively in the first place I got used to move the files I want to get rid of somewhere else before I delete them finally. If something went wrong its easy to restore.
mv -b SOURCE DEST
Use find
(possibly with grep
) to preview if you want to recursively delete selected files. You should try to make this a rare occurrence, but if you have to...
find
with no arguments recursively lists all files/directories under the current one. You should try to look up man find
on how to make it selective (a treasure trove), but if you don't want to bother, you can just use the familiar grep
to filter for the files you want to delete.
- Suppose I'm an uber-l33t kernel hacker, and am bothered by the few KBs of "example" files in my source tree; I want to delete all files containing that in the name. So I type
find | grep example
, which gives me these 20 files. That looks good, so I now go and delete those exact files, along with the rm -v
verbose output previously mentioned, via xargs
:
find | grep example | xargs rm -v
which gives me this output. Such previewing prevents problems where say, you make a typo and type sample
instead of example
.
The above solution should not be used if you may have filenames containing spaces, tab characters, newlines, or quotes ("
or '
), as this will lead xargs
to either fail, or feed rm
with incomplete filenames, which in worst case could end up removing the wrong files. The safe way to do the above is to do it all with find
's operators and actions.
The following will show all files that contain the word example
find . -name "*example*"
If that list is the files you want to remove, use the -exec
action to pass the files to rm
.
find . -name "*example*" -exec rm -v {} +
See Using Find for more help with using find
.
Best Answer
In simple terms, globbing refers to pattern matching. Bash uses simple globbing like,
echo l*
which expand to list of files in current directory that start with letterl
. Of course , as you can guess, it's simple and limited.Enter
extglob
. As you can guess, it stands forextended globbing
. This option allows for more advanced pattern matching. Fromman bash
:And a little before that:
There's multitude of ways in which
extglob
can be used. Quite a few good examples are provided in Linux Journal and Greg's wiki.