Making executable file linux

How to make a file executable in linux?

Aug 30, 2016 · 2 min read

Asked by Satyam

To give a little more context, I think that this question stems from the distinction between executable and non executable files on windows, characterized by the .exe extension.

In linux, every file can be an executable.

Let’s see what happens when you try to execute a file. First, we need to have a file. We can use the echo command and redirect its output to create a new file.

Note the lack of extension i n the filename. We could have named it hello.txt, or hello.sh, it’s entirely upto us, the extension mostly doesn’t matter. Unlike windows, you don’t need a special extension to make a file executable.

Now, we’ll execute the file.

Let’s go over what happened.

To execute a file all we need to do is enter the path of the file on the command prompt. We’ll also need execute permissions on the file, like to read a file we’d need read permissions. Note that setting or removing the execute permissions does not make a file executable or non-executable, but it just takes away or gives us the ability to execute it.

What exactly happens when we ‘execute’ a file is that the contents of the file are sent to the program located on the first line. In this case, it’s /bin/bash.
This means that all contents of the file are being sent to bash. So it’s as if you were typing those commands into the shell. Therefore, in the file we’ve created it ran the echo command.

Please note that there’s nothing special with ./, it just simply means ‘look for the file in the current directory’. It could have been any path, for example

And you could also have some other program handle the execution. For example, consider the following file in which I’ve set the first line to be the location of python binary, which I found via the command whereis python. It may be different on your system.

You could also set a .doc file’s permissions as executable, or a .jpg file. It’s perfectly legal. But when you try to execute those, their contents will be sent to shell to be parsed as shell commands. But they aren’t shell commands, so you’ll get an error.

Источник

How to make a file executable? [duplicate]

How can I make my file so that I can double click on it, and it runs. It is a .sh script, but I also have files that say:

in the description of what they are.

I can’t run any of these from terminal, or by double clicking.

If possible, I would like a way using either the GUI or a Terminal, but not a combination of the two.

Here is a screenshot of what I see when I right click then go on properties. The file first:

And the shell script here:

NB: I accept that this is a duplicate (I was looking for it, and couldn’t find it, so asked + answered it, hoping that I would find it) however, I don’t think the question about .desktop files is a duplicate.

1 Answer 1

There are two ways of making a file executable:

GUI Method:

Go to the permissions tab, then tick the box Execute: [✓] Allow executing file as program.

Command line method:

Note that chmod does also have some more advanced options. It accepts three groups of options, represented as — — — . The first set of — is User. The second is Group and the last is Other (everyone else).

r stands for Read, w for Write and x for eXecute.

To allow everyone to read it, but only Group to execute and User to read and write it would be -rw- rx- r— . This would be added to the command as:

chmod also can do this in numbers. It is based on binary.

So there are these numbers:

Execute by user is 100 . Execute by group is 010 . Execute by other is 001

Write by user is 200 . Write by group is 020 . Write by other is 002 .

Read by user is 400 . Read by group is 040 . Read by other is 004 .

Then you add these together to get the desired combination.

So to allow everyone to read it, but only Group to execute and User to write it would be 400 + 040 + 004 and 010 and 200

That adds up to 600 + 050 + 004 = 654 .

You could then run the command.

to set it. So to set all permissions you can run:

Finally, you can do:

To take all permissions away from everyone.

To add read and write for the user, without affecting any other permissions (e.g. Execute permissions).

This website has a very useful little tool, whereby you can tick the options you want and it gives you the command:

However, not all the possible combinations are sensible to use; the main ones that are used are the following:

755 — Owner has all, and Group and Other can read and execute

700 — Owner has all

Читайте также:  Mastercam для mac os

644 — Owner can read and write, and Group and Other can read

600 — Owner can read and write

And, if you’re using non-trivial user groups:

775 — Owner can read and write, and Group and Other can read

770 — Owner and Group have all, and Other can read and execute

750 — Owner has all, and Group can read and execute

664 — Owner and Group can read and write, and Other can just read

660 — Owner and Group can read and write

640 — Owner can read and write, and Group can read

777 and 666 are rarely used, except in /tmp .

Thanks Ilmari Karonen for pointing out the ones in common usage!

Источник

How to make a file (e.g. a .sh script) executable, so it can be run from a terminal

I have a script.sh file and type of this file is shellscript file. I want to make this file as application/x-executable file. How can I make it?

4 Answers 4

You can mark the file as executable:

You can then execute it like this:

If you want to use a different command to start it, you can add an alias:

Add this at the end of the file:

Open a new terminal session or type source

/.bashrc in your terminal to apply. Then simply use the new name to start the script.

There are two ways of making a file executable:

GUI Method:

Right-click the file and select Properties. Go to the permissions tab, then tick the box Execute: [ ] Allow executing file as program or in Nautilus Program: [ ] Allow this file to run as a program in Thunar.

Terminal / Command method:

You can either use:

chmod +x filename.extension

chmod +x /path/to/your/filename.extension

chmod does also have some more advanced options:

The spaces are to show that it is split up: — rwx — —

The first set of — is User. The second is Group and the last is Other (anyone else)

r stands for Read, w for Write and x for eXecute.

So to allow everyone to read it, but only Group to execute and User to read and write it (but for some reason not execute) would be:

-rw- rx- r— But this would be added to the command as:

chmod +rw-rx-r— /path/to/file.extension

chmod also can do this in numbers. It is based on binary (I think, as it is 1,2 and 4)

So there are these numbers:

Execute by user is 100 . Execute by group is 010 . Execute by other is 001 .

Write by user is 200 . Write by group is 020 . Write by other is 002 .

Read by user is 400 . Read by group is 040 . Read by other is 004 .

Then you add these together to get the desired combination.

So to allow everyone to read it, but only Group to execute and User to write it (but for some reason not execute) would be:

400 + 040 + 004 and 010 and 200

That adds up to 600 + 050 + 004 = 654.

You could then run the command.

chmod +654 /path/to/file.extension to set it.

And to set all permissions you can type:

chmod +rwxrwxrwx /path/to/file.extension

Or (this is a bit easier to write, but harder to remember each one):

chmod +777 /path/to/file.extension

Finally, you can do:

chmod -777 /path/to/file.extension

To take all permissions away from everyone.

chmod +300 /path/to/file.extension

To add read and write for user, without affecting any other permissions (e.g. Execute permissions).

This website has a very useful little grid checkbox thing, whereby you can tick the options you want and it gives you the command:

However, not all the possible combinations are sensible to use; the main ones that are used are the following:

755 — Owner has all, and Group and Other can read and execute

700 — Owner has all

644 — Owner can read and write, and Group and Other can read

600 — Owner can read and write

And, if you’re using non-trivial user groups:

775 — Owner can read and write, and Group and Other can read

770 — Owner and Group have all, and Other can read and execute

750 — Owner has all, and Group can read and execute

664 — Owner and Group can read and write, and Other can just read

660 — Owner and Group can read and write

640 — Owner can read and write, and Group can read

777 and 666 are rarely used, except in /tmp.

Thanks Ilmari Karonen for pointing out the ones in common usage!

To make it un-executable, run:

For example i created .sh file:

After i write some code on vi editor, i’ll exit from vi editor:

Let all users run your script

As stated you can set the execution bit of the file to make it executable with chmod +x . But you can also use chmod a+x :

NOTES:

  • A script doesn’t have to end with .sh it can end in .txt as shown here, or have no extension whatsoever.
  • Instead of just +x (only you can execute the script), use a+x so all users can execute the script.
  • The file name has a * appended to it to indicate it is executable. Also the file name changes color to green on most systems.

Run a script without making it executable

You can still run a bash script without making it executable. For example:

Источник

How To Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD

H ow do I compile C program and create an executable file under Linux or UNIX operating systems?

Читайте также:  Журнал обновлений microsoft windows 10
Tutorial details
Difficulty level Easy
Root privileges No
Requirements C compiler
Est. reading time 5m

[/donotprint]But you can use gcc command to compile program. First make sure you have gcc C compiler installed:

Make Sure Compiler Is Installed On a Unix-like System

Type the following which command or type command or command command to verify that gcc is installed:
$ type -a gcc
$ command -V gcc
$ which gcc
Sample outputs:

Find out version of gcc, run:
$ gcc —version
Sample outpust:

Syntax

The syntax is as follows to compile a C program on a Unix-like operating system:
gcc program.c -o program-output
OR
cc program.c -o program-output
OR
make program

Writing Your First C Program Under a Linux / UNIX-like system

Use a text editor such as vi or gedit or nano to create a C program called first.c:
$ vi first.c
Type the following lines (program):

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How Do I Compile My C Program?

To compile C program first.c, and create an executable file called first, enter:
$ gcc first.c -o first
OR
$ cc first.c -o first
To execute program first, enter:
$ ./first
Output:

However, both FreeBSD and Linux support direct make (GNU make utility to maintain groups of programs) command on C program without writing a make file. Remove, first program using the rm command:
$ rm first
$ make first

Output:

Execute program first:
$ ./first
Please note that above hack works with GNU/make only.

See also:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Your c code is missing the filename of the #include directive.

Thanks for heads up.

Hi, I was just wondering how would you create an executable for numerous C programs? I am modifying the Windows Frotz interpreter for a college assignment with the aim to output the game data to a html page. The source for the interpreter contains numerous C files in a number of subdirectories. I have made the modifications but need to create the executable to see if it works properly. This is my first time creating an executable so i’m a bit unsure how to go about it. Any help would be greatly appreciated.

I just want to thank you. I am 57 years old and I just started computing two years ago. I have built three towers, which is no big deal; but, I have been running a lot of different systems. I want to learn c and c++ and you have helped me a lot. I am grateful for any help I can get. I usually do stuff on my own.I still wonder what the greatest difference is between BSD and Debian other than software installation. I do think that BSD is closer to being pure UNIX than Debian, but the kernels are so alike or an I wrong? It world be nice to know. Anyway, this letter is to thank you.

Nice to see at 57, you are learning BSD and C/C++ development.

*BSD is under BSD vs Linux is under GPL License
Network stack

Little change in command syntax for few utilities

A BSD and Linux kernel are different and follows different development methods and approach.

Please see this post along with discussion for more info.

i want a C program Compiler Code In VI editor

hi, i ve installed red hat linux.and trying to compile C++ prg.but getting a err msg saying that gcc not found.then i tried to see which compiler installed on my linx.To do so i am giving cmd – which
gcc – output is /usr/bin/which: – no gcc.

and i am not able to compile my prog..

plz help me in dis regard.

You need to install install update your repositories, and install it thru the RPM comander or do it with de packgesmanager.

install g++ . in redhat and centos or fedora yum install g++

king777 – you need to make sure the gcc development package is selected when you install redhat. Otherwise, you have to download/install gcc manually.

i don’t know about software creation iam from a village school of india
shall you pls send any program in c for me iam too interest in these

If using the Unix system

Follow the below steps:
$ vi welcome.c
Save the content written below (This is called programme)

include
int main(void)
<
printf(“WELCOME TO THE WORLD OF C\n”);
return 0;
>

Step 3 ) comiple the progamme
gcc welcome.c -o WELCOME

Step 4) For execution
$ ./WELCOME

THNX. very useful. 😀 😀 😀

After typing the following command “$ which gcc” to verify that gcc is installed, the Output was “/usr/bin/which: no gcc in (/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/omar/bin)
” , instead of being “/usr/bin/gcc”. what i have to do now?

I would like to thank you very much. I am new user of linux and C language, I was trying to compile a small program for hours. You made it so easy thank you again and god bless you.

Many thanks for this! I love programming in C and perl and getting my feet wet with Linux/UNIX.

As a followup question, how would I take a program I’ve created (a calculator, for example), and package it so it can run as an executable on another computer (or even embed in a linux distro)?

i want to know how to create executable file.

when u compile any *.c file automatically the executable file is created, which is linked to ld further to get the output,the executable file created after compiling is called as intermediate object file or executable

I file con be executable thru the permitions
chmod +x program make in a sudo way

how to compile a user defined function in c using gcc.
what do you mean by segmentation fault.

Hi
i’m new linux worker.
befor linux i work on windows7 and now for robotic programing and… work in linux fedora12.
thank u for this note.
and i have a question!(excuse me for my bad english,becous i’m iranian)
–> how we can load and open the serial port in program??
–>how can i open the camera on linux?
–>and how we can comunicate with other laptop with wi-fi?

I followed the steps mentioned above.I m able to compile the code.But when i m givin “./first” to
run it its giving the following “-bash: ./first: Permission denied”. Please help me

Usually the compiled programm. If that’s not the case run
sudo chmod u+x
which gives the user permission to execute the file.

hii…iam on redhat 5.0….whenever i try to compile c program…iam gettin an error msg stating gcc command not found….explain me the easy way to install the gcc….please help me

i tried which gcc as you said but iam getting gcc not in and ./a.out is also not working…please someone help me out

HI! I’m new for linux and I’m using Fedora 13 actually I wnt to install compiler for c and c++ someone told me gcc is inbuilt in fedora 13, is it possible tell me how to know it’s installed or not or how to install it? how to create c or c++ programs in fedora 13 or any linux system, how to install other IDE’s for development. Please someone help me out……

I would like to know how can I execute the file “first” without using preceeding it
with the characters “./” . Any help is appreciated.

when i try to compile a C pogram in centos 5.2…i get the following error
$ cc prog.c
bash: cc: command not found

how to get rid of this prob…pls help

actually it is because gcc is not installed so type “yum install gcc” if u have a centos or fedora

i tried it as su -c’yum install gcc’
but it also get faliure.to install the gcc so hw could i hv 2 go for install.

yum only works if you have a yum repository… otherwise you have to do it manually..

when i try to compile a C pogram in Fedora 13 .i get the following error
$ cc prog.c
bash: cc: command not found
how to get rid of this prob…pls help

Dear friend,
I cannot also create,compile & execute C program in Fedora 15.Please help me to do this mailing me the required commands.

I can’t not compile program

TopDollar !! 🙂
My first c-compilation yummie!
And it works perfect following your instructions.
Peace out!

hi friends can any one say me how can i add libraries(.h) to my gcc compiler

This really doesn’t help me to understand how to make the compiled program executable. When I say executable I mean a *.exe file that I can call by name (such as typing “first_program.exe” in a terminal & having it execute), or by double clicking in a graphical environment.

Does ANYBODY know how to do THAT?!

Can I use script on Windows XP??

If we have a program with multible *.c or *.h files how do we compile those together? And how do we make this a *.exe file or some sort of exacutable?

i just want to know how to install gcc files in fedora. /

use the following command in terminal (it works for me in fedora 14)
yum groupinstall ‘Development Tools’

This will download (approx 105Mb) of data and automatically install it on your system. the best thing is you dont have to worry about the dependencies 😉
then type gcc and press enter
you should get this error “no input files selected” this means gcc is installed.

i tried but it says that you need to be root to perform this command. now what should i do?

Источник

Читайте также:  C try catch segmentation fault windows
Оцените статью