GagA// 0n D nEt

It is currently Sun May 20, 2018 3:34 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Script: Copying files using xargs to a backup directory
PostPosted: Fri Jan 16, 2009 6:07 am 
Offline

Joined: Wed Jul 18, 2007 1:14 am
Posts: 181
I want to copy just the files off my folder /tmp/gagan and not any of the folders and save it to directory that I may change from time to time. This backup directory will be provided by me to the script and the script will copy the files into that specified folder. Also the folder contains couple of other files as well, which are backup and backup.old which I don't want to be copied over.

The following script can accomplish the task:
Code:
#!/bin/bash
# copyfile.sh to copy the files, you can modify it to copy the directories as well
# Author: Gagan Brahmi

#  Copy (verbose) all files in the specified folder below (modify the location as per your requirement
#+ to directory specified on command line.

E_NOARGS=65

if [ -z "$1" ]   # Exit if no argument given.
then
  echo "Usage: `basename $0` directory-to-copy-to"
  exit $E_NOARGS
fi 

find /tmp/gagan/ -type f |grep -v backup | xargs -i -t cp -i {} $1
#                                                 ^^ ^^       ^^
#                -t is "verbose" (output command line to stderr) option.
#                -i is "replace strings" option.
#                {} is a placeholder for output text.
#
#  List the files in the specified folder (find /tmp/gagan -type f),
#+ pass the output of "find" as arguments to "xargs" (-i -t options),
#+ then copy (cp) these arguments ({}) to new directory ($1). 
#
#  The net result is the exact equivalent of
#+   cp * $1
#+ unless any of the filenames has embedded "whitespace" characters.

exit 0


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group