Find the answer to your Linux question:
Results 1 to 5 of 5
filename is a list of absolute paths to files on individual lines I want to use filename as input to /bin/mv: mv `cat filename` destination/ Problem is there are whitespaces ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    file for input to /bin/mv

    filename is a list of absolute paths to files on individual lines

    I want to use filename as input to /bin/mv:

    mv `cat filename` destination/

    Problem is there are whitespaces in the pathnames. And if I escape the whitespaces

    sed -E 's/\ /\\ '/g filename > filenameclean

    mv `cat filenameclean` destination/

    still fails (operating on paths with spaces as multiple arguements)

    Any thoughts?

    Thanks.

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Try enclosing filename in double quotes:

    mv `cat "filename"` destination/

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    3

    have u tryed

    "mv `cat filename` destination/"
    or maybe
    cat filename | mv destination
    mv `cat filename` "destination"
    one has to work

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The problem here is that by using `cat filename`, you are printing out the file (spaces and all) to the commandline and then relying on Bash to parse them (and Bash breaks on spaces).

    There are two ways to do this.

    The first is using a loop to go through the file:
    Code:
    exec 3< filename; while read line <&3; do mv "$line" destination/; done
    We read each line of filename, inserting each line into the 'mv' command.

    The second way to do this is to use a program written for this purpose: xargs. The job of xargs is to read many lines and run the same command on each of them:
    Code:
    xargs -I{} mv "{}" destination/ < filename
    Here we redirect xargs's STDIN to be from filename, and let it essentially do the same thing as the loop above.

    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    May 2007
    Posts
    2
    Quote Originally Posted by Cabhan View Post
    Code:
    xargs -I{} mv "{}" destination/ < filename
    Does this make sense?
    This works perfectly, thank you. I looked at xargs before but didn't have the syntax right. Thank you for clearing it up for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...