LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-01-2010, 12:02 PM   #1
Andre1234567
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Rep: Reputation: 0
Problems using find . lname


I want to delete all links from a directory, which point to an other certain directory, but I can not delete it by a sh skript.

To more simply reproduce my problem please enter the following commands

1) touch orig
2) ln -s orig linked

the following commands shows that the links has been created correctly:

find . -lname orig

output is:
./linked


but why does this command not show the link?

test="-lname '*'";echo `find . ${test}`

>>no outputline

the variable lname is filled correctly as you see in the output of this line:

test="-lname '*'";echo `echo ${test}`

output:

-lname '*'

Any ideas?
 
Old 06-01-2010, 12:45 PM   #2
bdt-rob
LQ Newbie
 
Registered: Jun 2009
Location: Cambridge, England
Distribution: Gentoo, Arch, (xandross), (Slackware)
Posts: 16

Rep: Reputation: 2
You are asking find to locate all links that point to a file explicitly called '*'. I very much doubt you have any files called '*'. Why are you putting '*' in your script?
 
0 members found this post helpful.
Old 06-01-2010, 12:57 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Because * is a wildcard... and for find that is the correct syntax in this case works fine (You must specify it as '*' or \* and not *)-- for example:

Code:
user@core$ find . -lname '*'
./linked
./public_html/index.htm
user@core$ find . -lname *
find: paths must precede expression: file.1
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
user@core$ find . -lname \*
./linked
./public_html/index.htm
user@core$
In this case if you run bash with -x (debug info) you will see why this particular instance isn't working.

You also wouldn't want to echo `command`, just command or set the result to a variable. VAR=`command`.

Also of note `` has been depreciated, supposed to use $() these days $(command).

Last edited by rweaver; 06-01-2010 at 01:01 PM.
 
0 members found this post helpful.
Old 06-01-2010, 12:59 PM   #4
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
Perhaps you want this:

Code:
find <directory> -type l -lname '<file>' -exec rm "{}" \;
For all links in a directory just omit -lname '<file>'.

Regards,

Alunduil
 
0 members found this post helpful.
Old 06-01-2010, 03:50 PM   #5
Andre1234567
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Okay...maybe let me describe my problem somehow else

I want to delete links using the following sh skript:

----------------------------------------------
mysearch="find . -lname '*'"
for i in `${mysearch}`; do
rm $i
done
----------------------------------------------

This skript does not delete the links.

If I replace the rm statement by a debug outoput and run the sh skript this also does not show me an output

----------------------------------------------
mysearch="find . -lname '*'"
for i in `${mysearch}`; do
echo found link $i
done
----------------------------------------------

but if I just enter the following command in the command line it shows me the links.

find . -lname '*'

What have I done wrong in my first script and second script, so the files are not displayed/deleted in the output?

Thanks

Kind Regards
Andre

Last edited by Andre1234567; 06-01-2010 at 03:52 PM.
 
Old 06-01-2010, 04:40 PM   #6
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
I'll rewrite one as an example:

Code:
for i in $(find . -lname '*'); do
  echo found link $i
done
Regards,

Alunduil
 
Old 06-02-2010, 01:10 AM   #7
Andre1234567
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for your help, but how can I set the directory name by a variable?

This one does not work: How must I change it ?

-------------------
Directory="*"
for i in $(find . -lname '$Directory');do
echo found link $i
done

-----------------------

Thanks
Kind Regards
 
Old 06-02-2010, 02:30 AM   #8
bdt-rob
LQ Newbie
 
Registered: Jun 2009
Location: Cambridge, England
Distribution: Gentoo, Arch, (xandross), (Slackware)
Posts: 16

Rep: Reputation: 2
Don't be so trigger-happy with the single quotes!

When the shell expands stuff, it explicitly does not substitute variables within single quotes. Thus your script is explicitly looking for the directory name $Directory, not the value that should be substituted.

Also, your use of the wildcard "*" is highly likely to be broken in nearly all cases. If you quote it too much the shell won't expand it and find will look for asterisk as the link target. If you quote it too little it gets expanded too soon, and find will look for the first file name in the current directory as the link target, but then find some illegal stray arguments.

Code:
Directory="*/SomeTarget/*"
for i in `find . -lname "$Directory"`;do
echo found link $i
done
should work better. You'll note I've reverted to back-ticks - they're perfectly good enough for this and rweaver's objection is an irrelevant red herring.

Last edited by bdt-rob; 06-02-2010 at 02:31 AM.
 
Old 06-02-2010, 07:39 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by Andre1234567 View Post
skript
Our first victim of KDE's naming scheme?

It's "script".
 
Old 06-02-2010, 09:30 AM   #10
Andre1234567
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Okay..many thanks to all.

one last question. Why does the following script fail

------------------------------------------

Directory="-lname *"
for i in `find . "$Directory"`;do
echo $i
done

------------------------------------------

The following error appears:

find: invalid predicate `-lname *'
 
Old 06-02-2010, 10:40 AM   #11
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
The quotes are throwing things off. Take them out as such:
for i in `find . ${Directory}`; do

Regards,

Alunduil
 
Old 06-02-2010, 10:49 AM   #12
Andre1234567
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi ,

I have done this:



Directory="-lname *"
for i in `find . ${Directory}`;do
echo $i
done

But I still get an error:

find: paths must precede expression
Usage: find [path...] [expression]



Kind Regards

Andre

Last edited by Andre1234567; 06-02-2010 at 10:55 AM.
 
Old 06-02-2010, 11:08 AM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Instead of "``", use "$()".

I'm so annoyed that I seem to be the only one here that knows about "&>" and "$()".
 
Old 06-02-2010, 11:18 AM   #14
Andre1234567
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Sorry, but as I used this script I get the same error:

Directory="-lname *"
for i in $(find . ${Directory});do
echo $i
done


output

find: paths must precede expression
Usage: find [path...] [expression]
 
Old 06-02-2010, 04:28 PM   #15
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Try temporarily turning off bash filename expansion.
Code:
Directory="-lname *"
set -f                  # Turn off bash filename expansion
for i in $(find . ${Directory});do
echo $i
done
set +f                  # Turn on bash filename expansion
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
VMWare problems in Ubuntu 8.04 (Cant Find it??) oversword Linux - Software 4 05-02-2008 10:40 AM
problems using find Humbro Linux - Newbie 3 12-07-2007 08:13 AM
Problems to execute the FIND command hpinto Solaris / OpenSolaris 2 03-28-2006 06:56 PM
gcj problems 'cannot find -lz' zeppelin147 Linux - Software 0 11-17-2005 11:13 AM
C program that takes input "FName|LName" and outputs to std out its_godzilla Programming 2 01-18-2005 10:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration