DEV Community

Yujin
Yujin

Posted on • Originally published at jinyuz.dev on

1

Using find and execute a command

I mostly work on django projects and as a dev who likes shortcuts, I wanted to have an alias of pm for python manage.py.

I could’ve just added

alias pm="python manage.py"
Enter fullscreen mode Exit fullscreen mode

inside of my config but I was working on different projects where manage.py was located in different folders. Typical projects have it located under root while some projects have it under src/manage.py.

What I needed was a way to find where the manage.py was located and use that so I can do

$ pm runserver # OR
$ pm shell # OR
$ pm <command>
Enter fullscreen mode Exit fullscreen mode

Here’s my first attempt:

$ find . -type f -name "manage.py" -exec python {} \; -quit
Enter fullscreen mode Exit fullscreen mode

What this does is find under the current directory . a type of file -type f with a name of manage.py -name "manage.py". And then, for the file you found execute a command -exec The {} is the current file name returned by find command.

In our case it’s gonna be:

$ python ./src/manage.py
Enter fullscreen mode Exit fullscreen mode

The -quit is optional in most cases of using the find exec command since it it exits immediately after the first match.

But there’s a problem with that approach. If we just do:

$ alias pm="find . -type f -name "manage.py" -exec python {} \; -quit"
$ pm runserver
Enter fullscreen mode Exit fullscreen mode

We get:

$ find: runserver: unknown primary or operator
Enter fullscreen mode Exit fullscreen mode

In order to fix that, we have to wrap it inside a function so that it could take arguments:

$ alias pm='f(){ find . -type f -name "manage.py" -exec python {} "$@" \; -quit; unset -f f; }; f'
Enter fullscreen mode Exit fullscreen mode

The $@ stands for all other arguments you passed. You can add that inside of your .bashrc or .zshrc.

If you are using fish like me, I have this file inside my ~/.config/fish/functions/pm.fish

function pm --description "Find and execute manage.py"
    command find . -type f -name "manage.py" -exec python {} $argv \; -quit
end
Enter fullscreen mode Exit fullscreen mode

Now it works fine!

$ pm runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (2 silenced).
November 16, 2020 - 06:16:50
Django version 2.2.17, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs