: [: too many arguments in for -f in if

Hi Experts ,

I have following code

if [ -f /a/b/c/d/e/K*-*-*-*.s ]; then
mv path /filename  newdirpath
echo "K*  files moved successfully to newdirpath \n"
else
echo "K*  files DID NOT moved successfully to newdirpath \n"
fi

I am getting

"echo "K*  files DID NOT moved successfully to newdirpath \n"
scriptpath/scriptname.sh : [: too many arguments

Please help.

Thanks,
A

execute the below command and confirm me, that you want to move those files only ?

 
for i in /a/b/c/d/e/K*-*-*-*.s; do echo $i; done

Yes , i first want to confirm if K*-*-*-*.s files exist in /a/b/c/d/e path ...if exists i want to mv them to some other path ....
so I am using -f option to check the existance and then mving in one go using mv command ...
Is there any better option the check existance of file in particular dir ...? as for -f I am getting : [: too many arguments error ...

Thanks,
A

you cannot use -f, if you want to use the * in your pattern.

 
for i in /a/b/c/d/e/K*-*-*-*.s; 
do 
       #Here you can get the files one by one using $i
       mv $i newPath
       [ "$?" -eq "0" ] && echo "$i : File Move -Successful" || echo "$i : File Move -Failed"
done
#!/bin/bash

for i in /o/A/D/E/c/K*-*-*-*.s ;
do
mv i /om/R/a/e
[ "$?" -eq "0" ] && echo "$i : File Move -Successful" || echo "$i : File Move -Failed"
done

tried this script got getting "mv: cannot access i" , am i doing anything wrong ?
Is "$?" standard way of checking the existence of the file in particular dir ?
Thanks,
Ajay

mv i

change it to $i

it is working fine now when file exists ..." File Move -Successful"
but when file does not exist it is giving "mv: cannot access .......pathname"
anyways thanks a lot for your quick replies ...i appreciate your help.

what is mean by file does not exist ?

Is your file name has multiple words ?

eg: filename : abcd efgh.s ( there is space b/w d and e )

file does not exis = when there are no files at the given path
this scenario i just want "File Move -Fail" but currently along with "File Move -Fail" "mv: cannot access" is also getting displayed which I do not want ....
No file names do not have 'space' character ...it has '-' though ...
e.g filenames ...
Kay-Kaay-20110709060648-E-A.D.g.s

ok, try this..

$? is a special variable which tells, the previous command is success or not.

if it holds 0, then it success.

 
ls -l /a/b/c/d/e/K*-*-*-*.s > /dev/null
if [ "$?" -eq "0" ]
then
 for i in /a/b/c/d/e/K*-*-*-*.s; 
 do 
       #Here you can get the files one by one using $i
       mv $i newPath
       [ "$?" -eq "0" ] && echo "$i : File Move -Successful" || echo "$i : File Move -Failed"
 done
else
 echo "File does not exits"
fi
1 Like

itkamaraj Thank you for your help ...the final working code for me for the benefit of other users / readers .......

#!/bin/bash
ls -l path/name/K-*.s > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
for i in path/name/K-*.s ;
do
mv -f $i new/path/name
[ "$?" -eq "0" ] && echo "$i : File Move -Successful" || echo "$i : File Move -Failed"
done
else
 echo "Files does not exits"
fi

My real question here is like "K-.s" I have 6 more patterns e.g K-.gz , K-*.ff and so on at ....at path/name and ajay/x/y/z path .......one way is i can repeat the same above code 6 times to achieve this ....is there any other way which will do this in one shot so that i do not end repeating same code 6 times ......?

use function

unix books : Unix Books

sample function :

implement your code and let us know, if u have any issue

well ...back to the original topic of getting "too many arguments" error
....it was not because of " -f "

The problem was when i was running the script like "sh scriptname" it was running fine but for just "scriptname" it was giving above error.....

well , it was related with the default shell. I had set it as �#!/bin/bash� now I have changed it to �#!/bin/sh� and script is working fine ....

Thanks,
Ajay

---------- Post updated at 06:05 AM ---------- Previous update was at 06:03 AM ----------

itkamaraj ,
we CAN use -f, if we want to use the * in our pattern (at least this is working fine in sh shell...not sure about other shells ).Experts please comment ....
the problem was related to the default shell...

Thanks,
Ajay

The problem was too many arguments, just like it said.
Your wildcard pattern K*-*-*-*.s matched more than one file, or there was a space in the filename. However, the [ -f file ] test expects exactly one file.
To test for existence of multiple files, the most common way is to make a loop:

for i in /path/name/K-*.s ; do
  if [ -f "$i" ] ; then
    echo "file $i exists"
  else
    echo "$i does not exist, or is not a regular file"
  fi
done

Please not the quotes in the test, "$i", which will make it only one argument even for filenames with spaces.

Thanks mirni,
but my code is working fine i have tested with multiple files being in dir ...:b: as soon as i change �#!/bin/sh� to �#!/bin/bash� is starts giving "too many arguments" error any explanation to this ? :mad:

PS : I am working on Sun OS.And the scheduler tool executes the script as "scriptname" at the command prompt...Let me know in case you need any more info.

Thanks,
Ajay

It's normal that line has some limit. If line length is problem - like in this case, then use while with pipe and if input is huge, then use tmpfile.

ls -1 /path/name/K-*.s | while read i
do   
    if [ -f "$i" ] ; then     
        echo "file $i exists"   
    else     
    echo "$i does not exist, or is not a regular file"   
    fi 
done

On my Linux box, it doesnt work on either sh nor bash:

$ ls file*
file1  file2  file3  filelist
$ cat testf.sh
#!/bin/sh
if [ -f file* ] ; then
    echo exist multiple 
fi
 $ ./testf.sh
[: 4: file1: unexpected operator
 $ cat testf.bash
#!/bin/bash
if [ -f file* ] ; then
    echo exist multiple 
fi
 $ ./testf.bash 
./testf.bash: line 2: [: too many arguments

actually, my sh is 'dash' :rolleyes: :

$ ls -l `which sh`
lrwxrwxrwx 1 root root 4 2011-06-09 11:33 /bin/sh -> dash
1 Like

can you please try executing just as "scriptname" at the command prompt without "./"
e.g $testf.sh
BTW, my sh is "sh" only
> which sh
/usr/bin/sh
> uname -v
Generic_118558-27
> uname
SunOS
> uname -a
SunOS ssdb0046t 5.9 Generic_118558-27 sun4u sparc SUNW,Sun-Fire-V890
Thanks,
Ajay

That returns "Command not found", just as expected, since the current dir is not in PATH.

---------- Post updated 07-19-11 at 01:36 AM ---------- Previous update was 07-18-11 at 10:32 PM ----------

I'm sure that the test won't work in either sh nor bash, if you have more (or less) than one argument to test -f. The difference may be in how the error is reported, but nevertheless, supplying test -f with more or less arguments is a syntax error on your side.

test command accept only 1 argument when using option -f.
Look my while loop and try it.

  • read filename
  • test it

If you give next echo, you will see your test problem

echo if [ -f file* ]