Problem with deleting

Hi,

I have a folder A which contains files 1,2,3... I also have a file xyz under a diff folder B containg the entries 1,2,3...

I am trying to compare and delete the nonexisting ones from xyz using the below code:

QFILE=/B/xyz
cd A
ls -1 > /tmp/file1
for fname in `cat $QFILE`
do
  if [ -f /A/$fname ]
  then
    echo "Nothing to do\n" 
  else
    sed /$fname/d $QFILE > /tmp/file2
    mv /tmp/file2 $QFILE
  fi
done

but this is emptying the whole xyz file rather than just deleting the mismatching entry. pls suggest a mechanism to achieve this.

for d in dir/* do; [ -d "$d" ] && egrep -v "^`basename $d`$" file.txt > /tmp/file.$$ && mv /tmp/file.$$ file.txt || exit 1; done

js.

bash: syntax error near unexpected token `['

There's no point to cramming a script on to a single line; all it does is make it harder to read and debug.

for d in dir/*
do
 [ -d "$d" ] &&
   egrep -v "^`basename $d`$" file.txt > /tmp/file.$$ &&
     mv /tmp/file.$$ file.txt ||
       exit 1
done

And basename, an external command, is not necessary:

for d in dir/*/
do
  egrep -v "^${d##*/}$" file.txt > /tmp/file.$$ &&
    mv /tmp/file.$$

True. I tend to type one-offs on a single line... Tsdk, tsk. :slight_smile:

Depends on the shell you use...

js.

Even at the prompt, I usually type multi-line commands; they're easier to read.

The standard Unix shell can do it without an external command.