Change all files owners linux

How to chown/chmod all files in current directory?

I am trying to change the ownership and permissions of some files (and directories) in the current directory. I tried this:

. expecting that it would affect all the files in the current directory, but instead only affected the directory that I am in (which is the opposite of what I want to do). I want to change it on all the files without affecting the current directory that I am in. How can I chown and chmod all files in current directory?

@Shi I think it’s a fair question. Reading that man page wouldn’t help. Globbing is not part of chmod . It is builtin to the shell. Also reading documentation on globbing sucks the life out of you (I spent way to much time figuring out all the zsh’s features).

5 Answers 5

You want to use chown username:groupname * , and let the shell expand the * to the contents of the current directory. This will change permissions for all files/folders in the current directory, but not the contents of the folders.

You could also do chown -R username:groupname . , which would change the permissions on the current directory, and then recurse down inside of it and all subfolders to change the permissions.

chown -R username:groupname * will change the permissions on all the files and folders recursively, while leaving the current directory itself alone. This style and the first style are what I find myself using most often.

Also, beware of the dangers of the period. As it is placed right next to the almight «/» on the keyboard. A simple typo can easily turn chown -R username:groupname . into chown -R username:groupname / . Making a 2 second task a 2 day nightmare.

Just upvoted as the best and most concise anwser, although I’d be specific and name the directory, just to avoid a slip of the keyboard: chown -R username:groupname directoryname

This doesn’t do all of the files, it ignores the dot files, which were the ones I wanted to chown anyway (the git files needed to be owned by my user so. ) I was able to fix it with chown -R username:groupname ./.*

If you also want to recursively change subdirectories, you’ll need the -R ( -r is deprecated) switch:

chown -R username:groupname *

chown is great if you are a superuser. I had an issue where someone else had run make in my directory, and now owned some files that I could not modify. Here is my workaround which handles files and directories, although it leaves the directories lying around with suffix .mkmeowner if it can’t delete them.

  • The following script changes ownership of files and directories passed to it to be owned by the current user, attempting to work around permission issues by making a new copy of every directory or file not owned by the current user, deleting (or trying to delete) the original file, and renaming appropriately.
  • The intent is for it to be an abbreviation for «make me owner». I don’t use underscores because they are a pain to type.
% mkmeowner . % mkmeowner dirpath1 dirpath2 
#!/bin/bash [ "x$1" == "x-h" ] || [ "x$1" == "x--help" ] && cat \; done 
#!/bin/bash # change ownership of one file or directory f="$1" expr match "$" '.*\.mkmeowner$' > /dev/null && exit 1 # already tried to do this one if mv -f "$f" "$.mkmeowner"; then cp -pr "$.mkmeowner" "$f" && rm -rf "$.mkmeowner" exit 0 fi exit 1 

(1) We prefer answers that include some explanation. I was taken by surprise when I read your scripts and saw what they did, because your introductory paragraph didn’t give me a clue. (2) You forgot to mention that the user must put mkmeownerone into the search PATH. mkmeowner will blow up if that isn’t done. (3) Please learn how (i.e., when and where) to use quotes in shell commands and scripts. Saying $ isn’t a useful alternative; see this. … (Cont’d)

Читайте также:  Монтирование сетевых дисков windows linux

(Cont’d) … (4) If you run your script on a directory, it will potentially copy every file in that directory twice. (5) expr is antiquated. It’s more efficient to do simple string matching in the shell; bash also supports regular expression matching. For that matter, you could do the filename test in find. (6) Your expr match command tests whether $ ends with mkmeowner , not whether it ends with .mkmeowner . Therefore, the script will not work on itself. … (Cont’d)

(Cont’d) … Now, arguably, this is a good thing — you don’t want to be moving and potentially deleting a script while it’s running. (It’s not necessarily going to cause a problem, but it can be messy.) But you should understand (and document) special cases like that. (7) You might want to use underscores in multi-word strings. dirorfile is hard to read and understand (compare to dir_or_file ), and when I saw mkmeowner , my first thought was of a cat ( mk + meow + ner ).

Thanks for the notes Scott. 1. I thought I did have an explanation but let me be more explicit. 2. good point. I had captured that in my original script, but took out the reference. 3. I’ve been writing shell scripts for 30 years, but sure, I’ll try to learn more. 4. Yes it does. On purpose because if I don’t own the directory, I cannot delete it. I can mention that. 5. Yah I’m old fashioned, but expr still works pretty well 6. ha good point. 7. I might, I think I I get some artistic license though, as underscores are not a rule. I need a script that makes meows!

I don’t think there is anything wrong with $, right? It’s just a little longer than necessary. It’s a format I use often because I may want to use a variable to make a longer string like $.mkmeowner, and I don’t have to remember whether $f.heck means $+heck or $

Читайте также:  Linux терминал удалить пользователя

Источник

How to Change Ownership of Files and Folders Recursively

Learn how to use the chown command to recursively change the user and group ownership of files and directories in Linux command line.

chown owner_name file_or_folder

The problem arrives when you change the ownership of a directory, its content remains unchanged. The solution is not too complicated as well.

To change the ownership of all the contents of a directory, you can use the recursive option -R with chown command:

chown -R owner_name folder_name

If you want to change both owner and group recursively, you can use it in the following manner:

chown -R owner_name:group_name folder_name

Let’s see it in detail and also see how you can change user and group recursively. Things are a lot easier to understand if you are familiar with the concept of file ownership and permission.

chown recursively

To recursively change the ownership of a directory, use it like this:

chown -R new_owner_name directory_name

If you have to change the ownership of multiple directories with their contents, you can do it in the same line:

chown -R new_owner_name directory1 directory2 directory3

Let me show that with a sample example. I have a directory named new_dir with some content in it. This directory and its content are owned by the root user.

[email protected]:~# ls -l /home/abhi/ total 4 drwxr-xr-x 3 root root 4096 May 30 07:30 new_dir [email protected]:~# ls -l /home/abhi/new_dir/ total 20 -rw-r--r-- 1 root root 12813 May 30 07:30 new.txt drwxr-xr-x 2 root root 4096 May 30 07:30 one_more_dir

When I change the ownership of the new_dir, its ownership is changed:

[email protected]:~# chown abhi /home/abhi/new_dir [email protected]:~# ls -l /home/abhi/ total 4 drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir 

But the files and folders inside it are still owned by root.

[email protected]:~# ls -l /home/abhi/new_dir/ total 20 -rw-r--r-- 1 root root 12813 May 30 07:30 new.txt drwxr-xr-x 2 root root 4096 May 30 07:30 one_more_dir 

Now, if I use the recursive option -R with chown command, it changes the ownership for everything inside the specified directory, not just the directory.

[email protected]:~# chown -R abhi /home/abhi/new_dir [email protected]:~# ls -l /home/abhi/new_dir/ total 20 -rw-r--r-- 1 abhi root 12813 May 30 07:30 new.txt drwxr-xr-x 2 abhi root 4096 May 30 07:30 one_more_dir

Example of using chwon recursively

Change both owner and group recursively

The chown command allows you to change the owner as well as the group of files.

To recursively change the owner and group of a directory and all its content, use the chown command like this:

chown -R user_name:group_name directory_name

You can use the same for changing the ownership of multiple folders:

chown -R user_name:group_name dir1 dir2

Conclusion

Recently, I moved a self-hosted Ghost instance to a new server launched with DigitalOcean’s 1-click deployment. I had to upload the entire images folder from the backup (downloaded on the local system) to the new server. The system required changing the ownership of this image folder from root to ghost.

Читайте также:  Linux команда создать пароль

This method saved me the trouble. I hope this quick little tutorial helps you as well.

I highly recommend brushing up the basics of file permissions and ownership.

It’s one of the essential Linux concepts you must know.

Источник

Use chown to set the ownership of all a folder’s subfolders and files?

Will any of these answers work if the files inside a folder are owned by someone else and only have user read/write permission?

6 Answers 6

Usage: chown [OPTION]. [OWNER][:[GROUP]] FILE. or: chown [OPTION]. --reference=RFILE FILE. Change the owner and/or group of each FILE to OWNER and/or GROUP. [. ] -R, --recursive operate on files and directories recursively [. ] 

So you need to run (probably with sudo ):

chown -R USERNAME:GROUPNAME /PATH/TO/FILE 

Or, if the group shall be the specified user’s primary group (usually same name), you can also omit the GROUPNAME and just give the USERNAME: with a colon (no space before it!). It will be set implicitly:

chown -R USERNAME: /PATH/TO/FILE 

To only change the user and leave the group as it is, just specify USERNAME and no group name and no colon:

chown -R USERNAME /PATH/TO/FILE 

To only change the group and leave the owner user as it is, just specify :GROUPNAME with a leading colon:

chown -R :GROUPNAME /PATH/TO/FILE 

My username is timo and I did this to take ownership to all my files and folders on home directory (transferred from another account):

~$ sudo chown -R timo /home/timo/* 

This is how I normally do it, and I usually do this one folder at a time. Doesn’t take but a few moments to work through each folder.

This will also change ownership of symlinks instead of just the destination files that the symlinks point to.

man chown chown options user:group files/folders 

Not sure why other answers did not cover one dot. : And . are interchangeable, so you can use one dot for instance

chown -R user.group files/folders 

Either get to the terminal display mode as described elsewhere or do a ssh login from another computer. Usually the account is intact and it will be accessible via ssh.

You may also have an account on the same machine without the login loop problem. If you do, then login to that account (assuming it will let you sudo).

Once in, open a terminal and find the directory under which you can see the username directories. i.e. /home/username1 /home/username2

if any of the user directories is owned by root change it by running:

sudo chown -R username:username /home/username 

This example is based on an architecture where the user directories are under /home/

Run ls -l again to confirm the directory is owned by the user.

This was tested on Ubuntu 20.04

Источник

Оцените статью
Adblock
detector