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


Reply
  Search this Thread
Old 02-22-2017, 08:09 AM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 73

Rep: Reputation: 0
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.
 
Old 02-22-2017, 08:12 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
You have an extra dash in there after -type. Check the error message.
 
1 members found this post helpful.
Old 02-22-2017, 08:14 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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.
Old 02-22-2017, 08:20 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Ah. That's much better advice.
 
Old 02-22-2017, 08:21 AM   #5
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 googerpbs 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.
 
Old 02-22-2017, 08:26 AM   #6
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
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 View Post
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
 
Old 02-22-2017, 08:30 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by Springs View Post
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 {} \;
 
Old 02-22-2017, 08:44 AM   #8
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 View Post
......
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 View Post
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
 
Old 02-22-2017, 08:46 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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.
 
Old 02-22-2017, 08:49 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
additionally:
you can use -delete instead of -exec rm {}
 
Old 02-22-2017, 08:54 AM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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
 
Old 02-22-2017, 08:55 AM   #12
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] remove readme files from a large group of *.zip files with a command line function thealmightyos Linux - General 1 08-14-2014 04:24 PM
How to overwrite existing files by using tar command (Remove extra files) Kalibo Linux - Newbie 9 12-26-2013 12:42 AM
[SOLVED] Bash script to read through text files and find largest number in files pomico Programming 15 09-13-2012 01:07 PM
[SOLVED] command to find and remove all files from /tmp direct that are older than 5 days old vaibhavs17 Linux - Newbie 1 06-28-2012 11:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:39 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