Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-22-2017, 08:09 AM
|
#1
|
Member
Registered: Apr 2008
Posts: 73
Rep:
|
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
any ideas? am i doing something wrong?
Last edited by Springs; 02-22-2017 at 08:26 AM.
|
|
|
02-22-2017, 08:12 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
You have an extra dash in there after -type. Check the error message.
|
|
1 members found this post helpful.
|
02-22-2017, 08:14 AM
|
#3
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
|
1 members found this post helpful.
|
02-22-2017, 08:20 AM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Ah. That's much better advice.
|
|
|
02-22-2017, 08:21 AM
|
#5
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
find is recursive no loop needed
Code:
find /path/to/folder -iname "$line" -type -f -exec rm {} \;
I didn't see a deliminator to specify what files it needed to look for to delete.
the way it is written in that loop it'd delete everything.
you just need to give it a qualified specifier. something unique to the files you want it to find then delete.
like this
Code:
find /path/to/folder -type -f -iname "*gerp*" -exec rm {} \;
finding within file name goo gerpbs gets deleted.
example
Code:
(userx@SlackO⚡️~/test)>>$ touch goodgerptt
(userx@SlackO⚡️~/test)>>$ find . -type f -iname "*gerp*"
./goodgerptt
Last edited by BW-userx; 02-22-2017 at 08:31 AM.
|
|
|
02-22-2017, 08:26 AM
|
#6
|
Member
Registered: Apr 2008
Posts: 73
Original Poster
Rep:
|
Quote:
Originally Posted by Turbocapitalist
You have an extra dash in there after -type. Check the error message.
|
sorry that's me typing without reading it fully. my saved script doesn't have the extra - after -type
Quote:
Originally Posted by BW-userx
find is recursive no loop needed
|
that may be true but i'm trying to pass multiple file extensions through it which it can't do on it's own
|
|
|
02-22-2017, 08:30 AM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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
|
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:
Code:
find /path/to/folder \( -iname '*.pdf' -o -iname '*.txt' \) -type f -exec echo rm {} \;
|
|
|
02-22-2017, 08:44 AM
|
#8
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
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?
try this instead
Code:
(userx@SlackO⚡️~/test)>>$ find . -type f \( -iname \*.jpg -o -iname \*.png \)
./filebadfile1.jpg
./filebadfile1.png
|
|
|
02-22-2017, 08:46 AM
|
#9
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Interesting.
Code:
find /path/to/folder \( -iname '*.pdf' -o -iname '*.txt' \) -type f -exec echo rm {} \;
Is a nice way to oneline this.
Last edited by szboardstretcher; 02-22-2017 at 08:48 AM.
|
|
|
02-22-2017, 08:49 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,671
|
additionally:
you can use -delete instead of -exec rm {}
|
|
|
02-22-2017, 08:54 AM
|
#11
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,828
|
Is there some error message when the rm "doesn't work"?
Try running the script with the shell's "-x" option so that you can see what find command is actually being executed.
You can try using "-ok" in place of "-exec" and get prompted before each attempted execution of " rm".
And finally, find can do the deletion all on its own:
Code:
find /path/to/folder -iname "$line" -type f -delete
|
|
|
02-22-2017, 08:55 AM
|
#12
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
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
just them are gone
Last edited by BW-userx; 02-22-2017 at 09:01 AM.
|
|
|
All times are GMT -5. The time now is 01:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|