The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
get latest file inquirer Shell Programming and Scripting 4 04-02-2009 11:15 AM
ftp command to get latest file laiko UNIX for Dummies Questions & Answers 2 05-14-2008 06:10 AM
Read the latest file wereyou UNIX for Dummies Questions & Answers 3 02-10-2008 12:12 AM
Getting latest file from ftp arunavlp UNIX for Dummies Questions & Answers 3 02-08-2008 10:19 AM
get latest file via ftp command alx Post Here to Contact Site Administrators and Moderators 2 01-26-2006 11:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
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
How do I get the name of latest file?

1) How do I get the name of latest file in a variable?

2) Is it safe to delete all files from a dir

I am doing

cd $dir_name

if return_code > 0
rm *
fi

what are other alternates to delete all files from a dir in a shell script?

  #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

answering #1
Code:
ls -ltr | tail -1
will give you all info on the most recent file in the current folder
Code:
ls -ltr | tail -1 | awk '{print $9}'
will give you just the filename
Code:
MYFILE=`ls -ltr | tail -1 | awk '{print $9}'`
will put that filename into $MYFILE

for #2
normally not best to cd then rm, better to
Code:
rm \dir\folder\*
or similar.
Seen too many examples where the cd failed, and then all files were deleted or some other command executed.
  #3 (permalink)  
Old 01-02-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,788
Yes, deleting all files from a directory is safe... until you inadvertently hit a system directory and your config/essential programs go bye-bye.
To get the newest file, issue a VAR=`ls -tr1|head -n 1`
  #4 (permalink)  
Old 01-02-2009
Hangman2 Hangman2 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 27
Thanks - It worked.

Thanks

  #5 (permalink)  
Old 01-03-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,930
Quote:
will give you all info on the most recent file in the current folder
Code:

ls -ltr | tail -1 | awk '{print $9}'
tail not needed

just this would do

Code:
ls -lrt | awk 'END{print $9 }'
  #6 (permalink)  
Old 01-03-2009
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
Code:
To print the last field of last line
$ ls -lrt | awk '{ f=$NF }; END{ print f }'

This also works sometime
$ ls -t1 | head -n1
Sponsored Links
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 07:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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