using find to remove files using a while read command issue
Linux - NewbieThis 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
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.
using find to remove files using a while read command issue
hi all,
trying to write something to clear up files that i don't need in specific folders. i'm using the below to action it all but it seems that the rm command is not working?
Code:
while read -r line
do
find /path/to/folder -iname "$line" -type f -exec rm {} \;
done < input.txt
input.txt contents just have file extensions inside ie. *.pdf* or *.txt* in a list
now when i run the find command manually in a terminal it works. If i change "$line" to something that's in the input file it works.
as well, if i change the find command to echo the lines instead it works fine. just seems that when i use the find command as per the above it doesn't work
Before you EVER run a find command that executes a delete, you should TEST it first with something not so destructive. Like a -print or a copy to make sure the results are what you expect.
For example:
Code:
find /path/to/folder -iname "$line" -type f -print
or
find /path/to/folder -iname "$line" -type f -exec cp {} /path/to/test/folder \;
Last edited by szboardstretcher; 02-22-2017 at 08:17 AM.
that may be true but i'm trying to pass multiple file extensions through it which it can't do on it's own
Ok. You've run into the situation that find has an implied logical AND between each element in the expression. It is possible to use a logical OR when needed. But you'll need to also use parenthesis to apply proper precedence to the group:
I wasn't even going to address this original question because it is mute. I actually just closed that post off canceling it then read this so now that you've said something on it.
Let pull apart your code and see what it is not doing that you wanted it to. even though it is a bit redundant.
Though I see your logic in trying to get everything done at once, having more than one extension type file you are wanting to get rid off at the same time.
Quote:
Originally Posted by Springs
......
that may be true but i'm trying to pass multiple file extensions through it which it can't do on it's own
Quote:
Originally Posted by Springs
as well, if i change the find command to echo the lines instead it works fine. just seems that when i use the find command as per the above [in a loop] it doesn't work
any ideas? am i doing something wrong?
how does that code get the extensions out of your file and know where to place them within the line of code you're trying to run inside of the loop?
you're not still trying this in a loop, is someone?
no more loop needed, nor shell script. just run it off the cli. one line bam done.
I'd suggest creating a test dir and files and just keep in inside of it by replacing the path to with just a period . to keep it inside of that dir to see if it deletes only the ones being looked for.
doesn't hurt to test first.
like so
Code:
(userx@SlackO⚡️~/test)>>$ touch badfile.jpg
(userx@SlackO⚡️~/test)>>$ touch badfile.png
(userx@SlackO⚡️~/test)>>$ ls
ConvertNumbersIntoWords EspeakConvertNumbersIntoWords a.out badfile.jpg badfile.png dependencies goodgerptt h homework1 main.cpp
(userx@SlackO⚡️~/test)>>$ find . -type f \( -iname \*.jpg -o -iname \*.png \) -delete
(userx@SlackO⚡️~/test)>>$ ls
ConvertNumbersIntoWords EspeakConvertNumbersIntoWords a.out dependencies goodgerptt h homework1 main.cpp
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.