Results 1 to 5 of 5
Ok so I've got a text file with a list of .gz files, these .gz files are in various sub directories of one parent directory and I've hacked this little ...
- 04-15-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 1
Scripting help, copying files listed in a text file
Ok so I've got a text file with a list of .gz files, these .gz files are in various sub directories of one parent directory and I've hacked this little script together to copy them from their current location to a new one and spit out any it can't find to "/home/user/not_found" but for the life of me can't get it to run properly!
Any help would be greatly appreciated.
The shell script as it currently looks:
Code:#!/bin/sh source="/mnt/source_dir" dest="/mnt/output_dir" list="/home/user/file_list" while read -r line do file=`find "$source" -name "$line"` if [ -n "$file" ] then cp $file $dest else echo "$line" >> "/home/user/not_found" fi done<$list
- 04-15-2010 #2
what if it was:
Code:if [ -z $file ]; then echo $line >> /home/user/not_found else cp $file $dest fi
linux user # 503963
- 04-16-2010 #3Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
Theis testing if the string is not equal to ''.Code:-n "$file"
TheCode:#!/bin/sh source="/mnt/source_dir" dest="/mnt/output_dir" list="/home/user/file_list" while read -r line do file=`find "$source" -name "$line"` if [ -f "$file" ] ; then cp $file $dest else echo "$line" >> "/home/user/not_found" fi done<$listis testing if the file exits and is a regular file.Code:-f "$file"
- 04-16-2010 #4Just Joined!
- Join Date
- Oct 2006
- Posts
- 1
Let's assume the parent directory is /folder
now in shell do like..
#for i in `find /folder -type f | grep .gz`; do cp -prf $i /path/to/destination_folder; done
- 04-16-2010 #5Just Joined!
- Join Date
- Sep 2006
- Location
- Rochester
- Posts
- 17
Scripting help
Having defined list, now you need to read the contents of that file.
I know of two quick ways to do that:
1) exec 3<$list
while read -r line <&3
do
<stuff>
done
2) for line in `cat $list`
do
<stuff>
done
Both will work, but #2 has problems if there are spaces in the file names.
Also, #2 will blow up if the character count in the list file is large.
Good luck with your scripting.


Reply With Quote
