Computing.Net > Forums > Unix > Check for files with different ext

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Check for files with different ext

Reply to Message Icon

Name: hismail
Date: March 14, 2006 at 07:32:48 Pacific
OS: Sco Openserver
CPU/Ram: 2gig/4gig
Product: Sco
Comment:

Hi
i want to know is there a way to create a variable where I have a multile list of files called :
audit.12
audit.13
audit.14
I need to copy these files to another directory but keeping them with a date extension
I tried this ,but no luck:
#!/bin/ksh
DATE=`date +%d%m%y`
CONO=`grep [0-1][0-9]`
cp audit.$CONO /home/singgm/bckaudit
cd bckaudit
cp audit.$CONO audit.$CONO.$DATE


Thanks in advance
Hamim



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 14, 2006 at 09:06:34 Pacific
Reply:

A way. No error checking:


#!/bin/ksh

adir="/home/singgm/bckaudit"

DATE=$(date +%d%m%y)

ls -1 audit.*|while read file
do
cp $file $adir
cp $file $adir/$file.$DATE
done


0

Response Number 2
Name: hismail
Date: March 14, 2006 at 23:01:39 Pacific
Reply:

Hi Nails
I tried your option ,but it gives me erors
:UX:cp: ERROR: Incorrect usage
UX:cp: TO FIX: Usage: cp [-i] [-p] f1 f2
cp [-i] [-p] f1 ... fn d1
cp [-i] [-p] [-r] d1 d2


0

Response Number 3
Name: nails
Date: March 15, 2006 at 09:35:45 Pacific
Reply:

That's a very straight forward script. It works on my Solaris 8 box. I'm not an HP-UX guy

The only problem I could see is if you spaces in your files or directories. Try places quotes around your variables:

cp "$file" "$dir"


0

Response Number 4
Name: hismail
Date: March 16, 2006 at 02:21:09 Pacific
Reply:

Hi nails
ok Thanks
I did however come up with this solution as well:

dir="/home/singgm/bckaudit"
DATE=`date +%d%m%y`
for FILE in $1 `ls audit.*`
do
chmod 444 $FILE
cp $FILE $dir/$FILE.$DATE
done



0

Response Number 5
Name: nails
Date: March 16, 2006 at 11:02:01 Pacific
Reply:

If it works for you fine, but let me point out a potential problem the way you use the for loop:

Your script works because you have only 1 server name per line with no white space. Let's change the algorithm. Suppose you had to read a text file a line at a time which might contain whitespace:

for line in `cat myfile`
do
echo "$line"
done

suppose myfile contains this for data:

col11 col22
col14 col24

If you wanted to echo just two lines, the script fails.

Here's what you want to do

while read line
do
echo "$line"
done < myfile

In conclusion, you are using what's commonly called a Useless Use of Cat, UUOC. For more info, take a look at this link:

http://laku19.adsl.netsonic.fi/%7Eera/unix/award.html

I'm not being mean - just sharing information.


0

Related Posts

See More



Response Number 6
Name: arun.s
Date: March 25, 2006 at 02:35:11 Pacific
Reply:

guys,

how about this.just see the below solution. let me know if it goes fine.

#!/bin/ksh
dir="/home/signgm/bckaudit"
datestamp=`date +%d%m%y`
ls -l audit.*|while read file
do
file=`echo $file|tr -s " " ";"|cut -d ";" -f9`
cp $file $dir
cp $file $dir/$file.$datestamp
done

thanks
Arun S


this unix location and u have placed windows request. please specify on which platform u want solution.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Check for files with different ext

To find files with different ending www.computing.net/answers/unix/to-find-files-with-different-ending/7943.html

Check for new file, exec sql*plus www.computing.net/answers/unix/check-for-new-file-exec-sqlplus/6714.html

Files with equal names www.computing.net/answers/unix/files-with-equal-names/4198.html