using ls -lrt instead of ls

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using ls -lrt instead of ls
# 1  
Old 08-14-2009
using ls -lrt instead of ls

I have a requirement in which I shud use ls -lrt instead of ls command because when we use ls command we get an error..the script part where i get error is given below

for SAPRESPONSEFILES in `ls $SAPRESPONSEGOFILE | sed "s/go/dat/g"`

basically the script processes the files of format sd63****.dat and sd63****.go files

SAPRESPONSEGOFILE may have types as sd630000 to sd639999

we have a logic built in script which checks for the sequence number last after processing sd639998 file the files sd639999 ,sd0000 sd 0001 startb to come.Since while lsiting using "ls"
the sd630000 comes first before sd639999 the script fails due to sequence number issue.we tried to use ls -lrt but our team mate says it will have some performance issues.

# 2  
Old 08-14-2009
You probably don't mean:

ls -lrt
(ls hyphen-ell-arr-tee)


ls -1rt

(ls hyphen-one-arr-tee)

There is no difference in performance between "ls" with or without the "-1rt" because both perform a sort.

How many files do you have in the directory? The "for" construct may break with large numbers of files and may be better with "while".

If I'm not mistake the "sed" will cause ".go" and ".dat" filenames to all become ".dat", thereby creating identical pairs. Is this what you intended, or are you trying to confine processing to just ".go" and ".dat" files?
# 3  
Old 08-14-2009
the prob is not with processing .dat files
files will be coming into the input directory continuously of the format sd63****.dat.the prob is after processing sd639998.dat the file sd9999.dat,sd0000.dat,sd0001.dat .... start to come.while the script list the files after processing sd639998.dat, the sd630000.dat comes before sd639999.dat since we user ls command and then comes the error since the sequence sd639999.dat is missed if we use ls -lrt command it will be fine..but our team mate says there were performance issue when they uses ls -lrt command...

please help me out
# 4  
Old 08-14-2009
The -r flag just reverses the sort, it will still have the skip. Since they're being added continuously, I'm assuming they have sequential timestamps, so if you are using GNU ls, try the --sort=time option, which will sort by time instead of filename.

The performance issue is that, if there are thousands of files -- which there probably are -- it crams them all into one shell variable before continuing. This is slow and may even truncate the list if it is too large. Try something like this instead, piping it:
ls --sort=time $SAPRESPONSEGOFILE | sed "s/go/dat/g" |
  echo "File is ${SAPRESPONSEFILES}"

If the performance is too slow you could use shell string manipulation to replace go with dat and leave out sed.

If there are still performance issues with ls, then your directory is too huge, the problem is not ls in particular but rather the rapid statting and sorting of thousands of files in general.
# 5  
Old 08-14-2009
thanks a lot corona...

but why do performance issue occur when we use "ls -lrt" but not when using "ls" as my team mate say.....

Last edited by praviper; 08-14-2009 at 04:03 PM..
# 6  
Old 08-14-2009
[edit] We cross-edited there.

The ls -l performance issue might be because it prints extended information, if your program can't account for that. We haven't seen your program -- we don't know. We thought maybe it could, but if it can't:
$ ls crc32.c
$ ls -l crc32.c
-rw-r--r-- 1 tyler users 1634 Jul 15 10:04 crc32.c

...which would cause your code to theoretically check for files "-rw-r--r--", "1", "tyler", "users", "1634", "Jul", "15", and "10:04" in addition to "crc32.c" Smilie Its a good idea to check the manual page to see what the flags actually mean instead of blindly adding them.

Try just ls -t or ls --sort=time (they are equivalent) and see if that gets you the sorting order you want. If its backwards, ls -tr reverses it.

Last edited by Corona688; 08-14-2009 at 04:09 PM..
# 7  
Old 08-14-2009
hi that is working...i dont have GNU...

so is used ls -lrt with ur logic..its working....
do u mean to say it will give better perfromance ...

thanks a lot again
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What does total no. of files in ls -lrt o/p means?

when we fire ls -lrt command we see o/p as total 16 drwx------ 9 root root 8192 May 8 2002 lost+found drwxr-xr-x 2 root root 512 Jun 14 2002 TT_DB drwxrwxr-x 2 root root 512 Jul 31 2002 mail here total no. of files is always greater than... (4 Replies)
Discussion started by: Jcpratap
4 Replies

2. Shell Programming and Scripting

want to concatenate multiple files based on the rest of ls -lrt

uadm@4132> ls -lrt -rw------- 1 uadm uadm 3811819 Jun 6 04:08 data_log-2010.05.30-10:04:08.txt -rw------- 1 uadm uadm 716246 Jun 13 01:38 data_log-2010.06.06-10:04:08.txt -rw------- 1 uadm uadm 996 Jun 13 04:00 data_log-2010.06.06-10:04:22.txt -rw------- 1 uadm uadm 7471 Jun 20 02:03... (5 Replies)
Discussion started by: mail2sant
5 Replies

3. Shell Programming and Scripting

Problems with ls -lrt

I am doing ls -lrt and it does not respond and have to close the xterm ls works ok (4 Replies)
Discussion started by: kristinu
4 Replies

4. UNIX for Dummies Questions & Answers

" ls -lrt " not working ! --- Need a insight from a expert .

Hi ! All This might be a silly question.. to experts in unix. but defintely its a interesting question and I want to know the root cause. Here is the problem.... I have a Linux OS box. It has diff files systems. When I go to a DIR like this /RootDIR/NFS/myDIR and give ls -lart .. it... (4 Replies)
Discussion started by: dashok.83
4 Replies

5. Shell Programming and Scripting

find command nonrecurslu listing ls -lrt

---------------------------------------------------------------------- I have tried find . type -f -exec ls -lrt {} \; but it listed files recursively ,I need only that dir files not internal dir file. --------------------------------------------------------------------- (8 Replies)
Discussion started by: RahulJoshi
8 Replies

6. HP-UX

sem_open(), -lrt and creating a library

I am on HP-UX delta B.11.11 I am using sem_open() and compiling/linking the library by specifying -lrt. Then, I am creating library like below: ar cr $libdir/liboscfe.a `cat fe.libs.$$ com.libs.$$` ranlib $libdir/liboscfe.a Now, I am compiling another utility sch2db which requires this... (1 Reply)
Discussion started by: apadha
1 Replies
Login or Register to Ask a Question