Knowledgebase

How to change file and folder permissions via SSH

Changing file permissions through SSH can be accomplished with the command chmod. Type the command followed by the desired permissions and the name of the file. For example:

chmod 644 example.txt

will change the permissions for the file example.txt that's in the current working directory to 644 (rw-r--r--). Don't forget to include the file extension (.txt in our example). If the file has spaces in its name put quotation marks around it. If the file is not in the current working directory and you don't want to change the working directory, you can use the full path to the file (e.g. chmod 644 /home/username/public_html/example.txt). You can set the permissions of several files by executing one command. Just list the files one after the other (separated by a single space). For example:

chmod 644 example.txt example.php example.html

will change the permissions to 644 for the three listed example files that are in the current working directory. You can use wildcards (*) to change the permissions of all the files from the same type. For example:

chmod 644 *.php

will change the permissions to 644 of all PHP files in the current working directory.

Folder permissions are change in exactly the same way. For example:

chmod 755 images

will change the permissions to 755 of a folder called images that's in the current working directory. This, however, will only change the permissions of the folder itself. If you want to change the permissions recursively, meaning to change the permissions of the folder and everything in it, you can use the -R option. For example:

chmod -R 755 images

will change the permissions to 755 of the folder images and all the subfolders and files inside it. You can also change the permissions of several folders with one command by listing them (e.g. chmod 755 files images scripts).

The chmod command can also be very useful in combination with other commands. For example, if you want to change the permissions of all files but you don't want the permissions of the folders to be changed, you can execute the following command:

find . -type f -exec chmod 644 {} \;

It will change the permissions of all files in the current working directory and those in all its subdirectories to 644.

In a similar way you can change the permissions of all folders without modifying the permissions of files. The following command:

find . -type d -exec chmod 755 {} \;

will change the permissions of all directories in the current working directory (and the subdirectories of those directories) to 755.

For some more details on what file permissions are, you can check out the tutorial on file permissions.

Was this answer helpful?

 Print this Article

Also Read