I'm confused by your question.
As I understand it ls checks to see if stdout
is a tty and if not it outputs 1 file per line.
You can force this output format by doing ls -1
Aliases can confuse things so do /bin/ls -1 to
test the various options.
As for your problem, it might be better to use find
instead of ls to generate the file list? What happens
if you have a filename that contains a : or \n in the
example you present?
How about the following (I haven't tested it) ?
#!/bin/sh
#pass either filenames or nothing
#note doesn't touch directories
if [ $# = 0 ]
then
FILES=$(find -type f -maxdepth 1)
else
FILES="$@"
fi
for FILE in $FILES
do
DEST=$(echo $FILE | tr A-Z a-z)
if [ "$DEST" != "$FILE" ]
then
mv -i "$FILE" "$DEST" #-i prompts for confirmation (-f is
opposite)
fi
done
Note the above doesn't handle \n in a filename either,
but you can with a few more tr commands in various places.
Padraig.
> -----Original Message-----
> From: Dermot McGahon [mailto:dermot.mcgahon at tradesignals.com]
> Sent: 19 December 2000 11:56
> To: ilug at linux.ie> Subject: [ILUG] Why does each filename from ls have a newline?
>>>> This behaviour is great because you can pipe output from ls to other
> commands and it works like you would expect, for example:
>> ls -R | egrep -v ".*:" | egrep -v "^$" | mawk '{ printf "mv %s %s\n",
> $0, tolower($0) }'
>> but I don't understand why the output is presented column across using
> bash and an xterm, when there are embedded newlines? How does this
> work?
>> Also there is a -1 option to ls so that it gives one filename per line
> but specifying the -1 option or leaving it out won't make any
> difference to how the above pipeline will work. So, do any of you use
> the -1 option and what do you use it for.
>> And finally, are there are better ways to convert filenames from
> uppercase to lowercase? The other that I know of is find -exec tr. Are
> there more?
>> Dermot.
> --
>>dmcgahon at dnrc:~/xmlrpc$ ls | tr "\n" "|"
> ChangeLog|agesort.php|base64test.php|bettydemo.php|booltest.ph
> p|client.php|demo1.txt|demo2.txt|demo3.txt|doc|httptest.php|in
> trospect.php|introspect_demo.php|mail.php|server.php|stringtes
> t.php|test.pl|test.py|vardemo.php|workspace.testPhpServer.fttb
> |xmlrpc.inc|xmlrpcs.inc|zopetest.php|dmcgahon at dnrc:~/xmlrpc$
>>dmcgahon at dnrc:~/xmlrpc$ ls -R | tr "\n" "|"
> .:|ChangeLog|agesort.php|base64test.php|bettydemo.php|booltest
> .php|client.php|demo1.txt|demo2.txt|demo3.txt|doc|httptest.php
> |introspect.php|introspect_demo.php|mail.php|server.php|string
> test.php|test.pl|test.py|vardemo.php|workspace.testPhpServer.f
> ttb|xmlrpc.inc|xmlrpcs.inc|zopetest.php||./doc:|Makefile|apido
> cs.html|arrayuse.html|bugs.html|custom.dsl|debugging.html|exam
> ples.html|helpers.html|index.html|introduction.html|manifest.h
> tml|reserved.html|support.html|sysmethhelp.html|sysmethodsig.h
> tml|xmlrpc-server.html|xmlrpc_php.sgml|xmlrpcmsg.html|xmlrpcre
> sp.html|xmlrpcval.html|dmcgahon at dnrc:~/xmlrpc$
>>>>> --
> Irish Linux Users' Group: ilug at linux.ie>http://www.linux.ie/mailman/listinfo/ilug for
> (un)subscription information.
> List maintainer: listmaster at linux.ie>
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!