Ls -ltr in scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ls -ltr in scripting
# 8  
Old 06-14-2015
Quote:
Originally Posted by Aia
Code:
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

... provided IFS is set to $'\n'
# 9  
Old 06-14-2015
Quote:
Originally Posted by Aia
Code:
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

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:
Code:
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:
Code:
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.

Last edited by Don Cragun; 06-14-2015 at 06:57 PM.. Reason: Add note...
# 10  
Old 06-14-2015
Quote:
Originally Posted by Don Cragun
Expanding on what Scrutinizer said... As already explained in this thread, this only works if IFS is set to the <newline> character.
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?
# 11  
Old 06-14-2015
Quote:
Originally Posted by Aia
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.
# 12  
Old 06-14-2015
Quote:
Originally Posted by Don Cragun
I apologize if I have offended you.
[..]
Sorry, I apologize, as well.
This User Gave Thanks to Aia For This Post:
# 13  
Old 06-15-2015
Thanks All,it's working fine i use IFS option
# 14  
Old 06-15-2015
Instead of saving and restoring IFS, one can set IFS in a sub-shell:
Code:
(
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):
Code:
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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fields in the Output of ls -ltr for a directory

Could you please let me know what each of the output fields in ls -ltr for a directory imply. Example : drwxrwsr-x 4294967295 infamgr infagrp 2147549184 Sep 2 17:01 job basically would want to know 4294967295 and 2147549184 (6 Replies)
Discussion started by: infernalhell
6 Replies

2. Shell Programming and Scripting

I have two commands “ls -h” and “ls -ltr”. How do i make sure “ls -ltr” is run after “ls -h” is suc

help me (2 Replies)
Discussion started by: sonu pandey
2 Replies

3. Programming

Ls -ltr Sort multiple columns

Hi All, I have one requirement, where I need to have output of ls -l command sorted on 1) first on filename 2) last modified time ( descending ) - latest change first. I am not able to figure out how to do it.. Also I dont have a way to change Date display for ls -ltr command.. I am... (1 Reply)
Discussion started by: freakabhi
1 Replies

4. UNIX for Dummies Questions & Answers

Not able to run ls -ltr

Hi , Whenever i try to execute ls with l as an option the system hangs. The workload on server is not more than 1. What could be the reason . Normal ls works fine but ls -l causes the system to hang. This is happening when i m in /usr/local/sbin directory. I can see that the... (4 Replies)
Discussion started by: pinga123
4 Replies

5. Solaris

using ls -ltr : display only last four file names

Hi , Server details – Machine hardware: sun4u OS version: 5.9 Processor type: sparc Hardware: SUNW,Sun-Fire-880 When I put ls –ltr command I get – /users/testuser> ls -ltr -rw-rw-r-- 1 testuser dba 76 Jan 13 2009 ftp.scr -rwxr-xr-x 1... (6 Replies)
Discussion started by: rahulbahulekar
6 Replies

6. Shell Programming and Scripting

How to parse the listing (ls -ltr)

Hi, I need to parse the listing (ls -ltr) in a given directory and get a particular value to see the success or failure. e.g drwxr-xr-x 5 sensr vpnuser 512 Nov 7 07:46 disc_001811. Here i need to take the value 5 which is after drwxr-xr-x . how to so the same in shell... (1 Reply)
Discussion started by: MuthuAlagappan
1 Replies

7. Shell Programming and Scripting

ls -ltr command On Remote server

I am writing a script where in i have to log into a remote machine and check for necessary file by typing (ls -ltr *200505) (this gets all 05month of 2008 yr files) and if files are found get them to the local machine. If not found print a message saying no files on local machine. When i was... (2 Replies)
Discussion started by: vasuarjula
2 Replies

8. Shell Programming and Scripting

ls -ltr help

i have to list some file with certain exemption suppose for example i have two files file1.log.1 file1.log.1.123 i want ls -ltr command to list only 1st type of files so i want like this ls -ltr *.log.*---------it should grep 1st kind of files but this command greps all the files... (4 Replies)
Discussion started by: ali560045
4 Replies

9. UNIX for Advanced & Expert Users

ls -ltr, TIME DETAILS ??

when I write the command ls -ltr it shows the detail of the files. Like -rwxrwxrwx 1 mqsiadm mqbrkrs 0 Aug 29 14:03 dspmq_temp -rwxrwxrwx 1 mqsiadm mqbrkrs 666 Aug 30 06:12 dspmq_script In this,DATE TIME is shown, Now the question is : how can I see the all the times... (5 Replies)
Discussion started by: varungupta
5 Replies

10. UNIX for Dummies Questions & Answers

what does the ls -ltr command list

Hi, THe following is the output when i run the command ls -ltr can anyone explain the meaning of the field in red -rw-r----- 3 orca orca 20924 Sep 08 19:21 BTL027SASI.gnt -rw-r----- 3 orca orca 20924 Sep 08 19:21 BTL027RITD.gnt -rw-r----- 3 orca orca ... (2 Replies)
Discussion started by: ranjita.c
2 Replies
Login or Register to Ask a Question