90

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

chown username:groupname .

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

Andrew
  • 14,554
  • 30
  • 70
  • 82
  • 4
    @Shi I think it's a fair question. Reading that man page wouldn't help. [Globbing](http://en.wikipedia.org/wiki/Glob_%28programming%29) 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). – djf Aug 15 '12 at 21:27
  • 1
    @djf: `chown -R user:group .` – Shi Aug 16 '12 at 20:05

5 Answers5

140

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.

Darth Android
  • 37,872
  • 5
  • 94
  • 112
  • 3
    @Andrew Be on your guard though, he can be both friend and foe to the weary or unprepared. – Darth Android Aug 15 '12 at 21:07
  • 12
    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. – teynon Apr 09 '16 at 19:34
  • @Tom That's why I should probably start using `-v` on all my recursive commands, but too lazy – Kellen Stuart Dec 30 '16 at 19:44
  • 2
    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` – Dave Everitt Aug 11 '19 at 12:26
  • 1
    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 ./.*` – iopq Jan 29 '23 at 07:54
  • chown scott:scott * chown: invalid option -- ' ' ??? – Scott Hather Aug 21 '23 at 09:29
14

I think you want this:

chown username:groupname *

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

chown -R username:groupname *

djf
  • 386
  • 3
  • 10
0

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.

Examples:

% mkmeowner .

% mkmeowner dirpath1 dirpath2
  • It requires the following script mkmeownerone to be in your path.

mkmeowner:

#!/bin/bash
[ "x$1" == "x-h" ] || [ "x$1" == "x--help" ] && cat << END && exit 0
Usage: $0 dirorfile [direorfile2 ...]:
    change ownership of directories or files to current user.
    Current user must have permissions to read those and write to owner directory.
END
mkmeownerone=`which mkmeownerone`
for d in $*; do
    find "$d" -not -user `whoami` -exec $mkmeownerone {} \;
done

mkmeownerone:

#!/bin/bash
# change ownership of one file or directory
f="$1"
expr match "${f}" '.*\.mkmeowner$' > /dev/null && exit 1 # already tried to do this one
if mv -f "$f" "${f}.mkmeowner"; then
    cp -pr "${f}.mkmeowner" "$f" && rm -rf "${f}.mkmeowner"
    exit 0
fi
exit 1
sbcondor
  • 11
  • 1
  • 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 `${f}` isn’t a useful alternative; see [this](https://unix.stackexchange.com/q/32210/23408#286525 "${variable_name} doesn’t mean what you think it does …").  … (Cont’d) – Scott - Слава Україні Mar 15 '19 at 16:24
  • (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 `${f}` ends with `mkmeowner`, not whether it ends with `.mkmeowner`.  Therefore, the script will not work on itself.  … (Cont’d) – Scott - Слава Україні Mar 15 '19 at 16:24
  • (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`). – Scott - Слава Україні Mar 15 '19 at 16:24
  • 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! – sbcondor Mar 15 '19 at 17:16
  • I don't think there is anything wrong with ${f}, 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 ${f}.mkmeowner, and I don't have to remember whether $f.heck means ${f}+heck or ${f.heck} – sbcondor Mar 15 '19 at 17:32
  • (3) Did you read the link?  If `f` contains a space (e.g., `fat cat`), then `"$f"` (with quotes) is `fat cat`, but `${f}` (without quotes) is two separate words: `fat` and `cat`.  No, the braces don’t hurt, but they leave you unprotected.   (7) If you dislike underscores, you can use CamelCase (`MkMeOwner`) or snakeCase (`mkMeOwner`).    :-)    ⁠ – Scott - Слава Україні Mar 15 '19 at 17:49
  • Well, my point #4 was that, if `bob-dir` contains `bob-file` (both owned by ``bob``), then the script will `mv -f bob-dir bob-dir.mkmeowner` and `cp -pr bob-dir.mkmeowner bob-dir` (which will copy `bob-dir.mkmeowner/bob-file` to `bob-dir/bob-file`, making it owned by you) and then (at least potentially) ***also*** do `mv -f bob-file bob-file.mkmeowner` and `cp -pr bob-file.mkmeowner bob-file` (in the newly created `bob-dir` directory).  I’ll admit, it’s not obvious to me how to fix that. – Scott - Слава Україні Mar 15 '19 at 17:49
  • Actually I don't like shift keys... – sbcondor Mar 15 '19 at 18:04
  • #4. huh. ya. thinking. – sbcondor Mar 15 '19 at 18:05
0

I needed to change the owner of the current directory too. None of the above chown . and chown * worked. I had to pass in the full directory path:

$ chown -R nobody: "$PWD"

In the above nobody is the user I need. Group is omitted because it's optional when it equals the user.

Alex
  • 181
  • 2
  • 6
0

Another way to change username:groupname of all files in the current directory is to work with a subshell:

chown username:groupname $(find . -name "[a-zA-Z0-9]*")

The bash built-in command find is used with a regular-expression [a-zA-Z0-9]* in a subshell $() which can form the basis of more complex administrative tasks.

Destroy666
  • 5,299
  • 7
  • 16
  • 35