LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 01: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, 01:39 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

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, 01:42 PM   #3
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2017 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,742

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

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

Or use 'locate' as said in the above post.
 
Old 02-01-2012, 01: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 01:53 PM.
 
1 members found this post helpful.
Old 02-01-2012, 02:05 PM   #5
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,665

Rep: Reputation: 487Reputation: 487Reputation: 487Reputation: 487Reputation: 487
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, 02: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, 02:22 PM   #7
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,528

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, 03:20 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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, 03: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


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

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

All times are GMT -5. The time now is 01:24 AM.

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