Count matching filenames in a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count matching filenames in a folder
# 1  
Old 07-02-2009
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  
Old 07-02-2009
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  
Old 07-02-2009
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

Smilie.

please advise
# 4  
Old 07-02-2009
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 Scott; 07-02-2009 at 08:10 PM..
# 5  
Old 07-03-2009
Thank you very much!. Really appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move matching file to folder in same directory

The below bash executes but nothing is copied. I am trying to cp or mv the matching .pdf to the corresponding folder. There will always be a matching .pdf found but the number of folders may vary. The portion in red is what is used to match the .pdf--- variable $pdf to the folder--- variable... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

How to copy files with the same filenames as those in another folder to that same folder?

Hello All A similar question like this was asked before but I need to change part of the question. I've two folders, Folder A contains some image files in 150 subfolders; Folder B contains text files in 350 subfolders. All image files in Folder A have the same filename as the text... (5 Replies)
Discussion started by: chlade
5 Replies

3. Shell Programming and Scripting

Matching filenames and modifying them internally

hi i am trying to match filenames and modify the files internally depending on the match the filenames are something like this cm01FEB2012bhav cm01AUG2012bhav ... Internally the file(cm01FEB2012bhav) looks like this 20MICRONS,EQ,64.1,65.7,62.45,63.7,64.5,64.1,113043,01-FEB-2012... (4 Replies)
Discussion started by: adarsh1993
4 Replies

4. Shell Programming and Scripting

Renaming files & folder according to the similarities in filenames

hello does someone want to help me for this one ? i want to rename files & a folder according to the similarities in filenames for example : the file with the good name cglogo tougl1953 dgmel bogd 01 -- ttgductoog ggdté gollogtd.ext1the others files needed to be renamed cglogo... (5 Replies)
Discussion started by: mc2z674gj
5 Replies

5. UNIX Desktop Questions & Answers

List of filenames in folder of share drive to unix

I am trying to get the names of files present in the particular folder to a text file and store that file to a unix location. For that I am using the ftp command . now all the code till the accessing te share drive is working (cd share drive location), but after that I am using a command- cd... (1 Reply)
Discussion started by: romeo22
1 Replies

6. Shell Programming and Scripting

HELP! Need to compare 2 folders on 2 different systems, and copy unmatched filenames to other folder

This has been tearing my hair out. I need to: 1: compare server1:/data/archive/ to server2:/data/archive/ (through rsync, ssh, etc) 2: filenames that don't match, get copied (scp) to server2:/data/ server1 and server2 have ssh, scp, rsync access between eachother. Is there any option in... (3 Replies)
Discussion started by: damang111
3 Replies

7. Shell Programming and Scripting

Print and Append the matching filenames

Hi, I want to check all files in a folder for some specific start string and append all matching filenames with _1, _2 .... for each file found in the directory. But, $file below gives me all details of the files like access permissions, owner and filename etc. I just want all the filenames... (3 Replies)
Discussion started by: chetancrsp18
3 Replies

8. Shell Programming and Scripting

Get the file count in a folder

What is the best way to get the file count (Including the subdirectories) under a folder? (12 Replies)
Discussion started by: un1xl0ver_rwx
12 Replies

9. Shell Programming and Scripting

copy all files with the same filenames as those in another folder

Hi, all: I've got two folders, folder A contains some image files (say, 100 files) in .jpg format; folder B contains all description files (say, 500 files) in .txt format. All image files in folder A are able to find their corresponding description files in folder B. That is to say,... (3 Replies)
Discussion started by: jiapei100
3 Replies

10. UNIX for Dummies Questions & Answers

removing the extension from all filenames in a folder

Hi there, I'm pretty new to UNIX and have tried trawling through this forum to find an answer to what I want to try to do, which I'm sure is very simple but I don't know how to do it. What I have a a folder that contains multiple files that I have copied from Windows and I want to remove the... (5 Replies)
Discussion started by: johnmcclintock
5 Replies
Login or Register to Ask a Question