Jumat, 11 April 2014

10 example of chmod command in UNIX Linux




chmod command in UNIX or Linux is used to change file or directory permissions. This is one of many UNIX basic commands which a UNIX or Linux user must be familiar with. In this UNIX command tutorial we will see how to change file permissions using chmod command, what are file permissions in UNIX, how to change permissions of directory and sub-directory using UNIX chmod command and finally how to create executable files in UNIX using chmod command. Before going directly into examples of chmod command let's spend few minutes on understanding file permissions in UNIX and why do we need to change file permissions etc.







chmod command examples in unix linuxIn UNIX each file has three permission read, write and execute and three classes user(owner), group and others. So each file is provided permissions in combination of class and permissions. You can see the permission of individual file or directory by using ls command. For example in below file






example@localhost~/test ls -lrt stock_trading_systems

-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*






It has read, write and execute permission for all users, group and others. You can read more about file permissions in UNIX in my post beginner’s guide to UNIX file permissions. To read more about UNIX and Linux file permissions see this UNIX tutorial on file and directory permissions.






Chmod command Examples in UNIX and Linux





Now let's see some practical and frequently used example of chmod command in UNIX





chmod command Example 1: making read only file in Unix


In this example of chmod command in UNIX we will see how to make a file read only by only providing read access to owner. You can also give read access to group and others and keep write access for owner which we will see in subsequent examples.






example@localhost~/test ls -lrt stock_trading_systems

-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*





Here file stock_trading_systems has read, write and execute permission “-rwxrwxrwx" for all



example@localhost~/test chmod 400 stock_trading_systems





400 means 100 000 000 means r-- --- --- i.e. read only for owner



example@localhost~/test ls -lrt stock_trading_systems

-r-------- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems





Now file is read only and only owner can read it “-r--------"












chmod command Example 2:  change permissions only for user, group or others.


In this example of chmod command we will see how to change file permissions on user, group and others level. You can easily modify file permission for any of these classes. If you are using text format than “u” is user , “o” is other and “g” is group also “r” is read , “w” is write and “x” is execute. + means adding permission and “-“is removing permission.






example@localhost~/test ls -lrt chmod_examples

-r-------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples



example@localhost~/test chmod u+w chmod_examples



example@localhost~/test ls -lrt chmod_examples

-rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples






Now let’s change file permissions only for group by using chmod command






example@localhost~/test ls -lrt chmod_examples

-rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples



example@localhost~/test chmod g+w chmod_examples



example@localhost~/test ls -lrt chmod_examples

-rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples






In this chmod command example we will change permission only for others class without affecting user and group class.






example@localhost~/test ls -lrt chmod_examples

-rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples



example@localhost~/test chmod o+w chmod_examples



example@localhost~/test ls -lrt chmod_examples

-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 chmod_examples









Chmod command Example 3:  change file permissions for all (user + group + others)


In last unix chmod example we learn how to change permission for user, group and others individually but some time its convenient to change permissions for all instead of modifying individual permission for user, group and other.


If you are providing permission in text format than “a” is used for “all” while “u” is used for user.






example@localhost~/test ls -lrt linux_command.txt

-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt



example@localhost~/test chmod a+x linux_command.txt



example@localhost~/test ls -lrt linux_command.txt

-rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*









Chmod command Example 4: Changing permissions in numeric format of chmod command


Chmod command in UNIX and Linux allows modifying permissions not just on text format which is more readable but also on numeric format where combination of permissions are represented in octal format e.g. 777 where first digit is for user, second is for group and 3rd is for others. Now if you write down 1st digit in binary format it will be written as 111 on which 1st digit is for read permission, 2nd is for write and 3rd is for execute permission.






example@localhost~/test ls -lrt unix_command.txt

-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 unix_command.txt



example@localhost~/test chmod 777 unix_command.txt



example@localhost~/test ls -lrt unix_command.txt

-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt*






Chmod command Example 5: How to remove file permission using chmod command Unix


In this example of chmod command in UNIX we will see how to remove various permissions from files. You can easily remove read, write or execute permission from file using chmod command in both numeric and text format. Below examples shows removal of execute permission represented by –x in text format.






example@localhost~/test ls -lrt linux_command.txt

-rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*



example@localhost~/test chmod a-x linux_command.txt



example@localhost~/test ls -lrt linux_command.txt

-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt






Chmod command Example 6: changing permission for directory and subdirectory recursively in Unix


This is the most frequently used example of chmod command where we want to provide permission to any directory and all contents inside that directory including files and sub directories. By using –R command option of chmod in Unix you can provide  permissions recursively to any directory as shown in below example of chmod command.






example@localhost~/test ls -lrt

total 8.0K

-rwxrwxrwx  1 example Domain Users    0 Jul 15 11:42 unix_command.txt*

drwxr-xr-x+ 1 example Domain Users    0 Jul 15 14:33 stocks/



example@localhost~/test chmod -R 777 stocks/



example@localhost~/test ls -lrt

total 8.0K

-rwxrwxrwx  1 example Domain Users    0 Jul 15 11:42 unix_command.txt*

drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/



example@localhost~/test ls -lrt stocks

total 0

-rwxrwxrwx 1 example Domain Users 0 Jul 15 14:33 online_stock_exchanges.txt*






Chmod command Example 7:  How to remove read and write access from file for all


So far we have been seeing how to provide read, write and execute permission to file and directory in UNIX and now we will see opposite of that i.e. how to remove read, write and execute access. Its simple in text format because instead of + we are going to use -. Just like + used to add permission – will be used to remove permissions.






example@localhost~/test ls -lrt stock_trading_systems

-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*



example@localhost~/test chmod a-wx stock_trading_systems



example@localhost~/test ls -lrt stock_trading_systems

-r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems






Chmod command Example 8: setting execute permission only on directories without touching files





Many times we just want to provide directory or subdirectory execute permission without modifying permissions on file just to make those directories searchable. Until I know this command I used to do this by finding all directory and then changing there execute permission but we have a better way to do it by using chmod command in UNIX. You can use "X" (capital X) options to provide execute permission to only directories without touching files. Let’s see an example of chmod command for that:






example@localhost~/test ls -lrt

total 8.0K

-r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems

drw-rw-rw-+ 1 example Domain Users    0 Jul 15 14:33 stocks/



example@localhost~/test chmod a+X *



example@localhost~/test ls -lrt

total 8.0K

-r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems

drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/






Remember to use X (capital case) if you use x (small case) it will affect all files and directories.





Chmod command Example 9:  changing mutiple permission for a file or directory in Unix or Linux


You can change combination of user + groups or groups+ other in one command to modify permissions of files and directory. Below example of chmod command just doing same it’s providing execute permission for user and read, execute permission






example@localhost~/test ls -lrt

total 8.0K

-r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems

drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/



example@localhost~/test chmod u+x,g+x stock_trading_systems



example@localhost~/test ls -lrt stock_trading_systems

-r-xr-xr-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*






Chmod command Example 10: How to copy permission from one file to another in Unix


This is very interesting example of chmod command in UNIX which copies permission from one file to another. You can easily reference source file and copy all permissions on that file to destination file as shown in following chmod example:






example@localhost~/test ls -lrt future_trading

-rwxrwxrwx 1 example Domain Users 0 Jul 15 15:30 future_trading*



example@localhost~/test ls -lrt stock_trading_systems

-r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems



example@localhost~/test chmod --reference=stock_trading_systems future_trading



example@localhost~/test ls -lrt future_trading

-r--r--r-- 1 example Domain Users 0 Jul 15 15:30 future_trading









These were some of frequently used example of chmod command in UNIX or Linux. Chmod command is as useful as UNIX find command or grep command and knowing how to change file permissions is essential skill while working in UNIX. Please share if you have any other example of chmod command which we should be aware of.





Other UNIX command tutorial you may like


































Source:http://javarevisited.blogspot.com/2012/03/10-example-of-chmod-command-in-unix.html

Tidak ada komentar:

Posting Komentar