The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
file size comparision local file and remote file dba.admin2008 Shell Programming and Scripting 4 11-13-2008 05:57 PM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 05:38 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-02-2009
Hangman2 Hangman2 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 27
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 (permalink)  
Old 01-02-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 01-02-2009
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,379

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 (permalink)  
Old 01-02-2009
Hangman2 Hangman2 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 27
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.

  #5 (permalink)  
Old 01-02-2009
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,246
As a simple example of how you could simply it, you could get rid of the grep and tail.
  #6 (permalink)  
Old 01-02-2009
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,379
Quote:
Originally Posted by Hangman2 View Post
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
)

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0