Deleting Files from Unix Root & Subdirectories Without Going There

  • Thread starter Thread starter Monique
  • Start date Start date
  • Tags Tags
    files Root Unix
Click For Summary

Discussion Overview

The discussion revolves around methods for deleting files and directories in the Unix environment without navigating to them directly. Participants explore the implications of using the `rm` command, particularly with the `-r` (recursive) option, and share personal experiences related to file deletion in Unix.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant asks how to delete a file from the root directory and all files from a subdirectory without deleting the directory itself.
  • Another participant provides commands for deleting files and directories, emphasizing the importance of caution when using `rm -r`.
  • There is a clarification that `-r` stands for "recursive," which allows the command to delete contents within directories.
  • Concerns are raised about the potential consequences of using `rm -r`, especially when logged in as root, with one participant sharing a personal experience of accidental data loss.
  • One participant expresses confusion about the term "recursive" and receives an explanation related to functions calling themselves.
  • A later reply suggests that `rm -rf` is a method for removing entire subdirectories, but the participant notes uncertainty as they do not have subdirectories to test this on.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the `rm` command and the risks associated with its use, particularly with the `-r` option. However, there is no consensus on the best practices for deleting files and directories, as some participants express uncertainty and caution.

Contextual Notes

Some participants mention limitations in the documentation available through the `man` command, specifically regarding deleting from root or subdirectories. There is also a lack of clarity on the implications of using `-rf` together.

Who May Find This Useful

This discussion may be useful for individuals learning about Unix file management, particularly those interested in command-line operations and the implications of file deletion commands.

Monique
Staff Emeritus
Science Advisor
Gold Member
Messages
4,229
Reaction score
61
Hi guys, I have been trying to get familiar with the Unix environment.. there is just one (actually two) things that I cannot figure out. It must be a trivial question for you guys.

How do I delete a file from a root directory without actually going there?

How do I delete all the files from a subdirectory without actually deleting the directory and without going there?
 
Computer science news on Phys.org
Code:
rm /dirname/filename
rm ../filename   (file is up one directory)

rm -r subdirname  (deletes everything in subdirname + subdir)
rm -r subdirname/* (same w/o removing subdir)
you can use ../ to go up one directory or start with / to start from the rootdir, in any unix command that takes a file arg

never ever do "rm -r /" :)
 
Last edited by a moderator:
Thanks Damgo, I was playing around with it yesterday and really couldn't figure it out :)

What exactly does -r stand for?

So rm -r / would remove everything that is placed underneath the rootdir? Better be carefull then :P
 
yeah, watch out. I've done that before, it wasn't pretty.

-r is 'recursive'

if you want to know what a UNIX command does, you can almost always type "man command" or sometimes "info command" to get the manpage with all the options and explanations. "command --help" sometimes gives a shorter version.

eg "man rm" tells you all the options you can use.
 
Yes, I had tried that yesterday: man rm
But it doesn't tell me anything about deleting from root or subdirectories :)

I noticed -r and was able to delete from the subdirectory and when promted I didn't allow it to delete the directory itself.. your solution is better though :)

So what does recursive mean?
 
Be very careful with this command especially if you are logged in as root. You could remove the contents of an entire harddisk with 8 keypresses :)

I have done this a once or twice with very serious consequences.
 
I have experience with files being deleted in Unix.. someone was trying to clean-up the system and was deleting old programmes. Instead of deleting the old program that was designed for me, the new one was deleted with all my data inside!

Ofcourse we have a backup. One problem: the backup apparently had had problems and the latest data were anno not recent so I had to reenter everything :S That is when I learned that if something is deleted in Unix, it is gone :P

So I tried looking up the meaning of recursive, which gave me things like "GNU: GNU's Not Unix" or some other weird compound names. Not very helpfull.
 
>>So what does recursive mean?
Something that calls or refers to itself. eg, f(n)=f(n-1)+f(n-2) is a recursive formula... here rm -r works by deleting something if it's a file; if it's a directory it calls rm -r on everything inside the directory.

silly chemists! :p
 
lol, I'd invite you to discuss the benefits of isolated populations in the genetic dissection of complex diseases with me :P

Thanks for the little computer 101 time :)
 
  • #10
Originally posted by Monique
Yes, I had tried that yesterday: man rm
But it doesn't tell me anything about deleting from root or subdirectories :)

I noticed -r and was able to delete from the subdirectory and when promted I didn't allow it to delete the directory itself.. your solution is better though :)

So what does recursive mean?

The way to remove entire subdirectories is to use rm -rf /dirname/otherdir or whatever you're trying to do. At least, I believe this. I don't have any subdirectories I'm willing to delete, so I'm going off memory.

As you know the -r stands for recursive, the f stands for force. I remember after 9/11 there were shirts on thinkgeek.com that said rm -rf /bin/laden. Very funny
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 24 ·
Replies
24
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 16 ·
Replies
16
Views
8K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 16 ·
Replies
16
Views
6K