How to read a file name that was recently edited when listed using ls command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a file name that was recently edited when listed using ls command?
# 1  
Old 01-30-2012
Question How to read a file name that was recently edited when listed using ls command?

I was trying to write a script that will process recently creatd file.
From below, the script should process input_20111230.dat file.

sam:/top/work/data/input: ls -ltr input_*.dat
-rw-rw-rw- 1 work edprod 455668 Dec 24 09:16 input_20111224.dat
-rw-r--r-- 1 work edprod 7344672369 Dec 30 00:45
input_20111230.dat

Please help me as how to extract file name alone(input_20111230.dat) from the above and pass it to the script as input.

Thanks in advance...
# 2  
Old 01-30-2012
If you're talking about the recently modified file, then try this:
Code:
ls -1t input_*.dat | head -1

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 01-31-2012
Thanks balajesuri...

From my analysis i guess, ls -ltr command list files such that recently created file gets listed at the end.
For example, input_20111230.dat ----> file created on 30 Dec

I guess ls -1t input_*.dat | tail -1 will solve my purpose.
Correct me if I am wrong...
# 4  
Old 01-31-2012
ls -1t


highlighted is not l its 1 (numerica one)
This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 01-31-2012
By the way someone help me as how to extract only the file name(input_20111230.dat) from the ls -ltr command output.

sam:/top/work/data/input:ls -ltr input_*.dat | tail -1
-rw-r--r-- 1 work edprod 7344672369 Dec 30 00:45 input_20111230.dat

I need the file name alone to be sent as input to a script. Thanks in advance...
# 6  
Old 01-31-2012
Code:
ls -ltr input_*.dat | tail -1 | awk '{print $NF}'

---------- Post updated at 10:09 AM ---------- Previous update was at 10:08 AM ----------

Code:
ls -ltr input_*.dat | nawk '{a=$NF}END{print a}'

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 01-31-2012
@siteregsam: I don't understand. Why would you want to go for a long listing (ls -l) and then truncate just the file name from it?

Didn't ls -1t input_*.dat | head -1 work?

Code:
$ ls -1t input_*.dat
input_20111230.dat
input_20111224.dat
$
$ ls -1t input_*.dat | head -1
input_20111230.dat

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Stomp has created two threads recently that can't be read

In the past five minutes stomp has created two thread titled "Bug: empty lines get sometimes duplicated in new post". When I try to read either of those threads, Safari gives me a window containing the message shown in the attachment. (11 Replies)
Discussion started by: Don Cragun
11 Replies

2. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

3. Shell Programming and Scripting

Capture listed files from find command

Hi all, I am using command find $InputPath -type f -mtime +30 However, I need to capture the file listed in screen. May I know how to achieve this? Thank you. (12 Replies)
Discussion started by: ashly
12 Replies

4. UNIX and Linux Applications

Notepad++ hang when open file edited in other text editor

Hi, I would like to ask about the notepad++ text editor application, Although there are alternative and more great text editor in linux (gedit, geany, jedit) im still using the notepad++ sometimes cause for some of my own reason one of those is the minimalist text(what i mean is notepad++ has a... (2 Replies)
Discussion started by: jao_madn
2 Replies

5. UNIX for Advanced & Expert Users

Who edited my text file?

I was editing my text file in gvim & I was getting message that its changed & to load again. I work in a workplace environment with everyone having there username.. How can I know that who edited my file? (4 Replies)
Discussion started by: krishanu
4 Replies

6. UNIX for Dummies Questions & Answers

Saving file edited whith sed

HI! I have a file that looks like this: >ANKRD30_2kb AAGTAACCAATGCAGGAAACCGAGAGGAGAGGTTTGGAAGGTGGTTTAGTGAGGTAATCCATCTTTTCT AGTGATAAACTGGCACCCAGTCAATTTATTCATCAGAAGGGAATACATCAGCCTGGCGTGGTGGCTCGC CCCCGACCCTGTCAGCGTCACCAGCAGCGCGGATCCATGGGCCAGAAGCCTCTAGGGCGCCTAAGTCAG Number of residues in the... (9 Replies)
Discussion started by: vanesa1230
9 Replies

7. Solaris

/etc/profile file edited. No command working

I have X4500 and I created a user. I wanted to give him root privileges and for editing the sudoers files I typed visudo sudoers. But it said visudo command not found. After googling I found that we need to set path in etc/profile. I edited that and put the following command ... (3 Replies)
Discussion started by: bharu_sri
3 Replies

8. UNIX for Dummies Questions & Answers

chmod command for recently modified files

hello! is there a way for me to use the chmod command to change permissions for several files all at once -based on the fact that these files were all most recently modified TODAY ? I can't use a wildcard on their filenames because the filenames are varied. But I was hoping I could somehow do... (2 Replies)
Discussion started by: polka_friend
2 Replies

9. UNIX for Dummies Questions & Answers

Edited Path in Profile File

Definitely a unix dummy. I edited the paths in the /etc/profile file - following the instructions on an install package. Now can't use vi, cat pg ls or any other unix commands. Guess I'm in big trouble. Path reads MANPATH=/opt/hpnp/man PATH=/opt/hpnpl/bin LD_LIBRARY_PATH=/opt/hpnp/lib ... (4 Replies)
Discussion started by: thewetch
4 Replies

10. HP-UX

CVSWeb - Directories listed but files not listed

I am using CVSWeb on HPUnix. When i access it, all directories are listed but files are not listed. I am getting the error "NOTE: There are 51 files, but none matches the current tag. " in tomcat sevrer log i am getting the message "rlog warning: Missing revision or branch number after -r"... (0 Replies)
Discussion started by: ganesh
0 Replies
Login or Register to Ask a Question