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 ] 


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.

  • locate wont have latest results.
    – Braiam
    Apr 22 '14 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 '14 at 5:54

I think you're looking for:

python $(find -name myscript.py)
  • Ah! It was foolish of me to forget the $! I couldn't understand why brackets weren't doing anything. Thank you! Apr 21 '14 at 22:27
  • Using python $(locate file_name.py) is slightly shorter to type. Apr 22 '14 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. Apr 22 '14 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).

  • 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. Apr 22 '14 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 **/. Apr 22 '14 at 13:53
  • This is also really convenient. Apr 22 '14 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.

  • 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 Apr 22 '14 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 '14 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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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