The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy Compress as one command findprakash UNIX for Dummies Questions & Answers 10 03-08-2009 01:19 PM
copy command failure mynix Shell Programming and Scripting 1 12-16-2007 12:35 AM
copy command sadiquep Linux 2 04-03-2007 12:01 AM
copy command perleo Shell Programming and Scripting 1 12-08-2002 07:24 PM
What is the command to copy a file DamienVall UNIX for Dummies Questions & Answers 5 05-24-2001 05:27 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-02-2007
ranga27 ranga27 is offline
Registered User
  
 

Join Date: May 2006
Posts: 63
Need help in Copy Command

i need to copy some files from a directory and move the copy files to some destination with the extension .dat

Can any one help on this
  #2 (permalink)  
Old 08-02-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,954
Code:
  ls -1 | while read file
    do
    cp $file /some/directory/.
    mv $file /some/mv/directory/${file}.dat
    done
  #3 (permalink)  
Old 08-03-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by matrixmadhan View Post
Code:
  ls -1 | while read file
    do
    cp $file /some/directory/.
    mv $file /some/mv/directory/${file}.dat
    done

The -1 is unnecessary, as ls always prints one file to a line when the destination is not a terminal.

More serious is that ls itself is not only unnecessary, but will break the script if any files are badly named with leading or trailing whitespace. You can correct that with:

Code:
ls | while IFS= read -r file
The script will still fail, however, if any filenames contain spaces since you didn't quote the file variable. When dealing with filenames, always use filename expansion (globbing) rather than a command, and quote the variable, e.g.:

Code:
for file in *
do
  mv "$file" "/some/mv/directory/${file}.dat"
done
  #4 (permalink)  
Old 08-03-2007
ranga27 ranga27 is offline
Registered User
  
 

Join Date: May 2006
Posts: 63
Thanks for the replies.

Just wanted to make sure if the destination is an ftp server and has an user id and password is there a way to copy the files .. i need to only copy the files not move the files ..

can any one help me on this as i am new to this shell scripting area ..


Thanks again !!!!
  #5 (permalink)  
Old 08-03-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,954
Quote:
The -1 is unnecessary, as ls always prints one file to a line when the destination is not a terminal.

More serious is that ls itself is not only unnecessary, but will break the script if any files are badly named with leading or trailing whitespace. You can correct that with:
I would like to really thank you for correcting and improvising the solution posted.

Many thanks for that !

I feel along with learning new techniques if we could sharpen what we have learnt already; it would help us to know better.
  #6 (permalink)  
Old 08-03-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ranga27 View Post
i need to copy some files from a directory and move the copy files to some destination with the extension .dat
Code:
copy_destination=/path/to/a/directory
dat_destination=/path/to/some/destination
cp * "$copy_destination"
for file in *
do
  mv "$file" "$dat_destination/$file.dat"
done
Replace the directory paths with the correct paths, and if you do not want to copy and move all the files, change * to whatever files you do want to copy.

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0