LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2010, 10:46 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Some Questions About Deleting Files


I know that rm -i will prompt wether you want to delete each file.

But rm -i -r will prompt for each file in each subdirectory recursively. How to make it prompt just for the directory itself, and then delete its contents without asking?

How to delete all the files in a directory without deleting . and ..?

How to recursively delete all tilde files in a directory?

How to GUI file managers delete files to Trash? Where is this "Trash" located? Can you delete to trash in the command line?

Last edited by MTK358; 02-09-2010 at 10:47 AM.
 
Old 02-09-2010, 11:18 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
> How to make it prompt just for the directory itself, and then delete its contents without asking?

There may be an easier way, but this is what came to me first:
Code:
for i in `find . -type d`; do 
  echo "Delete files from $i?"; 
  read A; 
  if [ "x$A" = "xY" ]; then   
    echo rm $i/*; 
  fi; 
done

> How to delete all the files in a directory without deleting . and ..?

find . -type f -maxdepth 1 | xargs rm -f

> How to recursively delete all tilde files in a directory?

find . -type f -name '~*' | xargs rm -f


GUI file managers 'delete to trash' is a move to a specific folder designated as trash.
 
Old 02-09-2010, 11:38 AM   #3
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Hm..

Quote:
Originally Posted by MTK358 View Post
But rm -i -r will prompt for each file in each subdirectory recursively. How to make it prompt just for the directory itself, and then delete its contents without asking?
Check out: rm -I -r DIRECTORY/* (also, man rm FTW).

Quote:
How to delete all the files in a directory without deleting . and ..?
Are you sure you understand what '.' and '..' represent? In any case, check out: rm -rf DIRECTORY/*

Quote:
How to GUI file managers delete files to Trash? Where is this "Trash" located? Can you delete to trash in the command line?
How about: mkdir ~/Trash and then use mv FILE ~/Trash instead of rm FILE?
 
Old 02-09-2010, 01:23 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by carbonfiber View Post
Are you sure you understand what '.' and '..' represent? In any case, check out: rm -rf DIRECTORY/*
As I understand . is the current dir, and .. is the parent dir. But I don't want to erase the entire FS!

How would that prevent . and .. from being deleted?
 
Old 02-09-2010, 01:37 PM   #5
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Hm. Let me ask you this: what command are you afraid of running, out of fear of removing "." and ".."?
 
Old 02-09-2010, 01:45 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
I'm not in need of running a command that I'm afraid of. I'm just curious to know.
 
Old 02-09-2010, 02:02 PM   #7
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Know -what-? You said you don't know how to prevent "." and ".." being deleted. Don't delete them? What do you want us to say? My guess is that by looking at a directory listing:

Code:
$ ls -a video/
.  ..  icantbelieveitsnot-pr0n	pr0n  sick-pr0n
you are considering that a command such as

Code:
$ rm -rf video
will delete: ., .., icantbelieveitsnot-pr0n, pr0n, sick-pr0n. Am I fortunate?
 
Old 02-09-2010, 02:10 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
What will happen if I do this:

rm -rf ..

And why does

sudo rm -rf *

delete everything, not just the contents of the current dir?
 
Old 02-09-2010, 02:13 PM   #9
mudangel
Member
 
Registered: May 2008
Location: Ohio
Distribution: Slackware
Posts: 267

Rep: Reputation: 56
Quote:
Originally Posted by carbonfiber View Post
Code:
$ ls -a video/
.  ..  icantbelieveitsnot-pr0n	pr0n  sick-pr0n
That's too funny!
 
Old 02-09-2010, 02:18 PM   #10
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Quote:
Originally Posted by MTK358 View Post
rm -rf ..
Try it:

Code:
$ mkdir one
$ mkdir one/two
$ cd one/two
$ rm -rf ..
 
Old 02-09-2010, 02:36 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by carbonfiber View Post
Try it:

Code:
$ mkdir one
$ mkdir one/two
$ cd one/two
$ rm -rf ..
Are you sure I won't erase everything in my account?
 
Old 02-09-2010, 03:40 PM   #12
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
No, but be careful. You will not be able to do a 'cd ..' afterwards, you'll have to change directory via absolute path.
 
Old 02-09-2010, 03:50 PM   #13
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Quote:
Originally Posted by MTK358 View Post
Are you sure I won't erase everything in my account?
I'm a complete stranger you've met on the internets. Are you really willing to trust me now that I'm saying: yes, I'm sure?

Anyway, here's what happens on my system:

Code:
$ mkdir one
$ mkdir one/two
$ cd one/two
$ rm -rf ..
rm: cannot remove directory: `..'
$ rm -rf .
rm: cannot remove directory: `.'
 
Old 02-09-2010, 03:57 PM   #14
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,275

Rep: Reputation: 126Reputation: 126
Quote:
Originally Posted by MTK358 View Post
And why does

sudo rm -rf *

delete everything, not just the contents of the current dir?
Remember that sub-directory entries are part of the current directory, just like files are.
 
Old 02-09-2010, 04:03 PM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
I'm so confused

Howcome rm -rf .. doesn't delete the parent but rm -rf * does?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files... Skillz Linux - General 7 10-14-2009 01:40 AM
Probably a simple sed questions for deleting lines... oldcarguy85 Linux - General 4 03-28-2008 08:11 AM
About deleting a files combilli Linux - Newbie 1 01-09-2007 11:54 PM
Deleting old files shiroh_1982 Linux - Newbie 2 06-21-2006 08:26 AM
Deleting files in C monil Programming 4 03-13-2005 11:33 AM


All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration