Matching a file with extension tmp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching a file with extension tmp
# 1  
Old 12-23-2008
Matching a file with extension tmp

Hi all,

I am writing a shell script for processing files in a directory.

I need to read files in the directory and process them and write it to another file.

For example, if the directory contains the following files,
file1,file2,file3

I want to process these files and create file1_tmp,file2_tmp,file3_tmp.

If the "_tmp" files are present already the should be deleted and recreated using the script.

I am using the following script.But I am not able to do it correctly.
#!/usr/ksh
COUNTER=0;
echo "Enter the directory path";
read DIRECTORY;
echo $DIRECTORY;
for FILE in `cd $DIRECTORY; ls -l | grep -v total | awk '{printf (" %-50s \n",$9) }'`;
do
A=`echo "$FILE" |sed -n -e '/_tmp$/p'`
if [ "$A" = "*_tmp" ]
then
COUNTER=`expr $COUNTER + 1`;
array[$COUNTER]=$A;
fi
done;

Can any one help me on this?

Thanks in advance
# 2  
Old 12-23-2008
Quote:
Originally Posted by ananddr
Hi all,

For example, if the directory contains the following files,
file1,file2,file3

I want to process these files and create file1_tmp,file2_tmp,file3_tmp.
Thanks in advance
Hi, first of all, do You only want the file names? Because I don't see much processing other than that going on. Then Your ls|grep|awk is unnecessary, and not portable. And maybe You are only interested in files with names starting with file and ending with a number? Then use that instead. And I think that the use of a wildcard in the if statement isn't really doing what You expect it to do. And You are only trying to "do" something with files called *_tmp, even though You said that that is what they should be renamed to after You have processed the original file? And I see no renaming going on.

I think You better rephrase Your question and give us a more detailed description. Smilie What You want to do with the files before You rename them for example.

As for the start of Your script:

Code:
#!/usr/ksh
COUNTER=0;
echo "Enter the directory path";
read DIRECTORY;
for FILE in $DIRECTORY/file*[0-9];
do
...

/Lakris
# 3  
Old 12-23-2008
try this
#!/bin/sh

COUNTER=0
echo -n "Enter the directory path: ";
read DIRECTORY
echo $DIRECTORY
cd $DIRECTORY

for FILE in `ls | grep -v _tmp$ `
do

echo $COUNTER $FILE

if [ -e ${FILE}_tmp ]
then
COUNTER=`expr $COUNTER + 1`
array[$COUNTER]=${FILE}_tmp
fi
done
# 4  
Old 12-23-2008
try this
#!/bin/sh

COUNTER=0
echo -n "Enter the directory path: ";
read DIRECTORY
echo $DIRECTORY
cd $DIRECTORY

for FILE in `ls | grep -v _tmp$ `
do

echo $COUNTER $FILE

if [ -e ${FILE}_tmp ]
then
COUNTER=`expr $COUNTER + 1`
array[$COUNTER]=${FILE}_tmp
fi
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

2. HP-UX

Core file under /tmp

Hi Guys Recently we have a issue where the core file under /tmp grows and there by /tmp becomes 100% full in our HP unix server. Any ideas or suggestions about how we can resolve this. (3 Replies)
Discussion started by: newtoaixos
3 Replies

3. Solaris

Permission of tmp file

The script written by csh, when it running it make some tmp file, the process need to read the tmp file to complete but tmp unable to open, don't have permission. Anyone know the way to automatic chmod the tmp file when run process. (2 Replies)
Discussion started by: Diabolist9
2 Replies

4. UNIX for Dummies Questions & Answers

copy all files matching the request and change the extension at the same time

Hi everyone When I'm starting my script I'm giving to it two parameters: script.sh ext1 ext2 I need to copy all files in a directory fitting ext1, to the same folder, with the same names, but with the changed extension to ext2. Till now I've just managed to do it for only 1 file, but I... (16 Replies)
Discussion started by: vacuity93
16 Replies

5. AIX

/tmp file system full

Hi, I would like to know if /tmp file system is full, wheather it will affect the peformance of application installed on AIX. if Memory and CPU are not heavily utilized. Regards, Manoj. (1 Reply)
Discussion started by: manoj.solaris
1 Replies

6. Shell Programming and Scripting

Removing tmp file too quickly?

Still trying to get the basics down and I would like a different solution to what I'm currently doing and a better understanding of why it's happening. I've written a simple backup script that tars individual directories and then dumps them to a NFS drive. STDERR is being dumped into a process... (2 Replies)
Discussion started by: mandelbrot333
2 Replies

7. HP-UX

Error: file </tmp/srw25108193> does not exist

Hi Everybody I need help from you guys. I'm getting this message on PC where i'm running Unix User using Reflection in order to get graphically format for Oracle Report Server. What is causing this msg and how to solve it? Regards and thanks in advance. Gege *Error: file... (0 Replies)
Discussion started by: cgege
0 Replies

8. UNIX for Dummies Questions & Answers

howto change parameter in vi to take tmp file

i have a problem running vi. there is no space in /var where it creates tmp file. How can I change this parameter so that it takes from other directory. thanks (4 Replies)
Discussion started by: ajaya
4 Replies

9. Solaris

after init all /tmp file has been removed

I'm new in Solaris server After the system support reboot the Solaris server, all the files in /tmp has been removed, is that normal under Solaris or under different init level will get different result? which init level will do that? (5 Replies)
Discussion started by: yesthomas
5 Replies

10. UNIX for Dummies Questions & Answers

monitoring /tmp and /var/tmp for suspicous activity

Hello, does anyone have a script that can check the contents of the /tmp directory and for example e-mail the directory content if anything other than session files are present? Maybe there are better ways to monitor suspicous /tmp and /var/tmp activity, if so I'm listening :) (1 Reply)
Discussion started by: jamesbond
1 Replies
Login or Register to Ask a Question