Extract first file only from ls command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract first file only from ls command
# 1  
Old 07-13-2005
Extract first file only from ls command

I need help in extracting first file only from an ls command
and store it in a local variable
I thought using awk we could build something - not very
familiar how to do it

Something like
ls -t $search_string and store the resultant
first file alone into a local variable ( as my result will have
multiple files )

Please help.
Thank you very much.
# 2  
Old 07-13-2005
Try
ls -t $searchstring | head -n 1 | read fileyouwant

or
fileyouwant=$(ls -t $searchstring | head -n 1)

I am using ksh.

If you create a script around it, use the normal #!/bin/ksh first line.

Greetings, William.
# 3  
Old 07-13-2005
with bash, you can also use

var=( $(ls) ) # var is an array witch contain the result of ls
echo ${var[0]}
# 4  
Old 07-14-2005
Bug ls -1t

try this

ls -1t|head -n 1

this should give you the output u want. Smilie
# 5  
Old 07-26-2005
Thanks for all of you who took time and answered.
>>>> fileyouwant=$(ls -t $searchstring | head -n 1) - It worked.

Thank you agin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command to extract empty field in a large UNIX file?

Hi All, I have records in unix file like below. In this file, we have empty fields from 4th Column to 22nd Column. I have some 200000 records in a file. I want to extract records only which have empty fields from 4th field to 22nd filed. This file is comma separated file. what is the unix... (2 Replies)
Discussion started by: rakeshp
2 Replies

2. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

3. Shell Programming and Scripting

Extract tty from ps command

When I try to extract tty from ps command , at time we get output , at times we dont. for eg i executed below quesry continulusly for some time,Actually i feel its because sometime pid allocated has some additional space at begining which causes this issue. ->ps | grep "/-ksh" | tail -1 | cut -f4... (2 Replies)
Discussion started by: lalitpct
2 Replies

4. Shell Programming and Scripting

need extract command

Hi, I am having an file 20110909.tar.gz (path-home/user01/archive/20110909.tar.gz ), contains some log files some csv files I want to extract that files . if i run "ls" command then their should be display all the log files and csv files on the same path. please help me to know the... (4 Replies)
Discussion started by: aish11
4 Replies

5. UNIX for Dummies Questions & Answers

command to extract sub-string out of file names

I have these files in a directory. It may have more class than the sample below: DEPT_CHEM101LEC_D_20110301.DAT DEPT_CHEM101LAB_D_20110301.DAT DEPT_BIO105LEC_D_20110325.DAT DEPT_BIO105LAB_D_20110325.DAT DEPT_CSC308LEC_D_20110327.DAT DEPT_CSC308LAB_D_20110327.DAT Is there way to extract out... (5 Replies)
Discussion started by: lv99
5 Replies

6. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

7. UNIX for Advanced & Expert Users

What is command to extract single file from an archieve?

I just want to extract one sigle file from an .ear archieve instead of extracting whole ear. Can anyone help me on this? (4 Replies)
Discussion started by: harshal_dcx
4 Replies

8. UNIX for Dummies Questions & Answers

tar command help -- extract single file

Hi, I want to view/display the contents of one file in tar file. For example if the tar file is sam.tar.gz and one of the file inside is E1.txt, how do i view the contents of this E1.txt file. Olso if I want to extract the E1.txt file only from sam.tar.gz how can i do that. Thanks in... (7 Replies)
Discussion started by: icefish
7 Replies

9. UNIX for Advanced & Expert Users

how to unzip and extract tar file in single command

In order to save diskspace and avoid of disk full during unzip then extract the tar file is there any tar unzip command would unzip and extract tar at the same time (test123.tar.gz) thank in advance (6 Replies)
Discussion started by: darkrainbow
6 Replies

10. Programming

extract command

hey i want to extract an argument from double qoutes, eg: when the user enters: prompt "mile" i need to extract everything within the double quotes, in my case, mile, and save it to a string. feedback/code would be appreciated thanks mile1982 (1 Reply)
Discussion started by: mile1982
1 Replies
Login or Register to Ask a Question