LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Some Questions About Deleting Files (https://www.linuxquestions.org/questions/linux-newbie-8/some-questions-about-deleting-files-788013/)

MTK358 02-09-2010 09:46 AM

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?

crabboy 02-09-2010 10:18 AM

> 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.

carbonfiber 02-09-2010 10:38 AM

Hm..

Quote:

Originally Posted by MTK358 (Post 3857905)
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?

MTK358 02-09-2010 12:23 PM

Quote:

Originally Posted by carbonfiber (Post 3857945)
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?

carbonfiber 02-09-2010 12:37 PM

Hm. Let me ask you this: what command are you afraid of running, out of fear of removing "." and ".."?

MTK358 02-09-2010 12:45 PM

I'm not in need of running a command that I'm afraid of. I'm just curious to know.

carbonfiber 02-09-2010 01:02 PM

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?

MTK358 02-09-2010 01:10 PM

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?

mudangel 02-09-2010 01:13 PM

Quote:

Originally Posted by carbonfiber (Post 3858081)
Code:

$ ls -a video/
.  ..  icantbelieveitsnot-pr0n        pr0n  sick-pr0n


That's too funny!

carbonfiber 02-09-2010 01:18 PM

Quote:

Originally Posted by MTK358 (Post 3858088)
rm -rf ..

Try it:

Code:

$ mkdir one
$ mkdir one/two
$ cd one/two
$ rm -rf ..


MTK358 02-09-2010 01:36 PM

Quote:

Originally Posted by carbonfiber (Post 3858097)
Try it:

Code:

$ mkdir one
$ mkdir one/two
$ cd one/two
$ rm -rf ..


Are you sure I won't erase everything in my account?

crabboy 02-09-2010 02:40 PM

No, but be careful. You will not be able to do a 'cd ..' afterwards, you'll have to change directory via absolute path.

carbonfiber 02-09-2010 02:50 PM

Quote:

Originally Posted by MTK358 (Post 3858119)
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: `.'


Quakeboy02 02-09-2010 02:57 PM

Quote:

Originally Posted by MTK358 (Post 3858088)
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.

MTK358 02-09-2010 03:03 PM

I'm so confused :confused:

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


All times are GMT -5. The time now is 04:22 AM.