LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Open file without specifying exact location (http://www.linuxquestions.org/questions/linux-newbie-8/open-file-without-specifying-exact-location-814320/)

usernameinuse 06-15-2010 01:49 PM

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).

Tinkster 06-15-2010 01:53 PM

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

rweaver 06-15-2010 01:56 PM

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

usernameinuse 06-15-2010 02:20 PM

Thanks.

Wim Sturkenboom 06-15-2010 11:49 PM

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

grail 06-16-2010 04:16 AM

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

rweaver 06-16-2010 02:32 PM

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.


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