Wrong output when writing to file

Hello,

I am having problem while redirecting output to a file where as on console output is proper.

for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; echo; done > output.txt

Output of above command is coming in single line but when i am redirecting output to a file, single line i breaking into multiple line.

Sample Output of Console(output is coming in single line) :

activestop.nicorette/, activestop_nicorette, prod-activestop_nicorette, prod-activestop_nicorette, prod-activestop_nicorette

Sample output in file (same format is coming in file):

activestop.nicorette/, activestop_nicorette, 
activestop_nicorette,activestop_nicorette,
activestop_nicorette

please suggest.

You need to make it a single line list first.

list="";for l in *;do list+=", $l";done;echo "$list" > output.txt

Which is just the same as:

list=""
for l in *
do  list+=", $l"
done
echo "$list" > output.txt

hth

Its is just printing main directories not the sub-directories/files..

Define your desired output and give an example of input.
Also in your example code, you do quite a double work, since ls -m already produces the output you listed.

You might just change to:

ls -m "$dir"/*

Thought , you need to check if its really a directory first.

hth

Not sure what you want, find might be helpful

find / will give you EVERY directory and file

find . will give you EVERY directory and file in your current directory

The description of the ls -m option in the standards is:

The default line width for a regular file is probably something like 80. I have no idea what line length you have set up for your console.

You could try something like:

COLUMNS=1000000 ls -md */ */*/

to list directories in the current directory and the directories in those directories on one line (assuming that no more than one million display columns are needed to print that line). Note, however, that if the value you set for COLUMNS is larger than the value you get from:

getconf LINE_MAX

(which on many systems is 2048), then the standard text processing utilities might not work on your resulting file.

And, note that your description of what you are trying to print is very vague. Without some idea of what your file structure looks like and what you hope to print from that file structure, we are all just guessing.

Hi Sea,

Below is the code i am using. On console output is fine but when i am redirecting output to file then single line is breaking in multiple lines. Can you please suggest..

[root@testserver httpd]# for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; done
buckets/, apr_bucasdfsadfa_fasdfasfaool.lo, apr_buckets_simpleapr_buckets_simple.lo, apr_buckets_socketapr_buckets.lo, apr_buowekrfwqereqjncckets_reqwer.lo
build/, config_vars.sh.in, export_varsexport_varsexport_vars.pl, mkdepphf_abuse_logphf_abuse_logp.perl, rules.mksocketapr_buckets.insocketapr_buckets
include/, util_md5util_md5util.h, util_scriptutil_scriptutit.h, util_timeutil_hutil_time.h, util_xmlutil_xml.h
os/, ap_regkeyap_regkey.c, MakefileMakefile.in, MakefileMakefileMake, osabuse_logabuse_log.h
server/, export_filesexport_files, export_vars.hexport_vars.h, httpd.exphttpd.exp, libmain.lalibmain.la
support/, logresolve.pllogresolve, log_server_statuslog_server_status, phf_abuse_phf_abuse_log.cgi, split-logfilesplit-log
[root@testserver httpd]# for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; done > output.txt
[root@testserver httpd]# cat output.txt
buckets/, apr_bucasdfsadfa_fasdfasfaool.lo, apr_buckets_simpleapr_buckets_simple.lo,
apr_buckets_socketapr_buckets.lo, apr_buowekrfwqereqjncckets_reqwer.lo
build/, config_vars.sh.in, export_varsexport_varsexport_vars.pl,
mkdepphf_abuse_logphf_abuse_logp.perl,
rules.mksocketapr_buckets.insocketapr_buckets
include/, util_md5util_md5util.h, util_scriptutil_scriptutit.h,
util_timeutil_hutil_time.h, util_xmlutil_xml.h
os/, ap_regkeyap_regkey.c, MakefileMakefile.in, MakefileMakefileMake,
osabuse_logabuse_log.h
server/, export_filesexport_files, export_vars.hexport_vars.h, httpd.exphttpd.exp,
libmain.lalibmain.la
support/, logresolve.pllogresolve, log_server_statuslog_server_status,
phf_abuse_phf_abuse_log.cgi, split-logfilesplit-log
[root@testserver httpd]#
  1. You did not use the code sample provided
  2. You did not apply the change Don Cragun mentioned.

Apply/Use either of it to get what you want.
hth

Thanks but that is not working...

[root@testserver httpd]# ls -md */ */*/

ls: cannot access */*/: No such file or directory
buckets/, build/, include/, os/, server/, support/

[root@testserver httpd]# ls -m

buckets, build, include, os, output.txt, server, support

I Tried below but it is not giving me proper output.

[root@testserver httpd]# ls -mRL
.:
buckets, build, include, os, output.txt, server, support

./buckets:
apr_bucasdfsadfa_fasdfasfaool.lo, apr_buckets_simpleapr_buckets_simple.lo, apr_buckets_socketapr_buckets.lo, apr_buowekrfwqereqjncckets_reqwer.lo

./build:
config_vars.sh.in, export_varsexport_varsexport_vars.pl, mkdepphf_abuse_logphf_abuse_logp.perl, rules.mksocketapr_buckets.insocketapr_buckets

./include:
util_md5util_md5util.h, util_scriptutil_scriptutit.h, util_timeutil_hutil_time.h, util_xmlutil_xml.h

./os:
ap_regkeyap_regkey.c, MakefileMakefile.in, MakefileMakefileMake, osabuse_logabuse_log.h

./server:
export_filesexport_files, export_vars.hexport_vars.h, httpd.exphttpd.exp, libmain.lalibmain.la

./support:
logresolve.pllogresolve, log_server_statuslog_server_status, phf_abuse_phf_abuse_log.cgi, split-logfilesplit-log

Why don't you adopt Don Cragun's proposal and adapt your own one liner:

for dir in */; do printf "%s, " "$dir"; COLUMNS=1000000 ls -m "$dir"; done 

Sorry for not understanding Don's suggestion properly.

Now my script is working perfectly. :slight_smile:

Thanks to you and Don for help...