Linux chmod to executable

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?

It is not a duplicate, because I have asked specifically about making it application/x-executable. The other question just asks for opening sh file in terminal.

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.

Do you know how to use sudo command after entering the command as: «alias command1 = ‘/home/user_name/dir/script.sh’. In mine, it works without sudo, but not with it.

@user1993 Generally, using ./filename.sh specifies a file in the current directory and using filename.sh specifies a file in the current directory or any directory of PATH. The first usage removes any uncertainty as to which file is accessed. In this case, you are attempting to execute the script with bash or another interpreter (by virtue of assumed #!/bin/bash as first line in your script) just by entering the filename. This usage requires the directory is specified. Alternatively, you can try bash filename.sh which seems to work with unspecified directory.

There are two ways of making a file executable:

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.

enter image description here

Terminal / Command method:

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)

Читайте также:  How to turn off linux

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

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:

enter image description here

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

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!

Источник

Linux Chmod Command

Unix/Linux file permissions are controlled with the [.inline-code]chmod[.inline-code] command. This command accepts numeric representations of permissions, as well as symbolic representations. For more details on file permissions, see our previous article.

[#with-numeric-representations][.inline-code]chmod[.inline-code] With Numeric Representations[#with-numeric-representations]

The most common usage of [.inline-code]chmod[.inline-code] is a command like [.inline-code]chmod 644 FILENAME[.inline-code], using the numerical representation of the permission you’re trying to achieve. Check our writeup on file permissions to see some common numeric representations you may want to use with [.inline-code]chmod[.inline-code].

[#with-symbolic-representations][.inline-code]chmod[.inline-code] With Symbolic Representations[#with-symbolic-representations]

Symbolic representations can be a little easier to grok than numeric representations. For example, to add the executable bit for all users to a file, you would run [.inline-code]chmod a+x FILENAME[.inline-code] («all add executable»). To add write capability for just the owner, you would run [.inline-code]chmod u+w FILENAME[.inline-code] («user add write»). But you can also just run [.inline-code]chmod 644[.inline-code] to a file to make it readable to everyone and writeable by you, or [.inline-code]chmod 755[.inline-code] to change permissions on a directory.

Читайте также:  Расширение deb в linux

When using symbolic representation, [.inline-code]u[.inline-code] means user (owner), [.inline-code]g[.inline-code] means group, [.inline-code]o[.inline-code] means other (world), and [.inline-code]a[.inline-code] means all (user, group, and other). Any combination of these are combined with an operator: [.inline-code]-[.inline-code] to remove a permission, [.inline-code]+[.inline-code] to add, or [.inline-code]=[.inline-code] to set permissions explicitly.

Note that «group» refers to the Unix group that owns the _file_, not the current user’s group. That’s controlled by [.inline-code]chown[.inline-code] or [.inline-code]chgrp[.inline-code] and is out of scope for this post, but be aware that sometimes a file is owned by a group that the current user isn’t a part of, so it can be an entirely separate permission. For example, in the case of a website, it’s common for the files to be owned by the current user, but assigned to a [.inline-code]www[.inline-code] group (or similar). You would want permission for both the user and group to modify the file in this case, and permission for the world (other) to read it.

To use symbolic representations, combine one or more user types, an operator, and permissions. For example, to add read and write permissions to user and group, you would use [.inline-code]chmod ug+rw PATH[.inline-code]. To set user and other permissions to read/execute but not write, you could use [.inline-code]chmod uo=rx PATH[.inline-code]. See the [.inline-code]man chmod[.inline-code] page for more examples.

[#controlling-read-and-write]Controlling who can read and write[#controlling-read-and-write]

The [.inline-code]r[.inline-code] and [.inline-code]w[.inline-code] bits of a file determine who can read and who can write to a file. If a user has read access, they’ll be able to [.inline-code]cat[.inline-code] the file, but without write access, they’ll be unable to modify it (this includes the ability to delete the file). A file that has the permissions [.inline-code]-rw——-[.inline-code] will be readable and writable only by the owner of the file (current user) but completely inaccessible to anyone else. In most cases you want the file to be readable by everyone, but only writable by you, which would mean permissions of [.inline-code]-rw-r—r—[.inline-code], or 644 (see the [file permissions article](>) for more details on numeric representations).

[#oops-i-broke-it]Oops, I broke it[#oops-i-broke-it]

If you ever mess up the permissions on a file and find yourself unable to modify it further because you don’t have write access to it (operation not permitted), you can fix it using [.inline-code]sudo[.inline-code], assuming you have super user access to the current system.

If you do have super user access to the system, you can bypass any permissions on a file and make changes to them as needed. For a regular file, to restore your ability to modify and delete it, use [.inline-code]sudo chmod 644 FILENAME[.inline-code], enter your system password, and then proceed.

If you don’t have access to [.inline-code]sudo[.inline-code] on your system, you’ll need to contact the system administrator to modify your access to any files you lack permissions for.

Читайте также:  Checking time on linux

[#making-a-file-executable]Making a file executable[#making-a-file-executable]

If the executable bit ([.inline-code]x[.inline-code]) is set on a file, it can be executed directly by the users with that permission. In the case of a binary or script (with a proper shebang), this means that you can call it without having to pass it to a shell or script processor. So, instead of [.inline-code]python3 myscript.py[.inline-code], you can just run [.inline-code]./myscript.py[.inline-code]. All command line utilities (binaries) have the executable bit set for at least the owner (user) and usually for group and world (other) as well.

To make a file executable for all users, use [.inline-code]chmod a+x FILENAME[.inline-code]. You can change the [.inline-code]a[.inline-code] to [.inline-code]u[.inline-code]ser, [.inline-code]g[.inline-code]roup, or [.inline-code]o[.inline-code]ther, or a combination of those letters, to specify exactly which users can execute the file, e.g. [.inline-code]chmod ug+x[.inline-code].

Note that directories must have the executable bit set in order for a user to enter them with [.inline-code]cd[.inline-code] or list them with [.inline-code]ls[.inline-code]. If the user doesn’t have permission to execute, they’ll be unable to enter or list the directory. This means you can hide an entire directory from users other than you or outside of your group using [.inline-code]chmod go-x DIRECTORY[.inline-code].

[#chmod-options][.inline-code]chmod[.inline-code] Options[#chmod-options]

There are a few switches available for the [.inline-code]chmod[.inline-code] command. The most pertinent ones tell [.inline-code]chmod[.inline-code] how to deal with symbolic links.

The [.inline-code]-h[.inline-code] switch tells [.inline-code]chmod[.inline-code] that if the file is a symbolic link, change the permissions on the link itself, rather than the file it points to. If you’re working with symbolic links and don’t want to affect the original file, the [.inline-code]-h[.inline-code] switch is your friend.

The [.inline-code]-R[.inline-code] switch tells [.inline-code]chmod[.inline-code] to act recursively, which is generally advised against, as we cover in the Recursive chmod article.

But if you _are_ using [.inline-code]chmod -R[.inline-code], be aware of the [.inline-code]-H[.inline-code] switch, which stops the recursive action from following symlinks, and the [.inline-code]-L[.inline-code] command which _forces_ all symlinks to be followed. The default is the [.inline-code]-P[.inline-code] switch, in which no symlinks are followed.

The [.inline-code]-v[.inline-code] switch will offer verbose output, showing the name of affected files when the command runs. If you specify it twice, it will also output the old and new permissions, in both octal and symbolic representation:

 $ chmod -vv 777 to_changelog.rb to_changelog.rb: 0100755 [-rwxr-xr-x ] -> 0100777 [-rwxrwxrwx ]

[#examples]Examples[#examples]

Set a file’s permissions to [.inline-code]-rw-r—r-[.inline-code], readable and writable by owner (user) and readable by everyone else:

Make a file or directory executable by the owner ([.inline-code]-rwx-r—r—[.inline-code]), readable by everyone else

In the case of a directory, this means that only the owner can use [.inline-code]cd[.inline-code] or [.inline-code]ls[.inline-code] on it, or affect anything it contains:

Make a file executable by anyone (e.g. [.inline-code]-rwx-r-x-r-x[.inline-code])

Make a file inaccessible to anyone but the owner ([.inline-code]-rw——-[.inline-code])

Remove read, write, and execute for group and world, leaving user (owner) permissions alone

Read more about Unix file permissions here. If you’re curious about batch applying chmod to files and directories, check out the Recursive [.inline-code]chmod[.inline-code] article.

Источник

Оцените статью
Adblock
detector