Internal and External Commands

Internal Commands:

As part of my OS Security Course, we had an assignment about differentiating Internal  and External commands in Linux. Internal commands are those which are built inside the shell, i.e they are already loaded into the system. It is directly executed in the system. They don’t require a separate process to execute them. They are PATH independent and they are not coded in files. PATH is represented as “$PATH”,  and it is an environment variable that tells the shell, which directories to search for executables, which are provided by the user. Just type and check, $PATH and env in your terminal. Oh…. btw env is an 32-bit LSB executable file, where we can see a list of current environmental variables, and their values for the current user. “env” file can be found in /usr/bin directory. We can locate the PATH variable inside the env executable by using grep command and with a pipe.

shankie@ubuntu:~/Desktop$ env | grep "$PATH"

OK coming back to the topic, in Linux we can differentiate the Linux commands, using the type command. If it is an internal command, type command will pop a message, that it is shell built-in. Else it will mention the directory of the command, i.e the directory where the executable is located.

shankie@ubuntu:~/Desktop$ type cd
cd is a shell builtin

External commands are not executed directly in the system. More clearly they are not loaded into the system. For Eg. cat, ifconfig, firefox( if installed) .They will have an individual process. They seek the help of the $PATH variable. Usually, normal linux commands are found in /bin or in /sbin. If the path of a particular command provided by the user is not mentioned in the variable means, it won’t be executed.

shankie@ubuntu:~/Desktop$ type ifconfig
ifconfig is hashed (/sbin/ifconfig)
shankie@ubuntu:~/Desktop$ type cat
cat is hashed (/bin/cat)
shankie@ubuntu:~/Desktop$ type firefox
firefox is /usr/bin/firefox

So this is the assignment, implemented in Python, HOPE IT IS COMPLETE NOW! given a list of terminal commands, which sorts the terminal commands issued by the user, based on internal and external commands and executes the terminal commands. You can also download the below code with a Sample Input from my Repository.


Variable Descriptions:

commands ( Terminal commands issued by the User )
final ( STDOUT of the terminal commands, issued by user )


#Header File
import os

# User Input
commands,final = [],[]
no = input("Enter the number of commands:")
for i in range(no):
    commands.append(raw_input("Enter the commands:"))

for i in range(no):
    dif = 'type '
    temp = commands[i]
    temp = dif+temp
    output = os.popen(temp).readlines()

# Special Case: Directory execution
def SHELL(catch):

#Generalization of Getting HOME DIR
    path = '/home/shankie'
    path = 'env|grep "HOME"'
    path = os.popen(path).readlines()
    path = str(path)
    end  = path.find('\\n')
    path = '/' + path[8:end]

# Normal Commands Other Than CD
if 'cd' not in catch:

# Moving Forward
    if len(catch) == 2:
        print "Your Directory is Changed! Select a option!"
        print "1. List the files and Subdirectory"
        print "2. Print the Path"
        catch = input()
        if catch == 1:
        elif catch == 2:
            print "Wrong Option"

# Moving Backward
        strip = catch[3:]
        print "Your Directory has Changed! Now you are inside the path!"

# Classifying Internal or External Command
while i < no:
    temp = str(final[i])
    index = temp.find('shell')
    if index > 0:
        print commands[i]+ ' is Internal Command'
        print commands[i]+ ' is External Command'
    temp = commands[i]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s