LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 05-23-2015, 03:31 PM   #1
HardenedCriminal
Member
 
Registered: May 2015
Posts: 104

Rep: Reputation: Disabled
Find a File delete out different file in same directory


I am trying to search for SOMEFILE and in every sub directory where SOMEFILE exist wipe out a DIFFERENTFILE.

Just can't get the end part right or need more steps.

Thanks to all in advance.

----------------------
#!/bin/bash

cd /etc/somewhere/ && find . -name 'somefile' -exec wipe -r -s {} \;
 
Old 05-23-2015, 03:52 PM   #2
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
{} substitutes filename - try fiddling with that.
 
Old 05-23-2015, 04:09 PM   #3
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
if A then delete B

What if B is not there

you are only looking for A .......
 
Old 05-23-2015, 04:09 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
maybe you can pipe something like this into xargs:
Code:
find . -name 'somefile' -printf "%h/file-to-delete \n"
 
Old 05-23-2015, 04:11 PM   #5
HardenedCriminal
Member
 
Registered: May 2015
Posts: 104

Original Poster
Rep: Reputation: Disabled
thanks will try.
 
Old 05-23-2015, 04:17 PM   #6
HardenedCriminal
Member
 
Registered: May 2015
Posts: 104

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by John VV View Post
if A then delete B

What if B is not there

you are only looking for A .......
Yes I am only looking for SOMEFILE that is in multiple sub-directories (many copies, same name) but only in those sub-directories do I want to wipe DIFFERENTFILE
 
Old 05-27-2015, 07:02 PM   #7
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
The find command has an -execdir option that could do what you want. In the following example, I created directories named 1, 2, 3, and 4, containing files named a and b. The find command with -execdir deletes b only in directories which contain a.
Code:
$ find . -type d    # This shows the directories.
.
./4
./1
./3
./2
$ find . -type f    # This shows the files we started with.
./4/b
./1/a
./1/b
./3/b
./2/a
./2/b
$ find . -name a -execdir rm -v b \;    # This finds some files and deletes other files.
removed `b'
removed `b'
$ find . -type f    # This shows the files which are left.
./4/b
./1/a
./3/b
./2/a
$ find . -name a -execdir rm -v b \;    # Error message if b does not exist.
rm: cannot remove `b': No such file or directory
rm: cannot remove `b': No such file or directory
$
 
2 members found this post helpful.
Old 05-27-2015, 08:41 PM   #8
HardenedCriminal
Member
 
Registered: May 2015
Posts: 104

Original Poster
Rep: Reputation: Disabled
Thanks just what I needed.
 
Old 06-04-2015, 05:30 PM   #9
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
Safety First, display only

Code:
echo 'Search For?' ;\
read SEARCH ;\
echo 'Remove?' ;\
read REMOVE ;\
for i in $(find . $SEARCH 2>/dev/null |grep $SEARCH| sed s/$SEARCH//g); do \
echo $i$REMOVE ;\
done
Remove the unwanted files

Code:
echo 'Search For?' ;\
read SEARCH ;\
echo 'Remove?' ;\
read REMOVE ;\
for i in $(find . $SEARCH 2>/dev/null |grep $SEARCH| sed s/$SEARCH//g); do \
rm $i$REMOVE 2>/dev/null ;\
done
Granted this will delete $REMOVE even if the file does not exist, but the error message is dumped into /dev/null. Also not that the use of ";" allows this to be cut and paste into shell so it does not need to be saved as a file to be executed.
 
Old 06-04-2015, 07:28 PM   #10
HardenedCriminal
Member
 
Registered: May 2015
Posts: 104

Original Poster
Rep: Reputation: Disabled
Thanks that's great.
 
  


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
how to delete all the files in a directory except one file masood24 Linux - General 6 12-12-2009 01:16 PM
Find folders with ONLY one specific file in and delete file and folder - How ? gedi1 Linux - Newbie 5 10-21-2009 08:09 PM
a2ps: cannot find file `c0419bt_.afm': No such file or directory rg3 Slackware 5 08-02-2009 03:11 PM
How do you delete a corrupted file or directory? BrianK Linux - General 8 10-23-2004 12:33 PM
how to delete a file or directory completely ty263 Red Hat 1 07-18-2004 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:11 AM.

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