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.

I am new to linux.

I made a python script that takes two inputs: a directory name and a file name. The file contains a pattern in each line. The output of the script is all the files that match the pattern and are in the given directory or its subdirectories.

Now I want to be able to invoke this python script using a command, like this:

user@machine:~$ search directory_path file_path

How can I do that?

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

Put the script in ~/bin if it is for a single user. If it is for multiple users put it in /usr/local/bin.

If you put it in /usr/local/bin, have it owned by root. (Enter root password where necessary)

sudo cp script /usr/local/bin
sudo chown root:root /usr/local/bin/script
sudo chmod 755 /usr/local/bin/script

Ubuntu automatically puts ~/bin on your user path (you have to log out and back in or start a new log in shell) and /usr/local/bin is already on the default path.

Make sure #! /usr/bin/env python is at the very top of the script and that the script is executable (chmod 755)

share|improve this answer
1  
2 doubts. 1. If I have bash script do I need to do same as for python script 2.if I have saved my file in /usr/local/bin than I have add sudo before command??? –  TLE Mar 14 '13 at 9:33
1  
For a bash script , add #! /bin/bash at the top. If you need to run the script as root, I advise the script be owned by root and you call the script with sudo. –  bodhi.zazen Mar 14 '13 at 13:15
add comment

Make an alias for your command using this syntax:

alias COMMAND_NAME='COMMAND'

This way, you made your own shortcut to your command ( And of course, you can pass parameters to your alias, as it's your command, nothing less, nothing more. Also you can link this alias to run an script, for example, I made an alias to open steam + dota2 instantly like this:

alias steamz='sh ~/Games/steam.sh'

)

But, your shortcut will be removed once you close your Terminal, so to save this alias and use it daily, add this piece to ~/.bashrc, this file contains commands running on each Terminal run, so if you add your alias definition to this file, your alias will be automatically defined on each Terminal run.

Good luck!

share|improve this answer
add comment

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.