faqts : Computers : Operating Systems : Linux

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

63 of 67 people (94%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Operating system: Linux: File: Execute: Run: How to run an executable program in Linux?

May 20th, 2004 13:00
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 27 February 2004 - 03:20 am -------------------

Operating system: Linux: File: Execute: Run: How to run an executable 
program in Linux?

---

To be able to run a file successfully:

---

Steps: Overview:

---

 1. -The file must of course be of the executable type
     in order to run successfully.
     (e.g. a shell script or a C++ executable)

 2. -The file must have execute permission set (use chmod)

 3. -The filename together with its path has to be typed completely,
     that should always work.

     (so only typing the filename without the path will usually not
      work)

        -- and or --

     1. But you can put ./ in front if the file is in the current
        directory (this is a quit common situation)

        -- and or --

     2. you can only type the filename without its path,
        if the path of that filename is already present in the PATH
        environment variable

---
---

Steps: Worked out:

 1. -The file must of course be of the executable type
     in order to run successfully.
     (e.g. a shell script or a C++ executable)
     If not you will get an errors like bad command or syntax errors.

      To check, goto the directory containing
      the file, and type

        file *

      it should then show information indicating it
      is an executable.

 2. -The file must have execute permission set (use chmod)

      Given that the file is indeed of the executable type, you can
      change its permission using the command chmod.

      e.g.

       chmod +x <your filename>

      e.g.

       chmod +x a.out

      to make your a.out file executable

 3. -The filename together with its path has to be typed completely,
     that should always work.

     (so only typing the filename without the path will usually not
      work)

      e.g.

      /home/myusername/a.out

     1. But you can put ./ in front if the file is in the current 
directory
        (this is a quit common situation)

         ./<your filename>

           e.g.

           ./a.out

         (if the file is in the directory above, type ../ in front,
          when 2 directories above type .../ in front, and so on).

     2. you can only type the filename and not the path
        if the path of that filename is present in the PATH variable


        You can type only the filename, given that directory is one of
        the components in your PATH.

        You can see the current content your PATH by typing:

         echo $PATH

        The shell will then by design try to append the components of
        the path in order from left to right, until that executable is
        found, or the path is exhausted.

         e.g. in general typing:

  PATH=$PATH:<your directory containing the file you want to execute>

  <your filename>

              will then execute that file.

         e.g.

          PATH=$PATH:/mydirectory

          a.out

          will execute the file 'a.out' located in 'mydirectory'.

---
---

Troubleshooting:

If you get errors like 'syntax error', make sure you are dealing with
an executable file.

Check with:

        file *

---
---

If you get the error 'permission denied', check if you have given the 
file
execute permission using chmod.

---

You can get the current permission settings by typing:

 ls -l <your filename>

---

you can set the permission settings by typing:

 chmod +x <your filename>

 or also:

 chmod <some 3 digit number> <your filename>

---
---

If you get the error 'command not found', check e.g. if you have typed 
the
filename correctly, possibly including its complete path.

---
---

Note:

You might need to have the correct login permission
to execute a file.

E.g. try to login as root if it does not work, by typing:

 su

 <supply your password>

---
---

Note:

So file extensions like .exe, .com, .bat, .wsh, ... like used
in Microsoft Windows to indicate a file is executable are by
design not used in Linux (or Unix).

---
---

Method: executing a file by putting it in the /bin directory

This is only a special case of that directory being in the path.

As the /bin directory is by default part of your PATH
environment variable.

So putting the file in /bin, and then typing only its
filename on the command line should in general also
work.

 e.g.

  cp <your filename> /bin

  <your filename>

---

 e.g.

  cp a.out /bin

  a.out

---
---

Internet: see also:

---

Linux: File: Permission: Set: How to set your read/write/execute 
permissions? [chmod / run]
http://www.faqts.com/knowledge_base/view.phtml/aid/24443/fid/107

---

C++: Compile: Operating system: Linux: How to compile C or C++ program 
on Linux? [run / hello world]
http://www.faqts.com/knowledge_base/view.phtml/aid/26744/fid/107

---

Operating system: Linux: Environment: Variable: Path: Set: How to set 
the PATH variable in Linux?
http://www.faqts.com/knowledge_base/view.phtml/aid/26752/fid/457

---

Linux: File: Execute: Check: How to check if a program is an 
executable in Linux? [file *]
http://www.faqts.com/knowledge_base/view.phtml/aid/29117/fid/107

---

How do I execute a file?
[Internet: see also: http://www.google.com search for 'how to execute 
a file in Linux': http://groups.google.com/groups?hl=en&lr=&ie=UTF-
8&threadm=br786m%24q2s%241%40string.physics.ubc.ca&rnum=1&prev=/groups%
3Fq%3Dhow%2Bto%2Bexecute%2Ba%2Bfile%2Bin%2BLinux%26hl%3Den%26lr%3D%
26ie%3DUTF-8%26selm%3Dbr786m%2524q2s%25241%2540string.physics.ubc.ca%
26rnum%3D1]

----------------------------------------------------------------------