LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2014, 11:50 AM   #1
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
faster way to get all files with links in a directory


hi,

I search all files that have a link into a directory

currently I go through all files in this directory,
check if this file is a link, and if ...
if this is a huge dir (lots of files), this takes very long time.

I wounder if there is a faster/more efficient way to this
 
Old 11-04-2014, 11:54 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What do you mean "have a link"? Are you trying to find all links in a directory, or all files that have a link pointing to them from elsewhere? Symbolic links or hard links?
 
Old 11-04-2014, 02:19 PM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Original Poster
Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
symbolic links
I need the targets of all links in a directory

currently I go through all files in the directory, if the file is a link than I readlink the link and the result goes into a list.
and this is, if there are very many files and some of them are symbolic links, a very expensive operation,
so I wounder if there is a better way, maybe a file system that knows something or something else, a magic trick possible :-)
 
Old 11-04-2014, 02:40 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You can use the -type l flag in find to find symbolic links, then exec to pass it to readlink. It would probably be faster than looping through every file and checking if it's a link.
 
Old 11-04-2014, 11:39 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
untested:

Code:
readlink * 2>/dev/null | sort -u

find . -type l -print0 | xargs -0 readlink | sort -u
Edit: Oh, forget it, readlink(1) doesn't support multiple files...
(file * | grep 'symbolic link to' would work, though)

The following would work, but requires multiple fork+exec, so it would be slow:
Code:
find . -type l -exec readlink {} \;

Last edited by NevemTeve; 11-05-2014 at 02:38 AM.
 
Old 11-05-2014, 01:05 AM   #6
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Original Poster
Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
will check this,
I suspect that find -type l will do the same loop I do, I guess, go through all names, stat each name and check type
I do not run a bash script, I use the C api, but I will measure it there is a difference between my code and find -type l
 
Old 11-05-2014, 02:42 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
'man find' is my friend:
Code:
find . -type l -printf '%l\n' | sort -u
 
2 members found this post helpful.
Old 11-05-2014, 05:56 AM   #8
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Original Poster
Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by NevemTeve View Post
'man find' is my friend:
Code:
find . -type l -printf '%l\n' | sort -u
this is a very interesting line, thanks
 
Old 11-05-2014, 09:47 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
some things are just plain hard work, this is one of them.
 
  


Reply



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
removeing symbolic links - directory barnea10 Linux - Newbie 5 04-18-2011 04:18 PM
[SOLVED] /etc/rc.d Directory soft links Soji Antony Linux - Newbie 3 02-20-2011 01:36 AM
recursive searching files in directory matching given name - taking care of links vikashtulsiyan Linux - Newbie 5 04-17-2010 01:17 AM
Apache directory links to other host javaholic Linux - Server 5 10-09-2008 11:30 AM
script needed: directory -> html with links to all files in dire flamesrock Linux - Software 2 03-22-2005 01:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 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
Open Source Consulting | Domain Registration