Visit Our UNIX and Linux User Community


Using "find" and "-exec rm" ... Just no luck :(


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using "find" and "-exec rm" ... Just no luck :(
# 1  
Old 08-23-2009
Bug Using "find" and "-exec rm" ... Just no luck :(

Hi All,

Ok, so I'm fairly new to Unix, but would like to think I'm grasping things fairly ok (Well until now Smilie ), BUT with this one I'm going in circles ... LOL .. really can't work out why? So sorry for this post if it seems a little "dumb" .. But here goes.

In short I'd like to achieve the "removal" of files based on a text list.

example, if I had a file called "delete.txt" it would consist of ONLY file names of what should be deleted.

These files would be found below a PATH and that's where I'm pointing find to.

I've been trying all variations of:

Code:
if [ -f $DELETE ] ;then
cat $DELETE | while read file; do find $HOME -name "$file" -exec rm -rf {} \;
done
mv -f $UPDATE/inbox/delete.txt $UPDATE/trash
fi

I've also been trying with xargs (But when my "delete.txt" file typically has spaces in it won't work Smilie .. I've tried using find with < $DELETE but also no luck. What's really strange is I'm sure it worked once or twice, but now haven't been able to get it working again. ANY help will be greatly appreciated. (And yes I see there are a few posts already in the forum, but apologiese, that's exactely what I've been trying with no luck Smilie - Sorry)

Hope to chat soon and thanks in advance Smilie

Regards
Dean

Last edited by radoulov; 09-03-2009 at 11:49 AM.. Reason: added code tags
# 2  
Old 08-23-2009
I see no reasons for you script (please encode it with code tags) not to work. Can you explain where you see it failing ?
# 3  
Old 08-23-2009
Hi Jlliagre,

Thanks for your reply, as I say, to me it should also work 100%, but it's not Smilie .. I'm not sure if perhaps it's based on the rest of the script below happening before the deletes happened as such, ie would it be possible that the mv command below "done" could be happening before it has chance to read the file? .. I also thought that file perms could be a problem, but this shouldn't be the case as the files are chmod 777 and the script is being run by cron as root.

Can I ask what you mean by encode it with tags? .. Would you like me to past the complete script?

Thanks and chat soon Smilie
# 4  
Old 08-26-2009
Is that a semi-colon between the if condition and "Then" ? Do you need that ?
# 5  
Old 08-26-2009
Hi Shababc,

Yip, there is a semi colon there (Smilie should that not be the case? The exact code is:

Code:
if [ -f $DELETE ] ;then
cat $DELETE | while read file; do find $HOME -name "$file" -exec rm -f {} \;
done
mv -f $UPDATE/inbox/delete.txt $UPDATE/trash
fi

Thanks and hope we can find out why this isn't working ... I'll try without the semi colon, but I have similar "if's" ALL with the space ;then in place so not sure?

Chat soon
Dean
# 6  
Old 08-26-2009
The second version is nearly there. It uses "rm -f" (files) rather than "rm -rf" (directories), but will still not deal with filenames containing spaces.

You need double quotes round the curly braces to preserve spaces.

Test with "echo" not real "rm" then remove the echo when you are happy.

Code:
if [ -f $DELETE ] ;then
cat $DELETE | while read file; do find $HOME -name "$file" -exec echo rm -f "{}" \;
done
mv -f $UPDATE/inbox/delete.txt $UPDATE/trash
fi

# 7  
Old 08-26-2009
Heyo Methyl,

Thank You .. Brilliant Smilie ... I'll give this a try and see how it goes .. Really appreciate the help Smilie

Dean
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #926
Difficulty: Medium
On 05:20:00 UTC on Saturday, 24 January 2065, the Unix time value will equal 4000000000 seconds.
True or False?

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

find . -path "*_nobackup*" -prune -iname "*.PDF" \( ! -name "*_nobackup.*" \)

These three finds worked as expected: $ find . -iname "*.PDF" $ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \) $ find . -path "*_nobackup*" -prune -iname "*.PDF" They all returned the match: ./folder/file.pdf :b: This find returned no matches: $ find . -path "*_nobackup*" -prune... (3 Replies)
Discussion started by: wolfv
3 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies

Featured Tech Videos