?
Solved

Unix BASH Scripting - Removing unreferenced files

Posted on 2011-02-14
11
Medium Priority
?
729 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1332 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 668 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
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 1332 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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