using ls -ltr : display only last four file names


 
Thread Tools Search this Thread
Operating Systems Solaris using ls -ltr : display only last four file names
# 1  
Old 03-05-2010
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 –

Code:
/users/testuser> ls -ltr
-rw-rw-r--   1 testuser  dba           76 Jan 13  2009 ftp.scr
-rwxr-xr-x   1 testuser  dba         1436 Jan 13  2009 menudb_qa
-rw-rw-r--   1 testuser  dba           16 Jan 26  2009 test.dat20090126
-rw-rw-r--   1 testuser  dba          112 Mar 10  2009 exp_vaucc2.log
drwxrwxr-x   3 testuser  dba         1024 Mar 24  2009 data
drwxrwxr-x   2 testuser  dba           80 Aug 18  2009 tmp_dblogs
-rw-rw-r--   1 testuser  dba          189 Sep  9 05:21 test.txt
-rw-rw-r--   1 testuser  dba          149 Sep  9 05:22 t.txt
-rw-rw-r--   1 testuser  dba           77 Dec 31 07:46 afiedt.buf

But want the command to display only last 4 file names –

Expected result –

Code:
/users/testuser> 
drwxrwxr-x   2 testuser  dba           80 Aug 18  2009 tmp_dblogs
-rw-rw-r--   1 testuser  dba          189 Sep  9 05:21 test.txt
-rw-rw-r--   1 testuser  dba          149 Sep  9 05:22 t.txt
-rw-rw-r--   1 testuser  dba           77 Dec 31 07:46 afiedt.buf

Plz help.

Last edited by radoulov; 03-05-2010 at 11:56 AM.. Reason: Use code tags, please!
# 2  
Old 03-05-2010
You could combine tail with the command i.e

Code:
ls -ltr |tail -4


Last edited by radoulov; 03-05-2010 at 11:55 AM.. Reason: Use code tags, please!
# 3  
Old 03-05-2010
Thank you!!
# 4  
Old 03-06-2010
Code:
ls -lrt | tac | head -4 | tac

# 5  
Old 03-06-2010
How about this?

Code:
ls -lrt | sed '6,$!d'

# 6  
Old 03-06-2010
MySQL

hi thillai_selvan your answer is not working for me.

Code:
ls -lrt | sed '6,$!d'

if the number of line is more than 100. how you get the last 4 lines.
# 7  
Old 03-06-2010
Quote:
Originally Posted by ungalnanban
hi thillai_selvan your answer is not working for me.

Code:
ls -lrt | sed '6,$!d'

if the number of line is more than 100. how you get the last 4 lines.

thanks for your feedbacks. According to the user input I have wrote the above command.

If you want to satisfy for all the cases you just use the following command

Code:
ls -lrt | sed -e :a -e '$q;N;5,$D;ba'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Generic script to load file details(ls -ltr) in to a database.

All, I am trying to create a report on the duration of an ETL load from the file arrival to the final dump in to a database for SLA's. Does anyone have any guidance or ideas on how metadata can be extracted; information of a file: like file name, created timestamp, count of records and load... (1 Reply)
Discussion started by: pradeepp
1 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. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. UNIX for Advanced & Expert Users

ls -ltr a list of filenames-with-spaces within a text file

OS: RHEL 5.8 shell: bash 3.2.25 Directory /home/guest/ contains these files: file a file b file c fileD fileE fileF testFile.txt I'm trying to find the syntax to run ls -ltr against this list of files that is contained within a text file, testFile.txt. The file testFile.txt has... (4 Replies)
Discussion started by: uschaafm
4 Replies

5. UNIX for Dummies Questions & Answers

ls -ltr for a future date/time stamp file

Hi When i do ls -ltr <file1> then it shows me the date and time of the file if - for whatever reason file has future date/time stamp then ls -ltr is not showing the time, it just shows only date part ... even if time is ahead by 2 hr than current time. suppose a file was copied from INDIA... (3 Replies)
Discussion started by: reldb
3 Replies

6. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

7. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

8. UNIX and Linux Applications

display only file names

Hi, I want to display only the file names of the ls command with a delimiter of comma. For example:- ls /etc/dir/N* gives the following output /etc/dir/N1 /etc/dir/N2 /etc/dir/N3 I want the output as N1,N2,N3,-->Here i need the final comma also. Also, here /etc/dir is being... (4 Replies)
Discussion started by: dnat
4 Replies

9. UNIX for Dummies Questions & Answers

Extracting only file size of the ls -ltr command.

All, I only need to get the file size when I do an ls -ltr filename.txt Could you give me the exact syntax. Thanks KOP (6 Replies)
Discussion started by: kingofprussia
6 Replies

10. Shell Programming and Scripting

Display file names that are being deleted

The print command lists the path of the file being found. I want the output as the file name along with the date that particular files were created i.e similar to a ls -l command. I am using find to list all the files that satisfy the selection criteria and delete them using rm. Can I display... (2 Replies)
Discussion started by: theguy16
2 Replies
Login or Register to Ask a Question