InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Finding and Running Commands in bash
Bash

Finding and Running Commands in bash

InfinityCoder January 11, 2017

You need to find and run a particular command under bash.

Try the type, which, apropos, locate, slocate, find, and ls commands.

bash keeps a list of directories in which it should look for commands in an environment variable called $PATH.

The bash built-in type command searches your environment (including aliases, keywords, functions, built-ins, and files in the $PATH) for executable commands matching its arguments and displays the type and location of any matches.

It has several arguments, notably the -a flag, which causes it to print all matches instead of stopping at the first one.

The which command is similar but only searches your $PATH (and csh aliases). It may vary from system to system (it’s usually a csh shell script on BSD, but a binary on Linux), and usually has a -a flag like type.
Use these commands when you know the name of a command and need to know exactly where it’s located, or to see if it’s on this computer. For example:

1
2
3
4
5
6
7
8
9
10
11
12
$ type which
which is hashed (/usr/bin/which)
 
$ type ls
ls is aliased to `ls -F -h'
 
$ type -a ls
ls is aliased to `ls -F -h'
ls is /bin/ls
 
$ which which
/usr/bin/which

Almost all commands come with some form of help on how to use them. Usually there is online documentation called manpages, where “man” is short for manual.
These are accessed using the man command, so man ls will give you documentation about the ls command. Many programs also have a built-in help facility, accessed by providing a “help me” argument such as -h or –help.

Some programs, especially on other operating systems, will give you help if you don’t give them arguments.

Some Unix commands will also do that, but a great many of them will not. This is due to the way that Unix commands fit together into something called pipelines, which we’ll cover later.

But what if you don’t know or can’t remember the name of the command you need?

apropos searches manpage names and descriptions for regular expressions supplied as arguments. This is incredibly useful when you don’t remember the name of the command you need. This is the same as man -k.

1
2
3
4
5
$ apropos music
cms (4) - Creative Music System device driver
 
$ man -k music
cms (4) - Creative Music System device driver

locate and slocate consult database files about the system (usually compiled and updated by a cron job) to find files or commands almost instantly.

The location of the actual database files, what is indexed therein, and how often it is checked, may vary from system to system.

Consult your system’s manpages for details. slocate stores permission information (in addition to filenames and paths) so that it will not list programs to which the user does not have access.

On most Linux systems, locate is a symbolic link to slocate; other systems may have separate programs, or may not
have slocate at all.

1
2
3
4
5
6
7
$ locate apropos
/usr/bin/apropos
/usr/share/man/de/man1/apropos.1.gz
/usr/share/man/es/man1/apropos.1.gz
/usr/share/man/it/man1/apropos.1.gz
/usr/share/man/ja/man1/apropos.1.gz
/usr/share/man/man1/apropos.1.gz

For much more on the find command, see all of Chapter 9. Last but not least, try using ls also.

Remember if the command you wish to run is in your current directory, you must prefix it with a ./ since the current working directory is usually not in your $PATH for security reasons.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Looping with Floating-Point Values in bash
The for loop with arithmetic expressions only does integer arithmetic. …

Looping with Floating-Point Values in bash

Finding a File Using a List of Possible Locations in bash
You need to execute, source, or read a file, but …

Finding a File Using a List of Possible Locations in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap