-   Linux - Newbie (
-   -   Open file without specifying exact location (

usernameinuse 06-15-2010 02: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...


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 02:53 PM

if the file is unique locate should work just fine.


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

vim $(locate -r 'unique_name$')


rweaver 06-15-2010 02: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...

'vim $(locate filename.txt | head -1)
alternately if its a file you frequently edit you could make an alias for editing it

alias vi-ugh='vim ~/foo/bar/blah/doh/ugh.txt'
then 'vi-ugh' will edit that specific file

usernameinuse 06-15-2010 03:20 PM


Wim Sturkenboom 06-16-2010 12:49 AM

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 05: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 03: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 03:19 AM.