Getting file name ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting file name ?
# 1  
Old 01-02-2009
Getting file name ?

Assuming /dir1/dir2 has two file

file1
file2

If I get the file name which includes the dir name using command

file_name=`ls -ltr /dir1/dir2/* | grep '^-' | tail -1 | awk '{print $9}' `

then I get file_name which includes the /dir1/dir2 also.

if I want to get just the filename which should be file1 or file2 then how can I do that?
# 2  
Old 01-02-2009
Hammer & Screwdriver try the following

Code:
file_name=`ls -ltr /dir1/dir2 | grep '^-' | tail -1 | awk '{print $9}' `

to avoid the directory path in your variable
# 3  
Old 01-02-2009

I wouldn't use four external commands just the get a filename. However, once you have it, it's a simple matter of parameter expansion:

Code:
file_name=${file_name##*/}

# 4  
Old 01-02-2009
That worked

This Worked.

file_name=`ls -ltr /dir1/dir2 | grep '^-' | tail -1 | awk '{print $9}' `


>>
I wouldn't use four external commands just the get a filename
>>

Is there an alternate to just get the latest file name from a dir?

I used grep to just get files from a dir or skip the dirs underneath it, tail -1 gives the latest file name. Awk just prints the name.

Thanks.

Smilie
# 5  
Old 01-02-2009
As a simple example of how you could simply it, you could get rid of the grep and tail.
# 6  
Old 01-02-2009
Quote:
Originally Posted by Hangman2
Is there an alternate to just get the latest file name from a dir?

Code:
dir=/dir1/dir2  ## adjust to taste
file_name=$(
 ls -t "$dir" |
 while IFS= read -r file
 do
   [ -f "$dir/$file" ] && { printf "%s\n" "$file"; break; }
 done
)

Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies
Login or Register to Ask a Question