[ILUG] Why does each filename from ls have a newline?

Dermot McGahon dermot.mcgahon at tradesignals.com
Tue Dec 19 12:55:45 GMT 2000


>>>>> "Padraig" == Brady, Padraig <Padraig.Brady at compaq.com> writes:

    Padraig> I'm confused by your question.  

Sorry. That's probably due to the fact that I was confused while
asking the question.

    Padraig> As I understand it ls checks to see if stdout is a tty
    Padraig> and if not it outputs 1 file per line.  You can force
    Padraig> this output format by doing ls -1 Aliases can confuse
    Padraig> things so do /bin/ls -1 to test the various options.

Yes. Harry and others have already explained that and it rang a bell. 
Can you rely on all shell commands working this way or do you have to
take it on a one by one basis?

ls -1 is redundant for pipelines though, isn't it? Maybe you would
want to use ls -1 if you were redirecting output to a file for later
processing.

    Padraig> As for your problem, it might be better to use find
    Padraig> instead of ls to generate the file list? What happens if
    Padraig> you have a filename that contains a : or \n in the
    Padraig> example you present?

Yes. That is a weakness for sure.

    Padraig> How about the following (I haven't tested it) ?

    Padraig> #!/bin/sh #pass either filenames or nothing #note doesn't
    Padraig> touch directories

    Padraig> if [ $# = 0 ] then FILES=$(find -type f -maxdepth 1) else
    Padraig> FILES="$@" fi

    Padraig> for FILE in $FILES do DEST=$(echo $FILE | tr A-Z a-z) if
    Padraig> [ "$DEST" != "$FILE" ] then mv -i "$FILE" "$DEST" #-i
    Padraig> prompts for confirmation (-f is opposite) fi done

    Padraig> Note the above doesn't handle \n in a filename either,
    Padraig> but you can with a few more tr commands in various
    Padraig> places.

That is excellent. Thank you very much. It removes all the non-ops for
files which are already all lowercase although mv probably doesn't do
too much processing in that case anyway.

This was also suggested:

  find root_of_cd -depth -exec mv {} `echo {} | tr A-Z a-z` \;

which depends on the version of find being able to reference {} twice
but most modern finds allow that.

Thanks for your help. I am much less confused now.

Dermot.
--





More information about the ILUG mailing list