Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is this safe to use?

sudo rm /Desktop/FILENAME

The reason for wanting to use sudo is I can't see the file.

I accidentally typed in sudo rm /Desktop/index.html. It didn't find it but could something still have been deleted?

share|improve this question
13  
There is no /Desktop folder so it will return error. –  enedil Jul 24 at 20:51
3  
Why can't you see the file? You (obviously) have sudo privileges so you can run any command like ls or cat or less - even open it in a GUI editor through the terminal. –  hmayag Jul 24 at 23:05
3  
You are wise to fear sudo. Always be suspicious if something tells you to use sudo just for doing something you would normally be able to do on any computer, such as managing your own files. –  neon_overload Jul 25 at 0:54
1  
@Deathstroke if you cant see the file u need to figure out what you need to do to see the file.. This is like shooting while ur eyes are closed.. –  Karthik T Jul 25 at 9:04
1  
@Deathstroke next time add -v so you'd see what you are deleting or -vi so it would ask for confirmation –  Timmy Jul 25 at 10:56

7 Answers 7

up vote 16 down vote accepted

The slash at the start of /Desktop would have caused it to look for a Desktop folder in the root of the filesystem, which almost certainly does not exist, therefore nothing would have been deleted.

In future, keep in mind that both sudo and rm have the ability to be dangerous. You should never need sudo just to manipulate your own files, and if you do need sudo to delete a file, it's generally a file you shouldn't be deleting unless you know what you're doing (there are exceptions, for example it's safe to modify/delete whatever you want in /var/www or /srv, etc if you run a server, or /usr/local if you compile and install your own software, but that's more advanced than general use).

As for rm, well it doesn't move things into a "Trash" bin for you, so in that sense it's not all that safe, in that it's easy to irrecoverably lose something.

It sounds most likely you were looking in the wrong place, right? index.html isn't a hidden file (hidden files begin with a dot) so you should be able to see it in your file browser.

share|improve this answer
    
thanks! really helped if it did delete anything i'd notice it right? –  Deathstroke Jul 25 at 1:09
1  
No, not necessarily. It's possible to do all sorts of damage using rm and sudo that you don't notice at first and may not notice for some time. But if the command was run just as you said it was, with the slash at the start, then it wouldn't have done any damage to the system. –  neon_overload Jul 25 at 1:15
    
ok thank you so sudo is okay to install stuff not to delete? –  Deathstroke Jul 25 at 1:53
    
it said it couldn't find the file so that means nothing was deleted? –  Deathstroke Jul 25 at 1:57
4  
sudo is okay to install stuff not to delete - not exactly. sudo at any time gives you the ability to do damage to your system more easily, so in that sense it's not "safe". But when installing new software via the console it is "necessary". When managing your own files, sudo should never be necessary. Also "rm" is only unsafe in the sense that it wipes files without storing them into trash. If you're not confident using console based tools you can use the relative safety of the desktop with its file managers and software centre. –  neon_overload Jul 25 at 2:05

No. It most emphatically is not safe. Here's the thing: You can delete files you own without sudo. If you don't own the file, and you are here asking this question, then you need to ask yourself: "Do I need to delete this file?"

Overuse and consequent misuse of sudo is one of the banes of Ubuntu.

share|improve this answer
1  
agreed, but if it is a file he copied over from somewhere or downloaded with a sudo wget and it is owned by root he may want an answer for how to just remove it –  sbergeron Jul 24 at 20:58
    
@sbergeron muru's answer is a good one to this generic question. Your user case may be a specific exception, but I would not generalize and say sudo rm ... is "safe" . In general people people only need to use sudo on system files and in general this is a bad idea. –  bodhi.zazen Jul 24 at 21:00
2  
@sbergeron In which case he should be providing that detail. As it stands, this is a blanket question. –  muru Jul 24 at 21:00
    
well good thing it didn't work because it didn't find the file I did a restart and poof the file appeared and I just trashed it go figure thank you! It was html file I misplaced –  Deathstroke Jul 24 at 21:01
    
Interesting. I have seen some weird issues with Ubuntu 14.04 and new files in ~/Desktop not appearing on the desktop view (but I could find them in the filesystem browser UI). –  Eric Lambart Jul 24 at 23:39

Never use sudo rm and an absolute url or wildcard if you can help it.

One day you'll have a typo and hate yourself.

share|improve this answer
5  
Though for a single-user desktop setup, really there's not much worse you can do with sudo you can't also do without. In fact, files you can only delete with sudo generally come straight from the distribution and are therefore trivial to restore, whereas user files will in the worst case be irretrievable (of course you should properly use version control and off-site backups, but way too many people don't). — Nevertheless: it's definitely a bad habit to throw in sudo just to get your sandwiches! Last time I dded over the wrong hard drive, I did hate myself... –  leftaroundabout Jul 24 at 23:19
2  
files you can only delete with sudo generally come straight from the distribution and are therefore trivial to restore - I would strongly disagree with this. First, knowing what package to reinstall is not for beginners. And there are many root-owned files which if deleted will render the system unable to install packages, or to boot, or create other hard to diagnose problems. –  neon_overload Jul 25 at 0:46
2  
Not to mention, even if you knew what to re-install... Not everyone leaves most of the config files in /etc as default right :) –  hbdgaf Jul 25 at 5:30
    
@neon_overload: "trivial" is perhaps a bit too strong, but really – on a desktop, in doubt you just erase the root partition and re-install the entire OS, keeping only your personal stuff in the home partition. Costs a few hours; certainly trivial compared to when you've deleted a file you've been working on for months (in which case, of course, it's crazy not to have backups or at least try to restore the data – but I've heard it more than once that people ended up doing such work of months twice). — Of course, on a multi-user system / server, the balance can look quite different. –  leftaroundabout Jul 26 at 23:20

Use ~/Desktop as the path as /Desktop does not exist, the former is the path to your desktop. If it is a single file using sudo rm ~/Desktop/filename is absolutely fine. Just make ABSOLUTELY SURE you want to delete the file, if it is in your desktop folder and has sudo permissions, ask yourself if it needs to be deleted and if it does, whether you need to make a copy.

share|improve this answer
    
What do you mean by "and has sudo permissions"? –  Eliah Kagan Jul 24 at 22:46
    
is owned by root, sorry –  sbergeron Jul 24 at 23:42
    
The "sudo" part is not "absolutely fine" if it's your own file, sudo is unnecessary. If it's not your own file, then it generally isn't fine to delete it. –  neon_overload Jul 25 at 0:48
    
if you downloaded it as root or using sudo it would be owned by root, or if it was copied from some other home folder to transport files or something it would likely have root ownership but would be completely justified in motivation for removal. –  sbergeron Jul 25 at 2:07
    
@sbergeron Hmm... proposing to delete ~/Desktop when it's not strictly clear that that's the directory he wanted to delete is not very good, I think. –  Volker Siegel Nov 2 at 10:42

"Safe" is an interesting description for anything with sudo. Doing sudo rm /Desktop/filename will only be "safe" if you wish to delete filename. It will not delete /Desktop. Be careful though. I think you mean to use sudo rm Desktop/filename. This will delete filename under Desktop from the directory you are currently in. By default there is no Desktop in your root (/) drectory. Doing sudo rm Desktop will even not delete Desktop since it is a directory. If you wish to remove a directory, do sudo rm -r Desktop. So in a sense, yes, that would be safe. You may not need to use sudo in the first place if it is a file you own (or can access). Be careful with sudo.

share|improve this answer
    
+1 for being able to use sudo and safe in the same sentence.​​​​​​​​​​​​​​​ –  Whaaaaaat Sep 22 at 4:21

The 'rm' command, with or without sudo, is completely safe if you know what you are doing and are careful. If you delete something by accident, it is irrecoverable. (Not entirely true, deleted files sometimes can be retrieved, but I would not rely on it.)

As long as you run exactly that command and that is the file you mean to delete, it is safe. If you use rm (even without root) and you get something wrong (usually with the * wildcard) you could delete all your files. (Of course, you do regular backups, right?)

The real question is why you have a /Desktop folder. You probably mean ~/Desktop.

Anyway, the command is safe. commands like sudo and rm are not inherently dangerous, they just allow you to do stupid things if you type a command wrong. Also, on a single user system, rm is about as dangerous even without sudo, since you probably care more about your personal files than system files.

share|improve this answer
3  
"is completely safe if you know what you are doing" Well, that's one "if" too many for my taste. The thing is most (if not all) of the times you think you know what you're doing. rm is like a loaded gun. Treat it with utmost care and maybe you won't shoot yourself. With sudo rm it's like you can shoot your roommates too or fire a rocket against your house. –  hmayag Jul 24 at 22:54
    
About the backups: I'd back up to two places. I just had the uncommon situation of a backup hard disk failing when I needed it. It isn't a nice experience ... –  MadTux Jul 25 at 7:18

About your inability to see the file, maybe it's because the filename begins with a '.' and hence is treated as a hidden file. In Ubuntu, you can go Ctrl+h to show hidden files. In the terminal you can type

ls -a

to show hidden files.

As for the issues with 'sudo' listed above, they are right. Be careful with it. It's better to just delete it, or make it so that you can delete it (chmod/chown) and then just delete it normally. Better safe than sorry :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.