LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 08-17-2009, 09:11 AM   #1
Paraply
Member
 
Registered: Jun 2007
Posts: 34
Thanked: 0
Recursively delete only dirnames ending with "_files"


[Log in to get rid of this advertisement]
rm with ./ seems to disable --recursive

I have a file tree with about 170 000 files and directories and I want to recursively only delete all directories with names ending with "_files".

"rm -r *_files" --> does not work (same for "-R" or "--recursive")

"rm -r ./*_files" --> works, but only in the first directory. It looks to me as if "./" is disabling the recursive functionality.

What am I doing wrong?
Paraply is offline  
Tag This Post , ,
Reply With Quote
Old 08-17-2009, 09:20 AM   #2
onebuck
Guru
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 5,449
Blog Entries: 1
Thanked: 204
Hi,

Quote:
excerpt from 'man rm';

DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If a file is unwritable, the standard input is a tty, and the -f or --force option is not given, rm prompts the user for whether to remove the file. If the response does not begin with `y' or `Y', the file is skipped.
OPTIONS

Remove (unlink) the FILE(s).

-d, --directory
unlink FILE, even if it is a non-empty directory (super-user only; this works only if your system
supports `unlink' for nonempty directories)

-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
--no-preserve-root do not treat `/' specially (the default)
--preserve-root
fail to operate recursively on `/'
-r, -R, --recursive
remove the contents of directories recursively
-v, --verbose
explain what is being done
--help
display this help and exit
--version
output version information and exit

To remove a file whose name starts with a `-', for example `-foo', use one of these commands:

rm -- -foo
rm ./-foo

Note that if you use rm to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred.
Be very careful when using the 'rm' and options as you could screw your system up.
onebuck is online now     Reply With Quote
Old 08-17-2009, 09:52 AM   #3
Paraply
Member
 
Registered: Jun 2007
Posts: 34
Thanked: 0

Original Poster
Gary,

thanks for the good wishes, but why were you assuming I had not read the rm manual? And if you think I need to learn how to ask questions "the Smart Way", why don't you point out what you consider lacking in my post?

Really, I find your reply to be most unhelpful.

Here is the entire "man rm" file on my (Arch) system:

"RM(1) User Commands RM(1)

NAME
rm - remove files or directories

SYNOPSIS
rm [OPTION]... FILE...

DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does
not remove directories.

If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or
--recursive are given, then rm prompts the user for whether to proceed with the entire operation. If
the response is not affirmative, the entire command is aborted.

Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not
given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the
file. If the response is not affirmative, the file is skipped.

OPTIONS
Remove (unlink) the FILE(s).

-f, --force
ignore nonexistent files, never prompt

-i prompt before every removal

-I prompt once before removing more than three files, or when removing recursively. Less intrusive
than -i, while still giving protection against most mistakes

--interactive[=WHEN]
prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always

--one-file-system
when removing a hierarchy recursively, skip any directory that is on a file system different
from that of the corresponding command line argument

--no-preserve-root
do not treat `/' specially

--preserve-root
do not remove `/' (default)

-r, -R, --recursive
remove directories and their contents recursively

-v, --verbose
explain what is being done

--help display this help and exit

--version
output version information and exit

By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each
listed directory, too, along with all of its contents.

To remove a file whose name starts with a `-', for example `-foo', use one of these commands:

rm -- -foo

rm ./-foo

Note that if you use rm to remove a file, it is usually possible to recover the contents of that file.
If you want more assurance that the contents are truly unrecoverable, consider using shred.

AUTHOR
Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.

REPORTING BUGS
Report rm bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>

COPYRIGHT
Copyright © 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent
permitted by law.

SEE ALSO
unlink(1), unlink(2), chattr(1), shred(1)

The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are prop‐
erly installed at your site, the command

info coreutils 'rm invocation'

should give you access to the complete manual.

GNU coreutils 7.4 May 2009 RM(1)


As you can see, there is nothing here that pertains to "-d, --directory" etc.

Last edited by Paraply; 08-17-2009 at 10:08 AM..
Paraply is offline     Reply With Quote
Old 08-17-2009, 10:02 AM   #4
onebuck
Guru
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 5,449
Blog Entries: 1
Thanked: 204
Hi,

You did try the '-d' option as root? We all present things in a manner that is not always complete. I assumed since you were using the 'rm' recursively then you may have over looked the '-d' option since nothing seemed important below a certain point.
onebuck is online now     Reply With Quote
Old 08-17-2009, 10:12 AM   #5
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 321
Thanked: 23
I thought the original post was about recursively removing the directories that ended with _files from the current directory. I posted basically what the OP said he already tried.

Last edited by joeBuffer; 08-17-2009 at 10:43 AM..
joeBuffer is offline     Reply With Quote
Old 08-17-2009, 10:30 AM   #6
onebuck
Guru
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 5,449
Blog Entries: 1
Thanked: 204
Hi,

My bad on the '-d'. It is not longer supported with 'rm'.

I'm used to explicit remove with the '-r' and '-f' to force but do remember the '-d'. I also remember that the unlinking with the '-d' could cause some problems. So the 'rm -rf somefiles' would be better use. But as root you had better be certain of the use.

You might want to look at 'shred' if you are a little paranoid about the files.

I'll have to note to the author about the need to remove the '-d' from the online reference.
onebuck is online now     Reply With Quote
Old 08-17-2009, 10:32 AM   #7
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,134
Thanked: 465
Code:
find . -type d -name \*_files -print0 | xargs -0 echo rm -r
check the output and if it gives the correct result (it echoes the rm command with all its arguments) remove the echo statement and run again.
colucix is offline     Reply With Quote
Old 08-17-2009, 10:38 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 2,908
Thanked: 271
I've never had a problem using "rm -rf" to remove full directory hierarchies, use with caution, it's like nuclear fission, once it starts it's very difficult to stop the chain reaction s
i92guboj is online now     Reply With Quote
Old 08-17-2009, 10:45 AM   #9
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,134
Thanked: 465
Quote:
Originally Posted by i92guboj View Post
it's like nuclear fission, once it starts it's very difficult to stop the chain reaction s
He he... you're right! After eleven years using *nix, last month I accidentally "removed minus ar" an entire directory tree: 300 Gb before my time of reaction let me press Ctrl-C!
colucix is offline     Reply With Quote
Old 08-17-2009, 10:57 AM   #10
onebuck
Guru
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 5,449
Blog Entries: 1
Thanked: 204
Hi,

I love the brick wall icon. It would have to be a bigger wall for me.

But yes, one should use caution whenever one is 'root' and using 'rm'.
onebuck is online now     Reply With Quote
Old 08-17-2009, 10:57 AM   #11
Paraply
Member
 
Registered: Jun 2007
Posts: 34
Thanked: 0

Original Poster
Quote:
(...) check the output and if it gives the correct result (it echoes the rm command with all its arguments) remove the echo statement and run again.
Perfect.

This worked exactly as I wanted. Thanks a lot!

I certainly need to learn more about the shell and utilities, but somehow I seem to get totally lost whenever I try perusing 'man find'...
Paraply is offline     Reply With Quote
Old 08-17-2009, 11:03 AM   #12
onebuck
Guru
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 5,449
Blog Entries: 1
Thanked: 204
Hi,

Glad to hear you got what you want.

Sorry about the mix up.

You can look at 'Learning the Shell' to aid you in the future.

Just a few more links that may aid you;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

These links and others can be found at 'Slackware-Links' .
More than just Slackware® links!
onebuck is online now     Reply With Quote
Old 08-17-2009, 11:07 AM   #13
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,864
Blog Entries: 6
Thanked: 226
Quote:
Originally Posted by colucix View Post
He he... you're right! After eleven years using *nix, last month I accidentally "removed minus ar" an entire directory tree: 300 Gb before my time of reaction let me press Ctrl-C!
Yeah -- I did it once cleaning up a mission-critical user-acceptance/bugfix system because I scripted rm -rf after a cd and forgot to error trap the cd. No problem -- what are backups for? Reinstall and restore. Routine stuff. But DVDs were new then and there was no way to mount both / and /usr with the DVD drive in place and the only way to install the OS was from DVD. Ouch! A long night and a tired dawn but the users only lost a couple of hours work.
catkin is offline     Reply With Quote
Old 08-17-2009, 11:08 AM   #14
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 321
Thanked: 23
I've read some about find and can use it pretty well, but I haven't read anything about xargs yet, and it's at the top of my list right now.
joeBuffer is offline     Reply With Quote
Old 08-17-2009, 11:11 AM   #15
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,864
Blog Entries: 6
Thanked: 226
Hello onebuck
Quote:
Originally Posted by onebuck View Post
My bad on the '-d'.
"My bad" is appearing more often. I'd assumed it came from automated translation but that's not the case here. Has automated translation produced a new usage? I love how human languages evolve and how, doing so, they encapsulate human history.
catkin is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
"Ending Session" Problem mickeyboa Fedora 2 03-11-2008 10:32 AM
smb.conf having share names ending in "$" the_imax Linux - Server 3 03-11-2008 06:50 AM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 05:24 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
Recursively deleting ".directory" Frank616 Linux - Software 2 03-26-2005 12:58 AM


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration