Solved

Unix BASH Scripting - Removing unreferenced files

Posted on 2011-02-14
11
714 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

758 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

19 Experts available now in Live!

Get 1:1 Help Now