Ls -ltr in scripting

Hi All,

I wrote below script

for i in `ls`
do
echo "list of files $i"
done

but i want display as like ls -ltr,it's displaying as one column

Thanks

Use for i in $(ls -ltr) , then, abandoning the deprecated `...` nomenclature.

Sure you need the -tr ?

Use \n in the echo statement

It is not clear, what you want to achieve.. Your loop puts "list of files" in front of every filename. This would show an ls -ltr version of that:

ls -ltr | sed 's/^/list of files /'

But that is probably not what you are looking for ...

Assuming Scrutinizer guessed correctly at the output you want, and that you don't mind changing the value of IFS in your script, you could get by without sed :

# Note: There is nothing but a <newline> character between the single quotes
# on the following two lines.  You could use IFS=$'\n' with a recent bash or ksh.
IFS='
'
for i in $(ls -ltr)
do	printf 'List of files %s\n' "$i"
done

Or, if you need to preserve and restore IFS:

IFS_set=${IFS-unset}
IFS_old=${IFS}
# Note: There is nothing but a <newline> character between the single quotes
# on the following two lines.  You could use IFS=$'\n' with a recent bash or ksh.
IFS='
'
for i in $(ls -ltr)
do	printf 'List of files %s\n' "$i"
done
if [ "$IFS_set" = "unset" ]
then	unset IFS
else	IFS=$IFS_old
fi

Both of the above produce output like:

List of files total 16
List of files -rw-r--r--  1 dwc  staff  330 Jun 13 23:37 problem
List of files -rwxr-xr-x  1 dwc  staff  182 Jun 13 23:38 tester
printf "List of files %s\n" $(ls -ltr)

List of files total 12
List of files -rw-rw-r--. 1 aia aia  148 Jun  7 19:49 index.html
List of files -rw-rw-r--. 1 aia aia 1446 Jun 12 20:27 pvz.html
List of files -rw-rw-r--. 1 aia aia 1984 Jun 13 22:50 pvz.bgcolor.html
List of files drwxrwxr-x. 2 aia aia   23 Jun 14 12:06 pvz

Longhand using OSX 10.7.5, default terminal using sh...
Think of your problem and adapt. As you were attempting to use builtins then 'KIS', (Keep It Simple).

Last login: Sun Jun 14 20:01:53 on ttys000
AMIGA:barrywalker~> sh
AMIGA:barrywalker~> ls -ltr > /tmp/listing
AMIGA:barrywalker~> while read line; do echo "List of files $line"; done < /tmp/listing
List of files total 3056
List of files drwxr-xr-x+  5 barrywalker  staff     170 29 Mar  2013 Public
List of files drwxr-xr-x   3 barrywalker  staff     102  6 May  2014 URL
List of files drwx------+  5 barrywalker  staff     170 17 Oct  2014 Music
List of files drwx------+  5 barrywalker  staff     170 17 Oct  2014 Pictures
List of files drwx------+  4 barrywalker  staff     136 17 Oct  2014 Documents
List of files drwx------@ 38 barrywalker  staff    1292 24 Oct  2014 Library
List of files -rwxr-xr-x   1 barrywalker  staff  128656 22 Nov  2014 03080.sh
List of files -rwxr-xr-x   1 barrywalker  staff  128990  6 Dec  2014 03083.sh
List of files -rwxr-xr-x   1 barrywalker  staff  131697  7 Dec  2014 03090.sh
List of files -rwxr-xr-x   1 barrywalker  staff  134772 29 Dec 21:05 03100.sh
List of files -rwxr-xr-x   1 barrywalker  staff  135072 30 Jan 20:30 03120.sh
List of files drwxr-xr-x  42 barrywalker  staff    1428  2 Feb 22:14 Scope
List of files -r--------   1 barrywalker  staff  140746  3 Apr 11:32 03140.sh
List of files drwxr-xr-x  15 barrywalker  staff     510  6 Apr 18:16 sox-14.4.0
List of files -rw-r--r--   1 barrywalker  staff  146294  9 May 17:02 03180.sh
List of files -rwxr-xr-x   1 barrywalker  staff  146712 19 May 19:25 AudioScope.txt
List of files -rwxr-xr-x   1 barrywalker  staff    1452 23 May 23:26 AAWG
List of files drwx------+ 11 barrywalker  staff     374 30 May 14:29 Desktop
List of files drwx------+  5 barrywalker  staff     170  1 Jun 15:44 Movies
List of files -rwxr-xr-x   1 barrywalker  staff  147913  1 Jun 22:02 AudioScope.sh.txt
List of files -rw-r--r--   1 barrywalker  staff  147913  2 Jun 18:59 03197.sh
List of files -rwxr-xr-x   1 barrywalker  staff  149973  2 Jun 22:15 AudioScope.sh
List of files drwx------+ 18 barrywalker  staff     612 11 Jun 09:59 Downloads
AMIGA:barrywalker~> exit
exit
AMIGA:barrywalker~> exit
logout

[Process completed]

... provided IFS is set to $'\n'

Expanding on what Scrutinizer said... As already explained in this thread, this only works if IFS is set to the <newline> character.

With a default setting of IFS ( IFS=$' \t\n' ), that command would produce something more like:

List of files total
List of filesl 12
List of files -rw-rw-r--.
List of files 1
List of files aia
List of files aia
List of files 148
List of files Jun
List of files 7
List of files 19:49
List of files index.html
... ... ...

And, you get the same results with:

IFS=$'\n' printf "List of files %s\n" $(ls -ltr)

because that only sets IFS in the environment for the invocation of printf ; not for the evaluation of the results of the command substitution done in the current shell execution environment when preparing arguments to be passed to printf .

It puzzles me that since it was "already explained in this thread" you have to repeat again what it was explained.

Can I choose to let whoever comes across the thread to make that determination of understanding? May I post, without arousing your urge to comment back in a rectifying tone?

I apologize if I have offended you.

I believe The UNIX & Linux Forums is a place where inexperienced users can learn how to effectively to get their jobs done using the tools available on UNIX systems, Linux systems, and other systems that provide UNIX system or Linux system utilities.

When posts are added to a discussion suggesting that the submitter use something that won't work 99% of the time, the other people reading the thread can choose to ignore it and assume the newbies will eventually figure out that the suggestions found here are not moderated, may be more confusing than helpful, and it is up to them to determine the usefulness of the suggestions. Or an interested reader might try to explain why a given suggestion won't (at least in many circumstances) work so newbies can learn more quickly what works, what doesn't work, and why.

Sorry, I apologize, as well.

1 Like

Thanks All,it's working fine i use IFS option

Instead of saving and restoring IFS, one can set IFS in a sub-shell:

(
IFS='
'
cnt=0
for i in $(ls -ltr)
do
  cnt=$((cnt+1))
  echo "line ${cnt}: $i"
done
echo "total: $cnt lines"
)
# won't work in the main shell: echo "total: $cnt lines"

Of course any variable that is set in the sub-shell will not be in the main shell.
Also a while loop runs in a sub-shell (at least if fed from a pipe):

cnt=0
ls -ltr |
while read i
do
  cnt=$((cnt+1))
  echo "line ${cnt}: $i"
done
# won't work in the main shell: echo "total: $cnt lines"

Or in ksh, bash, zsh, a way to use IFS without saving its value, and without an extra subshell, is by using a function with a local variable:

prt_list() {
  typeset IFS="
"
  printf "List of files %s\n" $(ls -ltr)
}
prt_list
1 Like