954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Shell Script to Copy Files

i,

Here is the scenario I'm dealing with:

My company has a bunch of old servers and workstations that have data on them that I need to copy to a "drobo" (multi-storage device) and eventually send the storage unit to someone else. I have a list of "contract numbers" that I need to search for on the disks and then copy them to the storage media.

Here's what I've tried so far:

a) I've created a list with all the contract numbers I need to search for and placed them in a file
b) Tried creating a foreach/for script that cat's the file:

foreach n (`cat list.txt`)
foreach? ls -lR
foreach? grep -i $n
foreach? cpio -pdmv /destination/
foreach? end

For one I don't know how to put that all into a file and create a real loop and the cpio copy fails giving me errors that it "there's no files found.

Hopefully, I've given you enough info to at least get us started. I know it's a simple script and won't require much, but it's something that would really help.

If you could do this asap I would cetainly appreciate it

Thanks

gmdune
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
#!/bin/bash

# does this help?

# Make a list of files to process:
# it is ls -"one" not -"el"
ls -1 >> list_of_files.txt
find /xxx/yyy -name *.txt >> list_of_files.txt

for file_name in `cat list_of_files.txt`
do
  # process the file e.g.
  [ ! -f $file_name ] && echo "$file_name does not exist. Check if it is absolute path or relative path. If it is relative path ensure you are running the script in correct directory."
  echo cp $file_name some_dir
done

# if the list_of_files.txt is very big you can do this as well:
while read file_name
do
  # process the file e.g.
  [ ! -f $file_name ] && echo "$file_name does not exist. Check if it is absolute path or relative path. If it is relative path ensure you are running the script in correct directory."
  echo cp $file_name some_dir
done < list_of_files.txt


PS:
ls and find (depending on arguments) return relative paths, so ensure that either your script is run in the correct directory. Or edit the list_of_files.txt to make all paths absolute.
E.g.

my_curr_dir=`pwd`
ls -1 . >> list_of_files.txt
echo "relative paths:"
cat list_of_files.txt

echo "absolute paths:"
sed "s/^/$(echo $my_curr_dir/ | sed 's/[/]/\\\//g')/" list_of_files.txt
# edit the file itself:
sed -i "s/^/$(echo $my_curr_dir/ | sed 's/[/]/\\\//g')/" list_of_files.txt
echo "absolute paths inside the file:"
cat list_of_files.txt
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

Hey man, thanks so much for your quick reply and help...I really appreciate it...SOLVED!

gmdune
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You