Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Copy files listed in a text file - whitespace problem.

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-20-2008
60doses 60doses is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 13 April 2010, 10:37 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Copy files listed in a text file - whitespace problem.

Hi,

Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location.


Code:
$ more copy.out
dir1/file1
dir1/file2
dir1/file3
"dir1/white space"
dir1/file4

If I do the following:


Code:
$copy=`more copy.out`

$echo $copy
dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4

$cp -v $copy ./temp
dir1/file4 -> ./temp/file4
cp: space": No such file or directory
cp: "dir1/white: No such file or directory
dir1/file3 -> ./temp/file3
dir1/file2 -> ./temp/file2
dir1/file1 -> ./temp/file1

All of the files managed to be copied except for the file with a whitespace.

I have tried using for loop, but the same thing happened.


Code:
$ copy=`while read line; do echo "$line"; done < copy.out`

$ echo $copy
dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4

$ cp -v $copy ./temp
dir1/file4 -> ./temp/file4
cp: space": No such file or directory
cp: "dir1/white: No such file or directory
dir1/file3 -> ./temp/file3
dir1/file2 -> ./temp/file2
dir1/file1 -> ./temp/file1


I edited copy.out and replace "dir1/white space" with dir1/white\ space but it didn't work. Just by simply removing the double quotes failed to work either. I understand that by using echo, it will flatten any whitespace in $variable into a single space, so I've tried using double quotes $variable, but again, it failed to copy the file with a whitespace.

I don't like whitespace in files/dirs name, but some people seem to like it, so I'd like to know how to fix this problem.

Any advice offered is appreciated.

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 10-20-2008
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 123 Times in 116 Posts

Code:
$ cat copy.out
dir1/file1
dir1/file2
dir1/file3
dir1/white space
dir1/file4
$ while read file;do echo cp "$file" ./Newdir;done <copy.out
cp dir1/file1 ./Newdir
cp dir1/file2 ./Newdir
cp dir1/file3 ./Newdir
cp dir1/white space ./Newdir
cp dir1/file4 ./Newdir

Sponsored Links
    #3  
Old Unix and Linux 10-20-2008
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 9 January 2017, 4:40 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 Posts

Code:
$ (IFS=$'\n"';cp -v $(<copy.out) dir2)
`dir1/file1' -> `dir2/file1'
`dir1/file2' -> `dir2/file2'
`dir1/file3' -> `dir2/file3'
`dir1/white space' -> `dir2/white space'
`dir1/file4' -> `dir2/file4'

    #4  
Old Unix and Linux 10-20-2008
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts

Remove the quotes from the name in copy.out, and change IFS to a newline:


Code:
IFS='
'
cp $( cat copy.out ) ./temp

Sponsored Links
    #5  
Old Unix and Linux 10-20-2008
60doses 60doses is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 13 April 2010, 10:37 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
danmero, radoulov, cfajohnson:

All solutions worked Linux

Thank you so much!
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
copy files inside a text file hc17972 Homework & Coursework Questions 1 01-25-2011 07:39 AM
copy files inside a text file hc17972 Homework & Coursework Questions 1 01-25-2011 07:13 AM
Send a mail to IDs listed in a text file Sriranga Shell Programming and Scripting 5 03-11-2010 12:48 PM
Shellscript to sort duplicate files listed in a text file deaddevil Shell Programming and Scripting 11 11-02-2009 04:54 AM
CVSWeb - Directories listed but files not listed ganesh HP-UX 0 09-16-2005 04:32 PM



All times are GMT -4. The time now is 08:08 AM.