Creating executable files in Linux
One thing I plan to be doing is writing (painfully simple) Perl scripts, and I’d like to be able to run them without explicitly calling Perl from the terminal. I appreciate that, to do this, I need to grant them execute permissions. Doing this with chmod is easy enough, but it also seems like a slightly laborious extra step. What I would like is one of two things: Firstly, is there a way to set the execute flag when saving a file? Currently I’m experimenting with gedit and geany, but would be willing to switch to a similarly- (or better-) featured editor if it had this capability. Failing that, is there a way to declare that all files created in a particular directory should have execute permissions? My umask is set to 022, which should be OK, as far as I understand, but it would appear that the files are created as text files (with 666 default permissions) rather than executable files (with 777 default permissions). Perhaps I’m just being lazy, but I figure there must be a more convenient way than chmodding every single script one creates.
You have to print the output of every file. You have to import the right libraries of every file. This seems like another step in the process of programming, and one that is dangerous to circumvent.
5 Answers 5
This should return something like
Then in the first line of your script add:
Then you can execute the file
There may be some issues with the PATH, so you may want to change that as well .
Thanks, but that’s not quite my problem. I’ve already gotten into the habit of starting my scripts with #!/usr/bin/perl. I can run scripts fine once I’ve given them executable permissions; I was just looking for a simpler way of doing so.
May I recommend #!/usr/bin/env perl instead? The env program basically finds the argument given (in this case, «perl») on the PATH and runs that. It’s useful when you’re sending scripts to other people — for example, I use a Mac, and Perl is located in /opt/local/bin.
No need to hack your editor, or switch editors.
Instead we can come up with a script to watch your development directories and chmod files as they’re created. This is what I’ve done in the attached bash script. You probably want to read through the comments and edit the ‘config’ section as fits your needs, then I would suggest putting it in your $HOME/bin/ directory and adding its execution to your $HOME/.login or similar file. Or you can just run it from the terminal.
This script does require inotifywait, which comes in the inotify-tools package on Ubuntu,
sudo apt-get install inotify-tools
Suggestions/edits/improvements are welcome.
#!/usr/bin/env bash # --- usage --- # # Depends: 'inotifywait' available in inotify-tools on Ubuntu # # Edit the 'config' section below to reflect your working directory, WORK_DIR, # and your watched directories, WATCH_DIR. Each directory in WATCH_DIR will # be logged by inotify and this script will 'chmod +x' any new files created # therein. If SUBDIRS is 'TRUE' this script will watch WATCH_DIRS recursively. # I recommend adding this script to your $HOME/.login or similar to have it # run whenever you log into a shell, eg 'echo "watchdirs.sh &" >> ~/.login'. # This script will only allow one instance of itself to run at a time. # --- config --- # WORK_DIR="$HOME/path/to/devel" # top working directory (for cleanliness?) WATCH_DIRS=" \ $WORK_DIR/dirA \ $WORK_DIR/dirC \ " # list of directories to watch SUBDIRS="TRUE" # watch subdirectories too NOTIFY_ARGS="-e create -q" # watch for create events, non-verbose # --- script starts here --- # # probably don't need to edit beyond this point # kill all previous instances of myself SCRIPT="bash.*`basename $0`" MATCHES=`ps ax | egrep $SCRIPT | grep -v grep | awk '' | grep -v $$` kill $MATCHES >& /dev/null # set recursive notifications (for subdirectories) if [ "$SUBDIRS" = "TRUE" ] ; then RECURSE="-r" else RECURSE="" fi while true ; do # grab an event EVENT=`inotifywait $RECURSE $NOTIFY_ARGS $WATCH_DIRS` # parse the event into DIR, TAGS, FILE OLDIFS=$IFS ; IFS=" " ; set -- $EVENT E_DIR=$1 E_TAGS=$2 E_FILE=$3 IFS=$OLDIFS # skip if it's not a file event or already executable (unlikely) if [ ! -f "$E_DIR$E_FILE" ] || [ -x "$E_DIR$E_FILE" ] ; then continue fi # set file executable chmod +x $E_DIR$E_FILE done
How To Make A File Executable In Linux
If you’re someone with a background in a Windows operating system, chances are you’re aware of the “.exe” files. These files are referred to as executable files because their purpose is to execute a series of commands that make up the whole program.
However, when you make the change to a Linux distribution, this division of executable vs. non-executable does not apply. Any file can be made executable in Linux since it does not require a certain extension to be declared as an executable.
This provides a lot of flexibility when it comes to file management.
If you’re someone looking to learn how to make a file executable in Linux, then this guide is meant for you. We’ll provide a step-by-step solution on how you can make any file executable, either using the CLI or the GUI method.
Let’s take a look at the steps.
Method 1: Using The Command Terminal
The first method makes use of the Command Terminal. You can make any file executable by typing certain commands in the Terminal.
Although we’re going to be using Ubuntu 20.04 LTS in this guide, the steps shouldn’t be any different for other Linux distributions. With that said, just follow the steps explained with the help of an example.
First and foremost, open the Command Terminal on your system. The shortcut for Ubuntu is Ctrl + Alt + T.
For this guide, we’ll create a sample file using the echo command by using the following command:
This command will create a string literal with the text “This is a guide on how to make a file executable in Linux” and store it in a file called Test1.
To view the content of the file, type the following command:
You should notice that the file doesn’t end with an extension name. This means that you have the option to make the file executable.
In order to make Test1 an executable file, we’ll use the following command:
In case of a file extension, the command becomes:
Now you can call your file by typing its name in the Terminal as follow:
You can also execute the command in the following manner:
This will provide appropriate permission for the file to be executed.
If the CLI method seems complicated to you, worry not as you can achieve the same results with the help of the GUI.
Method 2: Using the GUI
Unlike the CLI method, the GUI method is much less daunting and simplified to understand what’s going on.
Follow these steps to make a file executable using the GUI.
Start by navigating to the file of your choice. Once you’ve navigated to the file, right-click on it and select “Properties”. A new window should appear.
Once a window opens, click on the Permissions tab.
In the Permissions tab, you should see an option titled “Allow executing file as program.”
You should now have the desired file in an executable format if you followed the steps correctly.
Understanding How File Execution Works
Learning how file execution works in Linux has its benefits as it provides more flexibility when it comes to an understanding of how the file works.
In Method 1, we used the command chmod +x. This was necessary in order to make the file executable since the file required “read” privileges. The “./” tells the Terminal to search for the location of the file.
Aside from Method 1, there are other ways to use the $ chmod command. This flexibility makes $ chmod extremely valuable. A list of options for the $ chmod command is given below:
- $ chmod 775 . This mode allows anyone to execute the file. However, only the owner of the file has permission to write in that file.
- $ chmod 0010 . Only users of a group will be allowed to execute the file.
- $ chmod 0100 . Permission to execute the file belongs exclusively to the user.
- $ chmod 777 . Provides permission to execute the file to all Linux users.
- $ chmod -777 . Doesn’t allow any user to execute the file.
Additional Information
Although the $ chmod command works for files without extension type, it should be noted that you’ll need to specify the file type in case it’s mentioned. For example, if you are dealing with a file that has a .run or .bin extension. The syntax for the execution command would be:
Additionally, make sure you have the correct name, file type, and file location before making any file executable.
Conclusion
If you followed the steps in the guide correctly, then the good news is you are now aware of how to make a file executable in Linux.
This guide covered different methods to make the file executable in Linux. We started by explaining the method involving the Command Terminal and followed up with the method to achieve the same with the help of the Graphical User Interface(GUI). We also covered additional uses of the $chmod command and the different permissions.
With this, we wish you all the best in your journey to master Linux.
Create executable application in linux
I wanna know how to create executable application in linux just like .exe file in windows. most of you have used/seen «Pidgin IM» in linux on click of that it will open the window where you can see your buddies and more similar to gnome-caculator. i want to create the same executable file for my application. thank you adavnce.
Could you further clarify your question? Is your question about wanting to know how to create a desktop/»Start menu» shortcut which the users could click on?
4 Answers 4
The most common way to create executables is to use a compiler. (Just like on windows.) The GNU Compiler Collection (GCC) may already be installed on your Linux system, see if you can find it with:
If not, you’ll have to install it. It has man pages ( man gcc ), info pages ( info gcc ), and an online manual.
Note the gcc command itself is the C compiler part, there’s g++ for C++ and others for other languages (though you have considerable control through command line options).
It sounds like you are looking for a framework such as Qt or WxWidgets. Both these frameworks allow you to create windowed GUI applications for Linux (and Windows and Mac OS X).
You first have to write your program. You could write it in C, C++, Python, Java, or anything. If you want the program to have a GUI, rather than just being command-line based, then you have to write code that paints windows, buttons, etc.
After doing that, you’ll have an executable. In Linux, in contrast to Windows, executable files don’t have a «.exe» suffix. Were you to open a terminal, you could just type in «pidgin» and the program would run.
Finally, to create the desktop/menu shortuct, that is specific to the GUI environment. In gnome, you can right-click on the desktop, select «Create Launcher», and follow the menu to select the executable file you have created — similarly to what you do in Windows.
Not sure if this is what you were asking about, but I hope that is helpful!