Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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?
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!
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.