Solved

Unix BASH Scripting - Removing unreferenced files

Posted on 2011-02-14
11
724 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
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!

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Utilizing an array to gracefully append to a list of EmailAddresses
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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