LinuxQuestions.org
Visit the LQ Articles and Editorials section
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-15-2010, 01:49 PM   #1
usernameinuse
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Rep: Reputation: 0
Open file without specifying exact location


Say I have a file in some obscure directory that I want to open and edit. I don't want to do something like this...

Code:
vim ~/foo/bar/blah/doh/ugh.txt
I'd rather be able to say find this file and open it. I know there are commands like locate and find to find a file or directory, but I'm not sure whether these can (or even should) be utilized in what I'm trying to do. Basically, what is the simplest way to open a file with a program w/o specifying its exact location? (In cases where there isn't another file with the same name in the entire system, and cases where there are multiple).
 
Old 06-15-2010, 01:53 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
if the file is unique locate should work just fine.

Code:
vim $(locate unique_name)
Beware: vim will typically create a backupfile "unique_name~" which
your locate will also find. Make sure you specify a proper filter
for the locate search, e.g.,
Code:
vim $(locate -r 'unique_name$')

Cheers,
Tink

Last edited by Tinkster; 06-15-2010 at 01:56 PM. Reason: added e.g. part
 
Old 06-15-2010, 01:56 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
There isn't a good way. What if there are multiple files with that name? That being said if you don't mind only getting the first one you could do something like...
Code:
'vim $(locate filename.txt | head -1)
alternately if its a file you frequently edit you could make an alias for editing it
Code:
alias vi-ugh='vim ~/foo/bar/blah/doh/ugh.txt'
then 'vi-ugh' will edit that specific file
 
Old 06-15-2010, 02:20 PM   #4
usernameinuse
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks.
 
Old 06-15-2010, 11:49 PM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Please note the locate requires updatedb to keep track of files. locate will not find files created till updatedb has been run.

updatedb is often run from a cron at midnight or so
 
Old 06-16-2010, 04:16 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Also, if you are worried about possible multiple return, you can issue the '-o' option and it will open all found.
Of course if the number is large it may look a little silly as you may only have one or so lines of each file.

Please mark as SOLVED once you have a solution
 
Old 06-16-2010, 02:32 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
I suppose you could do something like make an alias to a for loop... for a single return it would edit first instance, then next, etc... but that would suck if you were a bit ambiguous.
 
  


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
deleting file by exact size drpaudel Linux - Newbie 1 04-02-2008 03:56 AM
Upgrade 6->8: Could not open location 'file:///home/user' brianmcgee Fedora 4 11-20-2007 02:08 PM
find the exact location of a running exec file cahdecah General 4 08-16-2007 10:53 AM
Exact format of quota-file Marc Thoelen Linux - Server 0 07-23-2007 04:42 AM
Get location bar in "Open File" dialog box ZeroFill Linux - Newbie 1 06-30-2006 06:10 PM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration