test for existence of files with same extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test for existence of files with same extension
# 1  
Old 06-15-2011
test for existence of files with same extension

Hi,

I am checking for existence of files with the same extensions
Code:
#! /usr/bin/ksh

txtfiles = '*.txt'

if [ -f ${dirpath}/${txtfiles ]
then
cp ${dirpath}/${txtfiles} ${dir2path}
fi

I am getting the following error

line 5: [: too many arguments for the if check condition

Last edited by Franklin52; 06-15-2011 at 09:52 AM.. Reason: Please use code tags
# 2  
Old 06-15-2011
Code:
if [ -f ${dirpath}/${txtfiles ]       -->>> }  is missing

try:
Code:
if [ -f ${dirpath}/${txtfiles} ]


Last edited by Franklin52; 06-15-2011 at 09:52 AM.. Reason: Please use code tags, thank you
# 3  
Old 06-15-2011
Code:
if [ -f ${dirpath}/${txtfiles ]
or 
if [ -f ${dirpath}/${txtfiles} ]

Code:
for txtfiles in *.txt
do
   if [[ -f ${dirpath}/${txtfiles} ]] 
   then 
        cp ${dirpath}/${txtfiles} ${dir2path}
   fi
done

# 4  
Old 06-15-2011
Do not leave spaces around equal-to (=) sign while assigning values to variables and always double quote when referencing variables - its a good practice and it has its advantages too.
Code:
#!/usr/bin/ksh
txtfiles="*.txt"

if  [ -f "${dirpath}/${txtfiles}" ]
then
        cp "${dirpath}/${txtfiles}" "${dir2path}"
fi

# 5  
Old 06-15-2011
Code:
#!/bin/bash

txtfiles='*.txt'
dirpath='/home/me'
dir2path='/home/me/test'

echo $txtfiles -- $dirpath -- $dir2path

for f in "$dirpath"/$txtfiles; do

  echo $f

    [ -f "$f" ] && cp "$f" "$dir2path"

  echo "cp $f" "$dir2path"

done

is working fine. i tried in my system. But see, it's #!/bin/bash
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. Shell Programming and Scripting

Check the Files existence

Hi I have a requirement to check whether the files exists, then it will call other steps in shell script. I did ls *.csv|wc -l if then checking the count of the files should be more than 1 then it will call other steps. I am getting the error that too many arguements as there n... (13 Replies)
Discussion started by: cnrj
13 Replies

3. Solaris

How to test the existence of trailer record

SunOS 5.10 Generic_142900-15 sun4v sparc SUNW,T5240 I have a script that needs to test a file for the existence of a trailer record. Is there a command and is a header and trailer differect record type? Thanks in advance (1 Reply)
Discussion started by: Harleyrci
1 Replies

4. Shell Programming and Scripting

Test for existence of files

Hello, Can you please help me to see if log files exist in a directory? I need to scan logs in different directories, so I am using an array to change dynamically. I need help in the if test statement dir=/logs/MSD dir=/logs/UPD countA=1 while (( countA <= ${#dir } )) do cd ${dir}... (1 Reply)
Discussion started by: drbiloukos
1 Replies

5. Shell Programming and Scripting

Test File for Existence with Whitespaces in Path

Hi Everyone! I'm quite new to shell scripting so this might be trivial, though 3 days of struggle and search didn't help to solve the problem: I want to look for files called '*HUN*' in a huge amount of directories most of their names contain whitespaces and print the path of the directory if... (8 Replies)
Discussion started by: sumi76
8 Replies

6. Shell Programming and Scripting

Test for a file existence

deleted (1 Reply)
Discussion started by: ust
1 Replies

7. Programming

C function to test existence of a login

Hi everybody, I need to check in C program whether a given login is known on the system. Is there any system function that could do this ? So far, all I could find is getpwnam(), which answers my problem by parsing the local password database. But won't work if a user is authenticated by... (2 Replies)
Discussion started by: xavier054
2 Replies

8. Shell Programming and Scripting

check for FILES existence

hi, I have a list of filenames and I want to verify if they all exist. I know "if filename" would do the trick but how do I go about a list of files? thanks (3 Replies)
Discussion started by: mpang_
3 Replies

9. Shell Programming and Scripting

how to test for file existence using file size?

ok im doing this in a normal shell. i need to check the file in the script. how should i write the if else statment? if the filesize contains 0kb then it will echo out file contains nothing else if the script contains more than 0kb then it will echo out file exist. any one care to help?... (3 Replies)
Discussion started by: forevercalz
3 Replies

10. UNIX for Dummies Questions & Answers

How to test for files with a particular extension.

Hi!!, Is there any way on Ksh to test the files having a particular Extension? My program looks something like: for i in *$1* do if "$i" != files with ext of .Z then compress -f $i fi done Any suggestions??? :):) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question