Register
It is currently Sat Apr 19, 2014 11:42 pm

Script for list files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Dec 03, 2008 5:22 am   

Joined: Wed Dec 03, 2008 5:19 am
Posts: 5
Hello.

I need an script that do a files list, I will explain:

I have a list of include directories "include.txt"

/etc
/home
/var


I have a list of exclude directories "exclude.txt"

/etc/postfix
/var/apt
/var/cache

I need do with find command list all files included in the directories in "include.txt" file and will exclude all files in the subdirectories of the exclude list to other list, for example "result.txt"


Any help will be appreciated.

Josep


Top
 Profile  
 PostPosted: Mon Dec 08, 2008 4:34 am   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
Dear Josep,

Here is the script:

Code:

#!/bin/bash

# include directories

include='include.txt'

# exclude directories

exclude='exclude.txt'

# results

result='result.txt'

[ ! -f $include ] && { echo "Invalid include file"; exit; }
[ ! -f $exclude ] && { echo "Invalid exclude file"; exit; }

# build search path string

for dir in $(cat $include)
do
  [ -d $dir ] && searchpath=$searchpath$dir" "
done

[ -z "$searchpath" ] && { echo "No valid search path found in include file"; exit; }

# build exclude path string

for dir in $(cat $exclude)
do
  [ -d $dir ] && excludepath=$excludepath" -path $dir -prune"
done
excludepath=${excludepath//-prune -path/-prune -o -path}
[ -n "$excludepath" ] && excludepath=$(echo \( $excludepath \) -o | tr -s " ")

# build find argument string

arg=$(echo "$searchpath $excludepath -type f -print" |  tr -s " ")

# run find

find $arg > $result



This script will examine the include and exclude files and then create strings of valid include directories and valid exclude directories. These strings will be used to create the final argument string to find command. The search result will be saved in the file specified by result variable.

I have tested the script for a few test cases and it worked well. Please test the script and let me know if this helps.

Thanks,
Lijeesh


Top
 Profile WWW YIM  
 PostPosted: Wed Dec 10, 2008 2:13 pm   

Joined: Wed Dec 03, 2008 5:19 am
Posts: 5
Hello Lijeesh,

The script don't runs for me , don't produces nothing, I have changed relative path of include.txt and exclude.txt and continue without success.

Maybe we are using different environment? I use debian lenny, if you want I can give You access to a fresh installed virtual machine with lenny for test purposes.

Thanks
Josep


Top
 Profile  
 PostPosted: Wed Dec 10, 2008 9:44 pm   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
Hi Josep,

If I can get access to a Lenny with a 'not working' example, it would be great. I may be able to troubleshoot.

BTW I tested the script in Ubuntu :-)

Regards,
Lijeesh


Top
 Profile WWW YIM  
 PostPosted: Sun Dec 28, 2008 3:23 am   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
The above script lists only files, not directories. This has been rectified by removing -type f. Final script follows:


Code:

#!/bin/bash

# include directories

include='include.txt'

# exclude directories

exclude='exclude.txt'

# results

result='result.txt'

[ ! -f $include ] && { echo "Invalid include file"; exit; }
[ ! -f $exclude ] && { echo "Invalid exclude file"; exit; }

# build search path string

for dir in $(cat $include)
do
  [ -d $dir ] && searchpath=$searchpath$dir" "
done

[ -z "$searchpath" ] && { echo "No valid search path found in include file"; exit; }

# build exclude path string

for dir in $(cat $exclude)
do
  [ -d $dir ] && excludepath=$excludepath" -path $dir -prune"
done
excludepath=${excludepath//-prune -path/-prune -o -path}
[ -n "$excludepath" ] && excludepath=$(echo \( $excludepath \) -o | tr -s " ")

# build find argument string

arg=$(echo "$searchpath $excludepath -print" |  tr -s " ")

# run find

find $arg > $result




Thanks,
Lijeesh


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP