Solved

Unix BASH Scripting - Removing unreferenced files

Posted on 2011-02-14
11
717 Views
Last Modified: 2012-06-21
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
Comment
Question by:Lico_w
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 333 total points
ID: 34887117
Hi again,

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

Use instead:

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

wmp

0
 

Author Comment

by:Lico_w
ID: 34887200
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
 
LVL 13

Assisted Solution

by:Superdave
Superdave earned 167 total points
ID: 34887323
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34887382
Correct, sorry!
0
 

Author Comment

by:Lico_w
ID: 34888016
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34888109
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
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 333 total points
ID: 34888145
... and the last line should be

rm -f /tmp/$$

instead of

rm -f /tmp$$
0
 

Author Closing Comment

by:Lico_w
ID: 34888174
Perfect many thanks!!!!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34894396
What OS are you running this on?
0
 

Author Comment

by:Lico_w
ID: 34894782
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
 
LVL 48

Expert Comment

by:Tintin
ID: 34895704
OK, that explains why you have a different version of find.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now