LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-29-2017, 06:24 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Rep: Reputation: Disabled
Remove(not) files based on property value


Hi All,

Need your help to fix one script.

Main agenda is:
1. Read a property file.
2. Delete all files in directory except the name from Property file.

I am trying to read property file for value then deleting all files from directory except THAT value/name.

I have tried so far as below:
Property File i.e., earBuild.properties
Code:
#EAR Names
earName=XYZ.ear
OR
Property file can be with Multiple Ear names as:
Code:
#EAR Names
earName=XYZ.ear
earName=ABC.ear

I have written below shell script but loop is deleting all files in directory.

Code:
#!/bin/bash

echo "Reading property file at `pwd`"
ls -lrt

find /tmp/s/earProps/ -name earBuild.properties

FILE_NAME="earBuild.properties"
echo "EAR property file is: $FILE_NAME"

EARPATH="/tmp/s/props/ears/"

# Key in Property File
KEY="earName"

# Variable to hold the Property Value
prop_value=""

getProperty()
{
        prop_key=$1
        prop_value=`cat ${FILE_NAME} | grep ${prop_key} | cut -d'=' -f2`
}

getProperty ${KEY}
echo "KEY = ${KEY} ; Value = " ${prop_value}

for i in ${prop_value}; do
	find $EARPATH -type f ! -name $i -exec rm -rf {} \;
	echo "Value of I in loop is $i"
done

I am no bound to use find or rm or even for loop. Looking for the solution.
Appreciate help in advance.

Thanks
 
Old 08-29-2017, 10:08 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Each excluded file must be in the the same command. You can just print the results to test your program.

Code:
find . -maxdepth 1 ! -name  file_1 ! -name file_2  -type f  -print
 
Old 08-30-2017, 04:41 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
grep searches in the entire line, and can find a key within a value.
Improvement
Code:
getProperty()
{
        prop_key=$1
        prop_value=`awk -F= '$1==key {print $2}' key="$prop_key" "$FILE_NAME"`
}
rm -r is recursive, should not be used in the recursive find.
Code:
for i in ${prop_value}; do
	find "$EARPATH" -type f ! -name "$i" -exec rm -f {} \;
	echo "Value of I in loop is $i"
done
A good diagnostic is to put echo before the rm then you see what it would do!

Last edited by MadeInGermany; 08-30-2017 at 04:43 AM.
 
Old 08-30-2017, 05:58 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A function should be universal.
Using global variables is bad style.
I would return values on stdout like this
Code:
getProperty()
{
    awk -F= '$1==key {print $2}' key="$1" "$FILE_NAME"
}
And in the main code store it in a variable
Code:
prop_value=$(getProperty "$KEY")
Or pipe it to a while loop
Code:
getProperty "$KEY" |
while read i; do
	find "$EARPATH" -type f ! -name "$i" -exec rm -f {} \;
	echo "Value of I in loop is $i"
done
In this case it can all boil down to
Code:
while IFS="=" read k i; do
    if [ "$k" = "$KEY" ]; then
	find "$EARPATH" -type f ! -name "$i" -exec rm -f {} \;
	echo "Value of I in loop is $i"
    fi
done < "$FILE_NAME"
but maybe this is harder to understand/debug.
 
  


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
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 01:21 AM
Batch script remove old files based on their names and keep on for each version Reda01 Programming 6 05-08-2008 02:35 PM
how to remove long-windows-filename files based on exlusion list adamrosspayne Linux - Newbie 3 06-23-2006 02:25 AM
Remove files based on content stefaandk Linux - General 2 08-13-2005 08:03 AM

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

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