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
Pattern Matching Count Urgent krabu Shell Programming and Scripting 6 07-31-2008 06:23 PM
Awk to count matching IP address new_buddy Shell Programming and Scripting 3 05-28-2008 05:24 AM
removing the extension from all filenames in a folder johnmcclintock UNIX for Dummies Questions & Answers 5 05-21-2008 08:23 AM
command to get count in unzipped folder arunkumar_mca UNIX for Dummies Questions & Answers 4 10-17-2007 02:19 PM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 04:20 AM

Reply
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 07-02-2009
tententen tententen is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 17
Count matching filenames in a folder

Hi all,

I have 4 files for example named

abc01012009.txt
abc02012009.txt
abc03012009.txt
abc04012009.txt

in a folder. I would like to firstly backup the latest file available, in this case, the latest date available, abc04012009.txt to its subfolder named backup, and then rename the file to abc.txt, so that leaves me with

abc01012009.txt
abc02012009.txt
abc03012009.txt
abc.txt

then, it will run the commands I will set, and once the commands are completed, it go through the list of files again, and loop my commands, until no more files named abcDDMMYYYY.txt are available.Can someone please help, thank you so much!.

rgds
  #2 (permalink)  
Old 07-02-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
you mean this?
Code:
while : ; do
ls -1rt abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt > /dev/null
if [ $? -eq 0 ] ;then
lastfile=`ls -1rt abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt|tail -1`
cp $lastfile backup/
mv $lastfile abc.txt
run your set of commands here
else
exit
fi
done
  #3 (permalink)  
Old 07-02-2009
tententen tententen is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 17
yes thanks it worked!.

except when there are no more files left that matches the pattern, I get this error :P

abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt: No such file or directory

.

please advise
  #4 (permalink)  
Old 07-02-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 968
The fist "ls" is used to test if any files matching this criteria are found. The output of ls is "thrown away" (to /dev/null) - the point being to know if there are such files, not to list them. If files are found, the "if [ $? -eq 0 ]" will be true (the ls returned 0 meaning success - files were found). But ls will write to standard error if no files are found. You can get rid of the error by directing standard error to /dev/null too. The simplest way is to direct standard error to standard output (which you threw away to /dev/null), so...

Change:

Code:
ls -1rt abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt > /dev/null
To:

Code:
ls -1rt abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt > /dev/null 2>&1
I hope this was explanatory and not at all boring to read!

Last edited by scottn; 07-02-2009 at 07:10 PM..
  #5 (permalink)  
Old 07-02-2009
tententen tententen is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 17
Thank you very much!. Really appreciate it!
Sponsored Links
Reply

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 10:39 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