Did you write or modify a script in a text editor but can’t get it to execute in a Terminal program? There’s an easy solution to this common problem. Here’s how to make an executable file from a plain text file that will run in a Unix, macOS or Linux Terminal application.
In order to modify your text file, we will using the
chmod command. This is a very useful Unix command used to change the permissions of files. Besides forcing stubborn files to empty from your trash can, one of the most used options for the
chmod command is changing the execution rights of files.
Edit Your Text File
If you are just learning how to use Unix or Linux, then you might be modifying an existing Unix executable file using a plain text editor. But as soon as you save the file, it won’t automatically open in your Terminal application anymore. Why? When you saved the file it became a text file and your operating system no longer considers it to be “executable” file.
Open Your Terminal Application
Depending on your operating system (Mac, Unix, or Linux), your Terminal application might have a different name, but regardless, the following steps will be the same.
- The macOS already comes with a Terminal application. You can find it in your Utilities folder.
- Macintosh HD > Applications > Utilities > Terminal
LINUX OR UNIX
- Most Linux distributions include a Terminal application. If you don’t immediately see a Terminal icon, then try pressing Ctrl+Alt+T on your keyboard. Your Terminal application should open.
- Another option is use the search bar. Just search for “Terminal” to see if you have a Terminal app.
- If you can’t find your Terminal app, then download a new Linux Terminal emulator.
The chmod +x Command
Once you have a new Terminal window open, type sudo chmod +x and add a space at the end. DON’T press return yet.
Drag Your Text File Into The Terminal Window
Next, drag the text file that you want to convert into a Unix or Linux executable file into the Terminal window. This should add the file path at the end of your chmod +x command.
sudo chmod +x /path/file-name
Press return. After you press return, the Terminal will execute your chmod command and make your text file a Unix executable file. You should now be able to double-click on the file and have it automatically open in Terminal and execute accordingly.
If your file doesn’t automatically open in Terminal, then you might need to change the default program assigned to handle the file for your operating system.
Change The Default Open With Application (macOS)
Even if your Terminal program knows that it should execute your file, your operating system may still try to open your file in a text editor. Here’s how to make an executable Mac file that you can just double-click on and open in Terminal.
Right-Click On Your Text File And Select “Get Info”
Find your text file in the Macintosh Finder, right-click on it, and select “Get Info”.
Select Other From The “Open With” Drop-Down Menu
One of the options in the Get Info window is the “Open With” drop-down menu. Click on the drop-down menu and select “Other” at the bottom.
Search For The Terminal Application
In the top right corner is a search box. Type in “Terminal” in the search box and the Terminal application should appear in the search results. You will need to enable the “All Applications” option before you can click on the “Add” button.
Double-Click On Your File
Now double-click on your text file and it should automatically open in Terminal and execute properly. That wasn’t so hard, right?
How To Make An Executable File From A Text File
Congratulations! You did it! You now know how to make an executable file from a text file that will automatically execute in the macOS, Unix, or Linux Terminal. Give yourself a high five and grab a beer. Actually, grab us a beer too while you are at it. These tutorials don’t write themselves.
If you have any questions or comments, then please let us know in the comments.
Thank you for explaining this step by step. The other tutorials online assume that we know more than we might. I appreciate you making this so easy to follow.