copy script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy script
# 1  
Old 06-25-2012
copy script

I have a little copy script,
Code:
#! /usr/bin/bash

FILE_COPY_LIST=$1
SOURCE_DIRECTORY=$2
DESTINATION_DIRECTORY=$3

while read file
do
   if [ ! -f $SOURCE_DIRECTORY'/'$file ]; then
      echo $file "  cannot be found"
   fi
   cp -f $SOURCE_DIRECTORY'/'$file  $DESTINATION_DIRECTORY
done < $FILE_COPY_LIST

This just accepts a file with a list of file names, source directory, and destination directory. This works fine for simple applications, but I would like to use a specific column in a multi column file as the file list.
Code:
id	file_name	IDNUMBER	data
1	ST4125183.txt	ST4125183	151231
2	ST4045313.txt	ST4045313	162214
3	ST4107613.txt	ST4107613	205239
4	ST4144567.txt	ST4144567	252357
5	ST090750.txt	ST090750	280367

and pass the column header as in argument,

copy.sh file_with_list file_name src_dir/ dest_dir/

This would save allot of time, but I'm not sure how to go about it.

Any advice would be appreciated,

LMHmedchem
# 2  
Old 06-26-2012
If your cp supports --target_directory:

Code:
#!/usr/bin/bash
FILE_LIST="$1"
FILE_FIELD="$2"
SOURCE_DIR="$3"
DEST_DIR="$4"
 
# Some validation
if [ $# -ne 4 ]
then
   echo "usage: $0 file_list field src_dir dest_dir" >&2
   exit 1
fi
if [ ! -f "$FILE_LIST" ]
then
    echo "Invalid filelist $FILE_LIST">&2
    exit 2
fi
if [ ! -d "$DEST_DIR" ]
then
    echo "Invalid dest directory">&2
    exit 3
fi
if [ ! -d "$SOURCE_DIR" ]
then
    echo "Invalid source directory">&2
    exit 4
fi
 
awk -vS="$SOURCE_DIR" -vFLD="$FILE_FIELD" 'NR==1{for(i=1;i<=NF;i++)if($i==FLD)w=i;next}
w {print S "/" $w}' $FILE_LIST | xargs -r cp --target-directory="$DEST_DIR"

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 06-26-2012
This is one way to place the file name(column 2) from the file into a variable:
Code:
for f in `sed '1 d' test.txt | awk '{print $2}'`
do
# $f contains your file name
  echo Your file name from 2nd column: ${f}
  file_name=`echo ${f}`
done

Your file name from 2nd column: ST4125183.txt
Your file name from 2nd column: ST4045313.txt
Your file name from 2nd column: ST4107613.txt
Your file name from 2nd column: ST4144567.txt
Your file name from 2nd column: ST090750.txt

This User Gave Thanks to spacebar For This Post:
# 4  
Old 06-26-2012
Quote:
Originally Posted by Chubler_XL
If your cp supports --target_directory:
It seems to, since that worked great. It seems a touch faster than the cp script as well.

Quote:
Originally Posted by spacebar
This is one way to place the file name(column 2) from the file into a variable:
I think this would work fine as well. I tend to prefer using a column header name instead of a location when possible. I always forget what the format is and it can be useful to be able to support more formats without having to edit the script.

LMHmedchem
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy script output to a variable using same script?

I'm trying to copy script output and use it in this same script as a variable, and call the variable when script is compiled. The script is below. #!/bin/bash output=$(script) while read line; do if ]; then grep "$line" logfile.txt # Create text file echo "From: IT ... (4 Replies)
Discussion started by: SysAdminRialto
4 Replies

2. Shell Programming and Scripting

Script for File Copy

Hello All, This is my first post to unix.com. I have requirement to copy multiple *.dmp files from one server to other server. I have that other server NFS filesystem mapped to source server. Dump files are so huge almost 20TB. There are around 15-20 files each of 1.1TB. I want to copy these... (6 Replies)
Discussion started by: Amit Bansode
6 Replies

3. Shell Programming and Scripting

Help with file copy script

Hello, I am still pretty new at writing shell scripts and may have bitten off more than I can chew. I am trying to write a script that figures out if a file exists (a known name) in directory a, then copy it to my selected destination, otherwise, look in directory b and check there. If it is in... (14 Replies)
Discussion started by: r34lj4k3
14 Replies

4. UNIX for Dummies Questions & Answers

Copy script

Hi Experts Hi I'm trying run the below code, unfortunately it throwing error. Error Message: tail:cannot open ‘ H:\Test_Folder\' No such file or directory tail: invalid option -3 cp ` ls -tr *REPORT*.txt |tail -3` H:\Test_Folder\ to give you more insight of my requirement: I am trying... (3 Replies)
Discussion started by: parpaa
3 Replies

5. UNIX for Dummies Questions & Answers

Help with Copy Shell Script

Hello, I am currently learning UNIX scripting and have written a simple copy program. However, upon execution, it returns an error despite debugging correctly. Can anyone assist in explaining this? Attached screenshots illustrating a test execution. Many thanks. (13 Replies)
Discussion started by: dixer
13 Replies

6. Shell Programming and Scripting

Remote Copy Script

Hi All, My first post on the Forums! I've just whipped up a quick script to slightly automate a log file retrieval process for work and to be completely honest my BASH scirpting knownledge is extremely limited as is my understanding of a lot of linux commands. Basically the scripts purpose... (3 Replies)
Discussion started by: effektd
3 Replies

7. Shell Programming and Scripting

Help with Find All/Copy Script

Hi Gang, I am an old unix user and just recently returned in the flavor of a Mac. I have tons of pictures in the .orf format and would like to write a script to: Search the Hard drives for that file type Then, depending on date, copy it to a specific folder If there is an exact... (2 Replies)
Discussion started by: jlfx
2 Replies

8. Shell Programming and Scripting

Help:Copy file from one to other using script

Hi Frineds, Through my DTS process I am generating one file at one directory like, /sqlsrvr_demo/dts/put/transaction_fact.csv now I want to copy this files(only when the files size is greater than 0 kb) to some other directory like /sqlsrvr_demo/dts/get/transaction_fact.csv Can... (2 Replies)
Discussion started by: sunnysunny
2 Replies

9. Shell Programming and Scripting

copy script

I am trying to figure out how to copy a file that changes daily to a new directory with a different name each day. Example : webfile created today copy webfile to /hold/webfile.1 tommorrow copy webfile /hold/webfile.2 Is there a way to do this? I can't seem to figure this out. This needs to... (2 Replies)
Discussion started by: scbrown2
2 Replies

10. UNIX for Dummies Questions & Answers

Copy Script Problems .....

I got this script: print -n "Enter file name: " read name .... ..... ..... etc but at the prmpt, when I press enter (without typin a word), comes up with sum error message, is there away of getting it not to print that message?? (8 Replies)
Discussion started by: Makaveli.2003
8 Replies
Login or Register to Ask a Question