LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 02-01-2012, 12:32 PM   #1
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Rep: Reputation: 12
To find a pathname to any given file


Hello everyone,
I want to find the complete path name of the given file. For example "/home/user/madagascar" is a pathname of madagascar. How to find it using command line. Please help. Regards....
 
Old 02-01-2012, 12:39 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
You can use the "locate" command, "locate madagascar" will list the locations of files & paths containing "madagascar".
 
1 members found this post helpful.
Old 02-01-2012, 12:42 PM   #3
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,513

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
find . -name "*madagascar*"

Or : find /home -name "*madagascar*"

Or use 'locate' as said in the above post.
 
Old 02-01-2012, 12:43 PM   #4
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
I assume you understand that 'madagascar' will only be '/home/user/madagascar' under certain circumstances - e.g. pwd = /home/user

With these reasonable assumptions, a quick web search suggests that the following should work:
Code:
readlink -f partialfilename
eta:
limitations with locate: will not find the file if it has been created since the last run of updatedb; may find lots of other entries; may take significant time
limitations with find: "find . ..." won't do the job - results will be prefixed ./ not the full canonical path; if you supply the full working directory, still it may find other matching entries in subdirectories, and may also take a long time (both unless you include -maxdepth 1)

However, you may want to find multiple files not just the file that bash would look for.

Last edited by SecretCode; 02-01-2012 at 12:53 PM.
 
1 members found this post helpful.
Old 02-01-2012, 01:05 PM   #5
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 498Reputation: 498Reputation: 498Reputation: 498Reputation: 498
Use "find" then the return of files found are listed by the shell including their paths.

Example: --you look for the file named "overlay", issue this:

Quote:
sudo find /usr -type f -iname 'overlay'
Returns:
/usr/share/fluxbox/overlay

You must specify the -type f otherwise folders are included in search results. If you don't want to experience excuses from find you may issue it with 'sudo' as I have done above, you shall be prompted for the password if you are a sudoer.

Hope this helps.

Goodluck.
 
Old 02-01-2012, 01:15 PM   #6
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
Thanks.

locate filename. Served the purpose, It was indeed great to learn run readlink, find along with su. Your help is appreciated.
 
Old 02-01-2012, 01:22 PM   #7
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Since locate uses a database to find the file, it will only find files which where present on the system since the latest update from the database using the command
Code:
updatedb
If you use the old db, you could find files which are deleted or don't find files which are created after the update.

Kind regards
 
1 members found this post helpful.
Old 02-01-2012, 02:20 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Re locate: Try
Code:
$ locate -b madagasca
to restrict the search to base names.

Re SecretCode's comment
Quote:
limitations with locate: will not find the file if it has been created since the last run of updatedb; may find lots of other entries; may take significant time
limitations with find: may find other matching entries in subdirectories, and may also take a long time (both unless you include -depth 1)
  1. I think (but have not verified) that some versions of mlocate will update the the data base when a file is created.
  2. The -b option reduces the number of "false positives," but, you will still find every file or directory containing the string "madagasca."
  3. The search is actually quite fast since it takes place in the (indexed) data base of file entries.
  4. Depending on the settings in /etc/updatedb.conf, you might locate entries with "madagasca" in them that are system file or which belong to other users. If this is a problem, you can run udatedb in your home directory to create a "personal" data base, and tell locate to use that instead of the default system=wide data base.
See man locate and man updatedb for more information
 
Old 02-01-2012, 02:55 PM   #9
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Some versions may. On my system, I did a quick test
Code:
touch longuniquefilename
locate longuniquefilename
and it found nothing. In fact even now, hours later, it's still not in the locate db.

But as ever, there are lots of solutions to the general problem - which is best depends on the OP's specific requirement which wasn't very precise. I interpreted it as "get the canonical full path of a single file specified relative to the pwd".
 
  


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
[SOLVED] fetchmail saves to only a single file, pathname ted_chou12 Linux - Software 1 11-17-2011 11:37 PM
Making find do search in a directory that has whitespace in its pathname xode Linux - Software 2 04-18-2006 10:42 AM
Inode to pathname conversion.. vishalbutte Programming 1 03-13-2006 12:35 AM
pathname in ps command kaplan71 Linux - General 1 10-31-2005 12:57 PM
setting pathname for input file in C++ microsoft/linux Programming 27 05-09-2005 09:36 PM

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

All times are GMT -5. The time now is 08:08 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