Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I pack a folder with lot of sub-folders and files in Windows with 7zip, upload to VPS and then run the command:

tar -xvzf file.tar.gz

then all unpacked files and folders have permissions of 777. How do I get it so that folders will have permissions of 755 and files 644?

share|improve this question

migrated from Jan 30 '12 at 1:01

This question came from our site for professional and enthusiast programmers.

There is a website called "Command Line Fu" that has lots of other ideas also. – djangofan Jan 30 '12 at 1:45
up vote 10 down vote accepted

If you're running tar(1) as a regular user, it will apply your umask by default. If you're running tar(1) as root, then you can give --no-same-permissions command line option to ask tar(1) to respect the umask.

So: either run this as a regular user:

umask 022
tar zxvf file.tar.gz

or run this as root:

umask 022
tar zxvf file.tar.gz --no-same-permissions

You might want to stick umask 022 into your ~/.bashrc, ~/.bash_profile, or ~/.profile. (See bash(1) manpage for full details on the start up files. It's complicated.)

Details on umask can be found in your shell's manpage, the umask(2) system-call manpage, or the umask(1posix) POSIX-provided utility manpage (if you have the manpages-posix installed).

share|improve this answer
umask 022 tar zxvf file.tar.gz --> this asks me for confirmation for each file separately if I want to extract it – Kaspar Lemmo Palgi-Unt Jan 29 '12 at 14:35
Are you over-writing existing files when doing so? – sarnold Jan 30 '12 at 0:30

I found this solution that worked for me. For folders and subfolders:

chmod -R 777 */

And for all files (also in folders and subfolders):

find . -type f -name "*" | xargs chmod 644

All comments welcome if this is not a good way of doing it. I just started learning Linux.

Following comments, a more robust solution that handles special characters nicely would be:

find . -type f -print0 | xargs -0 chmod 644  # For files
find . -type d -print0 | xargs -0 chmod 755  # For directories
share|improve this answer
But the second command gives me on many files this output: chmod: cannot access the': No such file or directory chmod: cannot access Logo.txt': No such file or directory chmod: cannot access `./blog/wp-content/themes/OneRoom/LOGO': No such file or di rectory – Kaspar Lemmo Palgi-Unt Jan 29 '12 at 15:22
777 is dangerous and only applicable to a very small minority of systems. – sarnold Jan 30 '12 at 0:31
To improve upon your second command, find . -type f -print0 | xargs -0 chmod 644. The -name "*" isn't necessary, and xargs(1) will parse filenames on spaces or newlines unless you use the -0 -- which parses filenames on ASCII NUL character, which cannot appear in filenames. The -print0 for find(1) asks it to format its output in a mode suitable for xargs -0 input. – sarnold Jan 30 '12 at 0:32

Run the following commands in the root of the directory to set the desired permissions for your directories and files:

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

Be aware of the space between the closing curly bracket and the back slash

share|improve this answer
find: missing argument to `-exec' – Kaspar Lemmo Palgi-Unt Jan 25 '12 at 16:43
any idea why this command says to me "find: missing argument to `-exec'"? Thanks. – Kaspar Lemmo Palgi-Unt Jan 29 '12 at 14:37
Add a space between the closing curly bracket and the \; – ZNK - M Dec 16 '14 at 8:36

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

share|improve this answer
Welcome to Superuser, please be advised 1 line answers are rarely accepted, can you elaborate more on your answer? – 50-3 Sep 30 '13 at 1:11

If you use capital X in chmod you can use it to set the execute permissions only on directories. i.e.

chmod -R ugo+X .
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.