Find the answer to your Linux question:
Results 1 to 6 of 6
Hi Folks, i have a strange problem: try something like that: create a directory which contains files file1.txt file2.txt file3.txt i am a file with blanks.txt then try this find ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    3

    Post Problem: Output of Files with blanks, causes Line break


    Hi Folks,

    i have a strange problem:

    try something like that:

    create a directory which contains files

    file1.txt
    file2.txt
    file3.txt
    i am a file with blanks.txt

    then try this

    find $SOURCE -type f > /var/tmp/tmparchfiles

    for FILE in `cat /var/tmp/tmparchfiles`
    do
    echo $FILE
    done

    you will see, that the output will be something like this:

    ./yourdir/file1.txt
    ./yourdir/file2.txt
    ./yourdir/file3.txt
    ./yourdir/i
    am
    a
    file
    with
    blanks.txt

    not as ihoped:

    ./yourdir/file1.txt
    ./yourdir/file2.txt
    ./yourdir/file3.txt
    ./yourdir/i am a file with blanks.txt

    this is very uncool, because the filename is "broken"

    this is already one of my workarounds, the first script looked like this:

    for FILE in `find $SOURCE -type f`
    do
    echo $FILE
    done

    but this is the same

    even with print its the same, do you have an idea how to get the names "without" the line brake?


    Thank you in advance

    Ra

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    This behaviour is expected. Use quotation marks (like this: "$VAR") to suppress it.

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    3
    even with quotationmarks, you have the same output

  4. #4
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    117
    Quote Originally Posted by raoulvb View Post
    Hi Folks,

    i have a strange problem:

    try something like that:

    create a directory which contains files

    file1.txt
    file2.txt
    file3.txt
    i am a file with blanks.txt

    then try this

    find $SOURCE -type f > /var/tmp/tmparchfiles

    for FILE in `cat /var/tmp/tmparchfiles`
    do
    echo $FILE
    done
    Here's a way to do it:

    cat /var/tmp/tmparchfiles | xargs --delim '\n' echo

    Of course "xargs" collects arguments from its input, so that it can deliver a large number of them to the command it's supposed to run all at once (so, for instance, "find | xargs rm" could find 10,000 files but only actually invoke "rm" a few times...) This may not be what you want. In this case:

    cat /var/tmp/tmparchfiles | xargs --delim '\n' -n 1 echo

    (This is a "useless use of cat" of course, but somehow it doesn't bother me. )


    Another way to do it is to open the file within the shell, and explicitly read a line at a time from it:

    exec 7</var/tmp/tmparchfiles #File descriptor 7 is now open for reading
    while read f <&7; do echo $f; done
    exec 7<&-
    #close file descriptor 7


    Another option is to change the value of the $IFS variable so Bash will split arguments on newlines rather than on any whitespace character:

    IFS="\n"
    for f in $(cat /var/tmp/tmparchfiles); do echo $f; done


    If you go this route, you'll probably want to change $IFS back to its old value immediately after doing this...


    This sort of thing is one of my biggest pet peeves about the Unix shell. There's not a clear-cut way of delimiting multiple, successive items to be processed. Programs commonly separate values with newlines, but the shell separates things with whitespace...

  5. #5
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    I would go for the approach without the temporary file list:

    find $SOURCE -type f -exec echo "{}" \;

  6. #6
    Just Joined!
    Join Date
    Jun 2009
    Posts
    3

    Thumbs up

    Quote Originally Posted by tetsujin View Post
    Here's a way to do it:

    cat /var/tmp/tmparchfiles | xargs --delim '\n' echo

    Of course "xargs" collects arguments from its input, so that it can deliver a large number of them to the command it's supposed to run all at once (so, for instance, "find | xargs rm" could find 10,000 files but only actually invoke "rm" a few times...) This may not be what you want. In this case:

    cat /var/tmp/tmparchfiles | xargs --delim '\n' -n 1 echo

    (This is a "useless use of cat" of course, but somehow it doesn't bother me. )


    Another way to do it is to open the file within the shell, and explicitly read a line at a time from it:

    exec 7</var/tmp/tmparchfiles #File descriptor 7 is now open for reading
    while read f <&7; do echo $f; done
    exec 7<&-
    #close file descriptor 7



    Another option is to change the value of the $IFS variable so Bash will split arguments on newlines rather than on any whitespace character:

    IFS="\n"
    for f in $(cat /var/tmp/tmparchfiles); do echo $f; done


    If you go this route, you'll probably want to change $IFS back to its old value immediately after doing this...


    This sort of thing is one of my biggest pet peeves about the Unix shell. There's not a clear-cut way of delimiting multiple, successive items to be processed. Programs commonly separate values with newlines, but the shell separates things with whitespace...

    Thank you alot, the underlined one works great for me!

    very nice

    @secondmouse

    im sorry, this doesn´t work for me, because i have several functions that needs the filename, so execute would only work if i outsource this to a second script.

    Just for your information, this script is used to perform an archive operation, with mysql documentation in a fully normalized database

    thank you alot, for the kind support.

    Ra

Posting Permissions

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