|

Command Types In Linux

Command Types In Linux

There are a few different Command Types In Linux. In fact, there are four command types in Linux. So just what are these commands? First up, there are executable programs or compiled binaries. For example if you run ls /usr/bin, you will be greeted with a large collection of programs. These are most often C or C++ programs which have been compiled, or small scripts written in bash, Ruby, Python, or Perl. A second type of command is that which is a built in. Built in commands are part of the shell itself. For example, the convenient pwd command is actually a shell builtin. Some other built ins are break, cd, continue, eval, exec, exit, export, getopts, and more. The third type is that of a shell function. These are small scripts that are integrated into the environment and are useful for scripting repetitive administration tasks. Last up we have the alias, which is a super way to define macros or shortcuts so to speak of existing commands to reach a desired result. Let’s take a closer look at command types in linux now.

Command Types In Linux


Check The Command Type From The Shell

So now that we have a basic overview of the four different Command Types In Linux, how can we know what we are dealing with? There are many sources of documentation online about this such as the bash command reference. We can also learn about these commands right in our own server by using the type command. Lets see a few now.

vagrant@homestead:~$ type type
type is a shell builtin
vagrant@homestead:~$ type which
which is /usr/bin/which
vagrant@homestead:~$ type man
man is /usr/bin/man
vagrant@homestead:~$ type apropos
apropos is /usr/bin/apropos
vagrant@homestead:~$ type info
info is /usr/bin/info
vagrant@homestead:~$ type whatis
whatis is /usr/bin/whatis
vagrant@homestead:~$ type alias
alias is a shell builtin
vagrant@homestead:~$

Pretty Cool. We can see here that the type command gives us a way to figure out what type of command we are dealing with. We can see that in the case of executable commands, we are provided with the location of the binary. Another way to find this same information for executable programs is to use the which command.


Getting Help With Linux Commands

There are a few ways to get help with commands in Linux. We have the help, info, and man commands to help get information about other commands. Some of them work differently than others, and they will return different information based on the type of command you apply them to. The best way to get a handle on this is to simply test it out.


Here we get help with the type command

vagrant@homestead:~$ help type
type: type [-afptP] name [name …]
Display information about command type.

For each NAME, indicate how it would be interpreted if used as a
command name.

Options:
-a display all locations containing an executable named NAME;
includes aliases, builtins, and functions, if and only if
the -p option is not also used
-f suppress shell function lookup
-P force a PATH search for each NAME, even if it is an alias,
builtin, or function, and returns the name of the disk file
that would be executed
-p returns either the name of the disk file that would be executed,
or nothing if type -t NAME would not return file.
-t output a single word which is one of alias, keyword,
function, builtin, file or , if NAME is an alias, shell
reserved word, shell function, shell builtin, disk file, or not
found, respectively

Arguments:
NAME Command name to be interpreted.

Exit Status:
Returns success if all of the NAMEs are found; fails if any are not found.
vagrant@homestead:~$


In this example we use info to learn about the which command

vagrant@homestead:~$ info which
File: *manpages*, Node: which, Up: (dir)

WHICH(1) General Commands Manual WHICH(1)

NAME
which – locate a command

SYNOPSIS
which [-a] filename …

DESCRIPTION
which returns the pathnames of the files (or links) which would be exe‐
cuted in the current environment, had its arguments been given as com‐
mands in a strictly POSIX-conformant shell. It does this by searching
the PATH for executable files matching the names of the arguments. It
does not follow symbolic links.

OPTIONS
-a print all matching pathnames of each argument

EXIT STATUS
0 if all specified commands are found and executable

1 if one or more specified commands is nonexistent or not exe‐
cutable

2 if an invalid option is specified

Debian 1 May 2009 WHICH(1)


We can also use man to get help like so

vagrant@homestead:~$ man pwd
PWD(1) User Commands PWD(1)

NAME
pwd – print name of current/working directory

SYNOPSIS
pwd [OPTION]…

DESCRIPTION
Print the full filename of the current working directory.

-L, –logical
use PWD from environment, even if it contains symlinks

-P, –physical
avoid all symlinks

–help display this help and exit

–version
output version information and exit

NOTE: your shell may have its own version of pwd, which usually supersedes the ver‐
sion described here. Please refer to your shell’s documentation for details about
the options it supports.

AUTHOR
Written by Jim Meyering.

REPORTING BUGS
Report pwd bugs to bug-coreutils@gnu.org
GNU coreutils home page:
General help using GNU software:
Report pwd translation bugs to

COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or
later .
This is free software: you are free to change and redistribute it. There is NO WAR‐
RANTY, to the extent permitted by law.

SEE ALSO
getcwd(3)

The full documentation for pwd is maintained as a Texinfo manual. If the info and
pwd programs are properly installed at your site, the command

info coreutils ‘pwd invocation’

should give you access to the complete manual.

GNU coreutils 8.21 March 2014 PWD(1)
Manual page pwd(1) line 1/56 (END) (press h for help or q to quit)

Note: When testing out the man command, you’ll often find that the information provided can be quite voluminous. This is why man makes use of the less utility in order to be able to view sections of the manual output in chunks which is much more user friendly. Something else to be aware of with manual pages, is that they are broken down into multiple sections. Eight is the number to be exact, and they are organized as follows.

1

user commands

2

programming interfaces used in system calls with the kernal

3

interfaces for use with the C Language

4

deals with drivers and device nodes

5

file formats

6

reserved for games

7

random use case

8

reserved for system administration

If you want to search a specific section, you can do so in the format of man number command, however most times leaving off the section number is quickest and easiest.


Finding Appropriate Commands and Brief Descriptions

The extensive manual information provided can at times be daunting. We can narrow this down by using something like apropos and whatis. Suppose we have questions about working with disk related issues on the system. We can find a list of commands that may be appropriate, and then get more detailed information as we see fit. Let’s see how.

vagrant@homestead:~$ apropos disk
arm_sync_file_range (2) – sync a file segment with disk
cfdisk (8) – display or manipulate disk partition table
cgdisk (8) – Curses-based GUID partition table (GPT) manipulator
cryptdisks_start (8) – wrapper around cryptsetup that parses /etc/crypttab.
cryptdisks_stop (8) – wrapper around cryptsetup that parses /etc/crypttab.
df (1) – report file system disk space usage
eatmydata (1) – transparently disable fsync() and other data-to-disk synchronization …
fdformat (8) – low-level format a floppy disk
fdisk (8) – manipulate disk partition table
gdisk (8) – Interactive GUID partition table (GPT) manipulator
git-count-objects (1) – Count unpacked number of objects and their disk consumption
git-credential-store (1) – Helper to store credentials on disk
grub-mkstandalone (1) – make a memdisk-based GRUB image
grub-render-label (1) – generate a .disk_label for Apple Macs.
hd (4) – MFM/IDE hard disk devices
initrd (4) – boot loader initialized RAM disk
partx (8) – tell the Linux kernel about the presence and numbering of on-disk par…
quotactl (2) – manipulate disk quotas
ram (4) – ram disk device
sd (4) – driver for SCSI disk drives
sfdisk (8) – partition table manipulator for Linux
sgdisk (8) – Command-line GUID partition table (GPT) manipulator for Linux and Unix
sync (2) – commit buffer cache to disk
sync (8) – synchronize data on disk with memory
sync_file_range (2) – sync a file segment with disk
sync_file_range2 (2) – sync a file segment with disk
syncfs (2) – commit buffer cache to disk
vagrant@homestead:~$ whatis ram
ram (4) – ram disk device
vagrant@homestead:~$ whatis hd
hd (1) – ASCII, decimal, hexadecimal, octal dump
hd (4) – MFM/IDE hard disk devices
vagrant@homestead:~$ whatis df
df (1) – report file system disk space usage

Using alias To Create Custom Commands

Linux has the very handy alias command which is a convenient way to string together a sequence of commands or options as one command. Think of it like a macro. With clever use of aliases you can speed up your daily workflow. Let’s try to create a few of our own now.

vagrant@homestead:/etc$ alias hi='w; uname -a; pwd'
vagrant@homestead:/etc$ hi
14:45:30 up 4 min, 1 user, load average: 0.01, 0.07, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
vagrant pts/0 10.0.2.2 14:41 2.00s 0.08s 0.00s w
Linux homestead 3.13.0-30-generic #55-Ubuntu SMP Fri Jul 4 21:40:53 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
/etc
vagrant@homestead:/etc$

Awesome! We were able to string together a few commands, and then simply type the one alias name to produce results. The only limit with this is your imagination. Try to think of all the useful combinations you might be able to come up with.

List all aliases in Linux

The concept of aliases brings up a good question. What are the list of aliases that already exist on your system? This will be different depending on the Linux distribution, but there is an easy way to see what you already using compgen -a. Once you have your list of aliases, you can use the type command to see exactly how they are constructed.

vagrant@homestead:/etc$ compgen -a
..

alert
c
egrep
fgrep
grep
h
hi
l
la
ll
ls
vagrant@homestead:/etc$ type ..
.. is aliased to ‘cd ..’
vagrant@homestead:/etc$ type ...
… is aliased to ‘cd ../..’
vagrant@homestead:/etc$ type alert
alert is aliased to ‘notify-send –urgency=low -i “$([ $? = 0 ] && echo terminal || echo error)” “$(history|tail -n1|sed -e ‘s/^\s
*[0-9]\+\s*//;s/[;&|]\s*alert$//’)”‘
vagrant@homestead:/etc$ type c
c is aliased to ‘clear’
vagrant@homestead:/etc$ type egrep
egrep is aliased to ‘egrep –color=auto’
vagrant@homestead:/etc$ type fgrep
fgrep is aliased to ‘fgrep –color=auto’
vagrant@homestead:/etc$ type h
h is aliased to ‘cd ~’
vagrant@homestead:/etc$ type l
l is aliased to ‘ls -CF’
vagrant@homestead:/etc$ type la
la is aliased to ‘ls -A’
vagrant@homestead:/etc$ type ll
ll is aliased to ‘ls -alF’
vagrant@homestead:/etc$ type ls
ls is aliased to ‘ls –color=auto’

Command Types In Linux Summary

In this episode we had a nice overview of the different Command Types In Linux. We saw how to check the command types from the shell using the type command, which is certainly very useful. We also now have a good over view of how to get help in various ways for the different command types in Linux using tools like info, help, and man. We also found a way to find good commands for various use cases using the apropos command, and finally we took a quick look at setting up your very own commands in Linux using the alias utility.