1752803 Members
5634 Online
108789 Solutions
New Discussion юеВ

Re: problem with "-"

 
SOLVED
Go to solution
Pando
Regular Advisor

problem with "-"

Dear Gurus,

Am creating a script that would list and check the files. Some files created have the dash (-) as the first character in the file.
Herewith is part of the script:

ls -la *.res |awk '{print $9}' |while read FILE
do
product=$(awk -F "|" '/^Product/ {print $2}' $FILE|awk -F " " '{print $1}')
if [ $product = "\-" ]
then
$Exepath/mv -f $FILE $temp
fi
done
exit

How and what options should i used to list and moved the files correctly?
maximum points to all correct replies.
8 REPLIES 8
Denver Osborn
Honored Contributor

Re: problem with "-"

In your example, try changing your "\-" to "-" or \-


You could also use find instead of ls,

find /path -name "-*.res" |while read FILE ...


Hope this helps,
-denver
Denver Osborn
Honored Contributor

Re: problem with "-"

woah... time for bed :)

I was thinking the filename began w/ a "-" and not the contents of the file.


for FILE in `find /path -name "*.res"`
do
if [ "`head -1 $FILE`" = "-" ];then
mv $FILE $TMP/$FILE
fi
done

and I'm sure there's a million other ways (better than mine) to do it in a one-liner.

hope this helps,
-denver
Hein van den Heuvel
Honored Contributor

Re: problem with "-"


I'm not exactly sure what you want to have happen. If thise reply does not help enough you may want to provide some sample input and describe the desired effect in detail.

Anyway, I suspect that the answer to your question is to follow the mv -f with --
That will stop further option processing:

Example:

$ cat > -x
test
$ ls -1
-x
$ mv -f -x xx
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
$
$ mv -f -- -x xx
$ ls -1
xx

btw... instead of: ls -a *.res |awk '{print $9}'

Why not just use: ls -a1 *.res

hth,
Hein.
Pando
Regular Advisor

Re: problem with "-"

Hi Gurus,

I have a filename named as:

-_fpar__47713-5__4228_1116921571865.res
-_fpar__47714-8__4506_1116856891860.res
-_fpar__LOT ID__-_1116975671850.res

The problem occurs when i tried to move the files above using the script i've made.
Stuart Browne
Honored Contributor

Re: problem with "-"

$Exepath/mv -f -- $FILE $temp

The 'mv' command is seeing the first '-' of the filename as another command option. The '--' says "no further options".
One long-haired git at your service...
Muthukumar_5
Honored Contributor

Re: problem with "-"

- effect can be overried by using ./ or full path name or using -- with system commands.

$Exepath/mv -f ./$FILE $temp

You can make your script as,

cd
for file in `find . -name "-*.res"`
do
$Exepath/mv -f $FILE $temp
fi
done

hth.
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor
Solution

Re: problem with "-"

Fernando,

What shell are you using?
Did you try the -- as I suggested?
You need it in more places than just the mv.

Trying a quick reproducer, I (of course) even needed it on the 'touch' to create test files.

And then there is the space in one of the names to make the touch create tow files.
In ended up using perl to quickly create your files from your list:

$ perl -ne 'chop; open(X,">$_")' < x

(x was a file with your 3 sample filenames pasted into it)

Someone must really not like you to pick such ugly file names! Fix the source ?!

I also needed -- on the ls command, otherwise you get putput like:
ls: illegal option -- _
ls: illegal option -- _
ls: illegal option -- _
ls: illegal option -- 4
ls: illegal option -- 7
ls: illegal option -- 7
ls: illegal option -- 3
:

Seeing the file names you are challenged with does help us help you, but it is still not clear to me which files need to be moved where. And I see no "|" that is used for awk parsing, nor ^Product in the examples.

Given the ugly names with spaces, READ will have problems, so I would suggest to check out perl for the whole job. For example, here is a one-liner to move all the files startign with "-" in the name to a sub directory "tmp".

perl -e ' while (<*.res>) { rename $_, "tmp/$_" if /^-/ }'


It would be trivial to expend that to parse for |, or Product, or remove the "-".

Good luck!

Hein.
Volker Borowski
Honored Contributor

Re: problem with "-"

HI,

the problem is, that "mv" is not able to detect the end of your parameters.
So either use ./ as a prefix to your file, if you can ensure you are in a local directory or check the "mv" manpage, if your "mv"-version supports the "--" option to show the end of Parameters to "mv".

Use it like this

mv -f -- ............

Hope this helps
Volker