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?

link|improve this question
There is a website called "Command Line Fu" that has lots of other ideas also. – djangofan Jan 30 at 1:45
feedback

migrated from stackoverflow.com Jan 30 at 1:01

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

3 Answers

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 {}\;
link|improve this answer
find: missing argument to `-exec' – Kaspar Lemmo Palgi-Unt Jan 25 at 16:43
any idea why this command says to me "find: missing argument to `-exec'"? Thanks. – Kaspar Lemmo Palgi-Unt Jan 29 at 14:37
feedback

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).

link|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 at 14:35
Are you over-writing existing files when doing so? – sarnold Jan 30 at 0:30
feedback

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.

link|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 at 15:22
777 is dangerous and only applicable to a very small minority of systems. – sarnold Jan 30 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 at 0:32
feedback

Your Answer

 
or
required, but never shown