ls command help

Okay so I am very confused as to how to do this it says it is an exercise in my book but never talks about it or explains it please help I have tried everything every combination i can think of.

find out which of these filename paths exist using the ls command and save the list to a file in the pwd name 'existentFiles'

/etc/magic /sbin/fsck /boot/grub/menu.lst /usr/share/games/wombat /var/cache/man/index.db /usr/bin/defcon

with parameters that aside from the file name args, only the -1 flag for the ls command should be used.

I have no clue what the command would be I have tried
ls -1 /etc/magic /sbin/fsck /boot/grub/menu.lst /usr/share/games/wombat /var/cache/man/index.db /usr/bin/defcon > existentFiles

and many variations but nothing........

then the 2nd part is to find out from the same list which pats exist and which dont using the same parameters except adding both a list in the pwd named existentFiles and a list in the pwd for nonexistantFiles

Please help :frowning:

hey the option is not -1, its -l (el)...use it...it'll work fine

for the second part, after >existentfiles, type 2>nonexistentfiles it will send the error output to nonexistentfiles

it is actually -1 which is a new line for every file

hmmm...i'll chk it out n let u know...am also a newbie :slight_smile:

Are you allowed to use "cut"?

ls -1 /etc/magic /sbin/fsck /boot/grub/menu.lst /usr/share/games/wombat /var/cache/man/index.db /usr/bin/defcon 2>&1 1>existingFiles | cut -d" " -f1 >nonexistingFiles

If not, try

ls -1 /etc/magic /sbin/fsck /boot/grub/menu.lst /usr/share/games/wombat /var/cache/man/index.db /usr/bin/defcon 2>&1 1>existingFiles 2>nonexistingFiles

The drawback there is that instead of a simple list of files in "nonexistingFiles", you'll have a list like:

/boot/grub/menu.lst not found
/var/cache/man/index.db not found

use grep -v ?

#!/bin/ksh

if [ -f existentFiles ]
then
  rm existentFiles
fi

for a in /etc/magic /sbin/fsck /boot/grub/menu.lst /usr/share/games/wombat /var/cache/man/index.db /usr/bin/defcon

do
  if ( ls $a > /dev/null 2>&1 )
  then
    echo $a >> existentFiles
  fi
done

On my Mandrake box (using /bin/bash as the first line) I get:

$ cat existentFiles
/sbin/fsck

On my OpenBSD box (using /bin/ksh as the first line) I get:

$ cat existentFiles
/etc/magic
/sbin/fsck

Carl

And of course, just use "else" to echo $a to nonexistantFiles

  else
    echo $a >> nonexistantFiles

I'd add at the top after the first if block

if [ -f nonexistantFiles ]
then
  rm nonexistantFiles
fi

Carl