LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 09:46 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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 09:47 AM.
 
Old 02-09-2010, 10:18 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
> 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, 10: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, 12:23 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 12: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, 12:45 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I'm not in need of running a command that I'm afraid of. I'm just curious to know.
 
Old 02-09-2010, 01: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, 01:10 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 01: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, 01: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, 01:36 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 02:40 PM   #12
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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, 02: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, 02:57 PM   #14
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,407

Rep: Reputation: 141Reputation: 141
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, 03:03 PM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I'm so confused

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


Reply



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 12:40 AM
Probably a simple sed questions for deleting lines... oldcarguy85 Linux - General 4 03-28-2008 07:11 AM
About deleting a files combilli Linux - Newbie 1 01-09-2007 10:54 PM
Deleting old files shiroh_1982 Linux - Newbie 2 06-21-2006 07:26 AM
Deleting files in C monil Programming 4 03-13-2005 10:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:53 AM.

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
Open Source Consulting | Domain Registration