Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 4. User, Group and Permission

chown (Change Owner of File and Directory)

chown (Change Owner of File and Directory)

Changing File and Directory Owners with chown Command

The chown (CHange OWNer) command is used to change the owner of files or directories. With the -R option, you can change the owner of all files and directories under the specified directory.

To demonstrate the chown command, we'll create some directories and a file under the user_a home directory under superuser privileges.

Command Line - INPUT
cd /home/user_a
mkdir -p dir_a1/dir_a2
mkdir -p dir_b1/dir_b2
touch file_a

By running the commands above, you can create the following directory tree under the user_a's home directory.

user_a
├── dir_a1
│   └── dir_a2
├── dir_b1
│   └── dir_b2
└── file_a

Check owner statuses

As the directories and the file are created by the superuser, the owner and owner group of the directories and the file are set as root.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 8
drwxr-xr-x 3 root root 4096 Jan  2 15:31 dir_a1
drwxr-xr-x 3 root root 4096 Jan  2 15:31 dir_b1
-rw-r--r-- 1 root root    0 Jan  2 15:31 file_a
Command Line - INPUT
ls -l dir_a1
Command Line - RESPONSE
total 4
drwxr-xr-x 2 root root 4096 Jan  2 15:31 dir_a2
Command Line - INPUT
ls -l dir_b1
Command Line - RESPONSE
total 4
drwxr-xr-x 2 root root 4096 Jan  2 15:31 dir_b2

Change the owner of a file or directory

To change the owner of a single file or directory, you can simply run the chown command without any options. For example, to change the owner of file_a to user_a, run the following command.

Command Line - INPUT
chown user_a file_a

Check the owner status of file_ a by running the ls -l command. You can see that the file owner has changed to user_a.

Command Line - INPUT
ls -l file_a
Command Line - RESPONSE
-rw-r--r-- 1 user_a root 0 Jan  2 15:31 file_a

Change the owner of an entire directory recursively

To change the owner of an entire directory including sub-directory and files underneath, you can use the -R option. For example, to change the owner of the directory tree of dir_a1 to usr_a, run the following command.

Command Line - INPUT
chown -R user_a dir_a1

Check the owner status of dir_ a1 and dir_a2 by running the ls -l command. You can see that both directories' owners have changed to user_a.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 8
drwxr-xr-x 3 user_a root 4096 Jan  2 15:31 dir_a1
drwxr-xr-x 3 root   root 4096 Jan  2 15:31 dir_b1
-rw-r--r-- 1 user_a root    0 Jan  2 15:31 file_a
Command Line - INPUT
ls -l dir_a1
Command Line - RESPONSE
total 4
drwxr-xr-x 2 user_a root 4096 Jan  2 15:31 dir_a2

Change the owner and group owner of an entire directory recursively

To change the owner and owner group at the same time, you can use the following syntax (add the group name followed by a user name with : (colon).

Change the owner and group owner of an entire directory recursively

For example, to change the owner and owner group of the directory tree of dir_b1 to user_a, run the following command.

Command Line - INPUT
chown -R user_a:user_a dir_b1

Check the owner status of dir_b1 and dir_b2 by running the ls -l command. You can see that both directories' owners and owner groups have changed to user_a.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 8
drwxr-xr-x 3 user_a root   4096 Jan  2 15:31 dir_a1
drwxr-xr-x 3 user_a user_a 4096 Jan  2 15:31 dir_b1
-rw-r--r-- 1 user_a root      0 Jan  2 15:31 file_a
Command Line - INPUT
ls -l dir_b1
Command Line - RESPONSE
total 4
drwxr-xr-x 2 user_a user_a 4096 Jan  2 15:31 dir_b2

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Introduction to Web Servers in Linux

Web Server

Chapter 6. Docker Compose and Deployment Practice

Chapter 6. Docker Compose and Deployment Practice

Managing User Passwords in Linux

passwd (Set Password)

Setting Up Gunicorn as Your Django Application Server

Application Server Setup – Gunicorn

Decision Making in Django App Deployment

Overview of Django App Deployment (2)

Introduction to Web Servers in Linux

Web Server

Chapter 6. Docker Compose and Deployment Practice

Chapter 6. Docker Compose and Deployment Practice

Managing User Passwords in Linux

passwd (Set Password)

Setting Up Gunicorn as Your Django Application Server

Application Server Setup – Gunicorn

Decision Making in Django App Deployment

Overview of Django App Deployment (2)

Tags:

Access Mode

Permission

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server