<jupyter_start><jupyter_text>HDFS File PermissionsLet us go through file permissions in HDFS.<jupyter_code>%%HTML
<iframe width="560" height="315" src="https://www.youtube.com/embed/I8ZCZYQTaVU?rel=0&amp;controls=1&amp;showinfo=0" frameborder="0" allowfullscreen></iframe><jupyter_output><empty_output><jupyter_text>* As we create the files, we can check the permissions on them using `-ls` command.* Typically the owner of the user space will have **rwx**, while members of the group specified as well as others have **r-x**.* **rwx** stands for read, write and execute while **r-x** stands for only read and execute permissions.* We can change the permissions using `hadoop fs -chmod` or `hdfs dfs -chmod`. However one can change the permissions of their own files.* We can specify permissions mode (e.g.: `+x` to grant execute access to owner, group as well as others) as well as octal mode (e.g.: 755 to grant rwx for owner, rx for group and others)If you are not familiar with linux command chmod, we would highly recommend you to spend some time to get detailed understanding of it as it is very important with respect to file permissions.<jupyter_code>%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Granting write permissions on the folder to others.<jupyter_code>%%sh

hdfs dfs -chmod -R o+w /user/${USER}/retail_db/orders
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Granting write permissions on the folder to group.<jupyter_code>%%sh

hdfs dfs -chmod -R g+w /user/${USER}/retail_db/order_items
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* We can remove write permissions for every one.<jupyter_code>%%sh

hdfs dfs -chmod -R -w /user/${USER}/retail_db/orders
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* No files can be copied to the folder or can be deleted from the folder. Below command will fail.<jupyter_code>%%sh

hdfs dfs -rm /user/${USER}/retail_db/orders/part-00000
%%sh

hdfs dfs -chmod -R -w /user/${USER}/retail_db/order_items
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Adding write permissions only to owner. Now the owner will be able to delete the file, but others cannot.<jupyter_code>%%sh

hdfs dfs -chmod -R u+w /user/${USER}/retail_db/orders
%%sh

hdfs dfs -rm /user/${USER}/retail_db/orders/part-00000
%%sh

hdfs dfs -chmod -R u+w /user/${USER}/retail_db/order_items
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Let us go through the details using octal format.<jupyter_code>%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Granting write permissions on the folder to others. We can set these permissions for owner, group and other.|Binary Value|Permissions Mode|Decimal Value|---|---|---||000|---|0||001|--x|1||010|-w-|2||011|-wx|3||100|r--|4||101|r-x|5||110|rw-|6||111|rwx|7| * Granting write permissions on the folder to others.<jupyter_code>%%sh

hdfs dfs -chmod -R 757  /user/${USER}/retail_db/orders
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Granting write permissions on the folder to group.<jupyter_code>%%sh

hdfs dfs -chmod -R 775 /user/${USER}/retail_db/order_items
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* We can remove write permissions for every one.<jupyter_code>%%sh

hdfs dfs -chmod -R 555 /user/${USER}/retail_db/orders
%%sh

hdfs dfs -chmod -R 555 /user/${USER}/retail_db/order_items
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output><jupyter_text>* Adding write permissions only to owner.<jupyter_code>%%sh

hdfs dfs -chmod -R 755 /user/${USER}/retail_db/orders
%%sh

hdfs dfs -chmod -R 755 /user/${USER}/retail_db/order_items
%%sh

hdfs dfs -ls /user/${USER}/retail_db<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13582428679626357, 'fraction_numerical': 0.018682150971976775, 'mean_word_length': 5.21978021978022, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '26068', 'license': 'MIT', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1441, 'n_words': 420, 'path': '02_overview_of_hdfs/15_hdfs_file_permissions.ipynb', 'repo_name': 'itversity/spark-sql', 'size': '3961'}