LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-13-2009, 06:17 PM   #1
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Rep: Reputation: 32
Deleting files...


I want to delete a bunch of files that all begin with index in a bunch of subdirectories, and some of those sub directories have sub directories with files in it too. This is how I think I would do it, can someone please confirm for me before I delete stuff I shouldn't?

ls -R | del -f index*
 
Old 10-13-2009, 06:49 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Skillz View Post
I want to delete a bunch of files that all begin with index in a bunch of subdirectories, and some of those sub directories have sub directories with files in it too. This is how I think I would do it, can someone please confirm for me before I delete stuff I shouldn't?

ls -R | del -f index*
So you want to delete files that begin with the string "index" regardless of where they are, yes?

Code:
find /path/of/interest -type f | grep -P "/index[^/]*$" | while read path
do
    echo "Would delete file \"$path\""
    # rm "$path"
done
Definitely test this script before uncommenting the commented line.
 
Old 10-13-2009, 06:56 PM   #3
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Well I want the ones that are only in the directory public_html and all of it's sub directories. So if the /path/of/interest can be /home/user/public_html and it'll delete all the files with the string "index" within' that directory and all of it's sub directories, then yes.
 
Old 10-13-2009, 07:08 PM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Skillz View Post
Well I want the ones that are only in the directory public_html and all of it's sub directories. So if the /path/of/interest can be /home/user/public_html and it'll delete all the files with the string "index" within' that directory and all of it's sub directories, then yes.
I wrote the script so it only prints out what it would do if you uncommented the commented line, so why not try it as written and see what files it finds?
 
Old 10-13-2009, 07:30 PM   #5
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by lutusp View Post
I wrote the script so it only prints out what it would do if you uncommented the commented line, so why not try it as written and see what files it finds?
Tested and it will delete exactly what I want. Thanks so much!
 
Old 10-14-2009, 12:22 AM   #6
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
FYI

find /home/user/public_html -name index\* -exec rm -f '{}' \;

will do the same thing.

If you want to make sure you have all the correct files beforehand, leave off the "-exec rm -f '{}' \;" part of the command above. If it is what you want reinstate the -exec part and all is good.
 
Old 10-14-2009, 12:31 AM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by ArfaSmif View Post
FYI

find /home/user/public_html -name index\* -exec rm -f '{}' \;

will do the same thing.

If you want to make sure you have all the correct files beforehand, leave off the "-exec rm -f '{}' \;" part of the command above. If it is what you want reinstate the -exec part and all is good.
No, it is not the same thing at all -- not remotely. Your script will try to delete anything with "index*" as part of its path, including directories, file names that have "index*" located somewhere other than the beginning of their names, and so forth.

Quote:
will do the same thing.
Simply false. Think about how your script differs from mine.
 
Old 10-14-2009, 12:40 AM   #8
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Quote:
Originally Posted by lutusp View Post
No, it is not the same thing at all -- not remotely. Your script will try to delete anything with "index*" as part of its path, including directories, file names that have "index*" located somewhere other than the beginning of their names, and so forth.



Simply false. Think about how your script differs from mine.

1. rm -f will not delete a directory
2. index\* will only match files/directories beginning with the word index
3. Any directory index* will not be removed by rm -f and all files below it index* which are not directories will be removed.

And you should think about the word "patronising".

Last edited by ArfaSmif; 10-14-2009 at 06:04 PM. Reason: more info
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About deleting a files combilli Linux - Newbie 1 01-09-2007 10:54 PM
Deleting files rossi46 Mandriva 5 11-08-2006 08:15 AM
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
Deleting files! a_priebe47 Slackware 6 10-17-2004 09:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:11 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
Open Source Consulting | Domain Registration