• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 742
  • Last Modified:

Unix BASH Scripting - Removing unreferenced files

I have the following script which has been created below. The idea is to identify any 'war' files within a directory that haven't got a symbolic link pointing to them and remove them.

So first it needs to find the name of the link file, to store it to a temporary location, then finds war files and compares.....

However I cant get the first line to work and am getting the following error:
        find: bad option -ls

Any suggestions anyone?
#/usr/bin/ksh
cd /home/T104AHE

find . -type l -ls |awk '{print $NF}' >/tmp/$$

find. -name *.war -type f | while read file
do
        file=${file##./}
        echo $file | grep -qf /tmp/$$ && rm -f $file
done

rm -f /tmp$$

Open in new window

0
Lico_w
Asked:
Lico_w
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
woolmilkporcCommented:
Hi again,

your "find" implementation does not support "-ls"

Use instead:

find . -type l -exec ls {} \; |awk '{print $NF}' >/tmp/$$

wmp

0
 
Lico_wAuthor Commented:
Thanks that worked. However this seems to take the name of the link, I would like if possible to take the name of the file it is pointing to and store that in the temporary file, how would I do that?
0
 
SuperdaveCommented:
That was probably meant to have the -l option like this:

find . -type l -exec ls -l {} \; |awk '{print $NF}' >/tmp/$$

...although that assumes your filenames don't contain spaces.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
woolmilkporcCommented:
Correct, sorry!
0
 
Lico_wAuthor Commented:
Perfect thanks. I had to update me script a tad also as it was removing those files that WERE linked whereas I wanted to remove those that WEREN'T.

One thing I get the below message when I run the script, is this anything to be worried about?

sh: ./new_tidy.sh::  not found.
0
 
woolmilkporcCommented:
OK,

the "&&" should have been "||", right?

As for the other problem:

Look at the first line of your script:

#/usr/bin/ksh

should be:

#!/usr/bin/ksh


0
 
woolmilkporcCommented:
... and the last line should be

rm -f /tmp/$$

instead of

rm -f /tmp$$
0
 
Lico_wAuthor Commented:
Perfect many thanks!!!!
0
 
TintinCommented:
What OS are you running this on?
0
 
Lico_wAuthor Commented:
It's a HP Unix box, if I do uname -a it shows:

B.11.23 U

I assume that's the version no?
0
 
TintinCommented:
OK, that explains why you have a different version of find.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now