Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Sometimes I want to run certain files, but I don't want to navigate all the way to the folder.

If I know the file name and find -name file_name.py only returns one result, can I just use that?

For example, instead of navigating all the way to a specific folder and calling:

python file_name.py

Can I write:

python [ find -name file_name.py ] 

or

python [ locate file_name.py ]

Or some sort of variant for 'search my entire computer for this file name and use it'? Preferably in one line.

share|improve this question
    
locate wont have latest results. –  Braiam Apr 22 at 5:46
    
What text editor are you using to edit these files? I'd suggest using a text editor that allows you to easily search for files under the current directory. For example, with vim and the CtrlP extension you could type Ctrl-p then filename to find the file. The case-insensitive approximate string matching it performs is called fuzzy matching and is a common feature in text editors. –  Philip Apr 22 at 5:54

5 Answers 5

up vote 12 down vote accepted

I think you're looking for:

python $(find -name myscript.py)
share|improve this answer
    
Ah! It was foolish of me to forget the $! I couldn't understand why brackets weren't doing anything. Thank you! –  crclayton Apr 21 at 22:27
    
Using python $(locate file_name.py) is slightly shorter to type. –  crclayton Apr 22 at 17:08
    
Shorter yes, but if I were in a project tree, I would find find . -name myscript.py, so I didn't get a copy installed globally and a dev copy. So there's less variance in how I would use it those two times. –  hbdgaf Apr 22 at 20:34

Add the line shopt -s globstar to the file .bashrc in your home directory. (To just try this out, type this on the command line, it will only take effect in your current shell.)

Then you can use **/ to mean “in the current directory or a subdirectory and so on recursively”, i.e. the same thing as simple find commands.

python **/myscript.py

Caveat: **/ traverses symbolic links to directories, whereas find doesn't. In bash, you can't avoid this. In zsh, **/ is available out of the box and doesn't traverse symbolic links (there's ***/ for the rare cases where you want to traverse symlinks).

share|improve this answer
    
This one is definitely the fastest to type (and probably my favorite too)! On the other hand, if your directory is bloated, it might be very long, since bash's globbing mecanism involves sorting out the files! in that respect, the methods involving find might be more responsive. –  gniourf_gniourf Apr 22 at 12:22
    
@gniourf_gniourf Sorting is fast, it happens in memory, in time Θ(n log(n)) which is close to the Θ(n) for reading. **/ is a bit slower than find because find has an optimization that it doesn't call stat on files in leaf directories. Calling find …| … or find … -exec … \; is more responsive because files can be processed as soon as they're found, but $(find …) has to traverse the whole tree first just like **/. –  Gilles Apr 22 at 13:53
    
This is also really convenient. –  crclayton Apr 22 at 20:46

Just to show that there is always more than one way to do things (even without using Perl):

find -name file_name.py -exec python {} \;

will work as well. The exec option of find executes the following command for every file that it finds. In this command, {} acts as a placeholder for the file, and \; signifies the end of the command.

share|improve this answer
1  
if you add -quit at the end of the find command, this is a good answer: find -name file_name.py -exec python {} \; -quit –  gniourf_gniourf Apr 22 at 12:12
    
@gniourf_gniourf Ah, thanks for that. Yes, you may want the command to exit after processing the first file found. –  Jos Apr 22 at 12:16

You could store the output of find to a variable and do whatever you want to with it as follows:

file=`find -name file_name.py` && python $file

find -name file_name.py will search for files named file_name.py and store the result into file variable and if the command succeeded(results are obtained without errors), python interprets $file.

share|improve this answer

Another one:

xargs -0 python < <(find -name my_script.py -print0 -quit)

Now, as homework assignment, try to determine the pros, cons and differences of all the methods presented here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.