Bird
Raised Fist0
Linux CLIscripting~20 mins

chgrp (change group) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
chgrp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
πŸ’» Command Output
intermediate
2:00remaining
What is the output of this chgrp command?
You have a file named report.txt owned by user alice and group staff. You run the command chgrp developers report.txt. What will be the group of report.txt after this command?
AThe group changes to <code>developers</code>.
BThe group remains <code>staff</code>.
CThe command outputs an error: group 'developers' does not exist.
DThe command outputs an error: permission denied.
Attempts:
2 left
πŸ’‘ Hint
Think about what chgrp does and whether the group exists.
πŸ’» Command Output
intermediate
2:00remaining
What error does this chgrp command produce?
You run the command chgrp admin secret.txt but you are not the owner of secret.txt and not root. What error message will you most likely see?
Achgrp: cannot find group 'admin'
Bchgrp: changing group of 'secret.txt': Operation not permitted
Cchgrp: file 'secret.txt' does not exist
Dchgrp: invalid option -- 'a'
Attempts:
2 left
πŸ’‘ Hint
Think about permission requirements for changing group ownership.
πŸ“ Syntax
advanced
2:00remaining
Which chgrp command correctly changes group recursively?
You want to change the group of the directory project and all its files and subdirectories to team. Which command is correct?
Achgrp --recursive team project
Bchgrp -R team project
Cchgrp -r team project
Dchgrp team -R project
Attempts:
2 left
πŸ’‘ Hint
Check the correct option for recursive change in chgrp.
πŸš€ Application
advanced
2:00remaining
How to change group of multiple files using chgrp?
You have files file1.txt, file2.txt, and file3.txt. You want to change their group to editors in one command. Which command will do this?
Achgrp editors file1.txt; chgrp editors file2.txt; chgrp editors file3.txt
Bchgrp editors *.txt
Cchgrp editors file?.txt
Dchgrp editors file1.txt file2.txt file3.txt
Attempts:
2 left
πŸ’‘ Hint
Think about how chgrp accepts multiple files as arguments.
🧠 Conceptual
expert
2:00remaining
What happens if you run chgrp with a non-existent group?
You run chgrp ghostgroup file.txt where ghostgroup does not exist on the system. What will happen?
AThe command silently ignores the group and leaves the file unchanged
BThe command changes the group to a default group
CThe command fails with: chgrp: invalid group: β€˜ghostgroup’
DThe command changes the group to the current user's primary group
Attempts:
2 left
πŸ’‘ Hint
Think about how chgrp validates group names.

Practice

(1/5)
1. What does the chgrp command do in Linux?
easy
A. Changes the file name
B. Changes the user ownership of a file or directory
C. Changes the permissions of a file or directory
D. Changes the group ownership of a file or directory

Solution

  1. Step 1: Understand the purpose of chgrp

    The chgrp command is specifically designed to change the group ownership of files or directories.
  2. Step 2: Differentiate from similar commands

    Changing user ownership is done by chown, and changing permissions is done by chmod. Renaming files uses mv.
  3. Final Answer:

    Changes the group ownership of a file or directory -> Option D
  4. Quick Check:

    chgrp changes group ownership [OK]
Hint: Remember: chgrp = change group ownership [OK]
Common Mistakes:
  • Confusing chgrp with chown (user ownership)
  • Thinking chgrp changes permissions
  • Mixing up chgrp with mv (rename)
2. Which of the following is the correct syntax to change the group of a file named report.txt to staff?
easy
A. chgrp report.txt staff
B. chgrp staff report.txt
C. chgrp -g staff report.txt
D. chgrp -u staff report.txt

Solution

  1. Step 1: Recall the basic syntax of chgrp

    The correct syntax is chgrp [group] [file]. So the group name comes first, then the file name.
  2. Step 2: Analyze each option

    chgrp staff report.txt matches the correct syntax. chgrp report.txt staff reverses the order. chgrp -g staff report.txt uses an invalid flag -g. chgrp -u staff report.txt uses -u which is not valid for chgrp.
  3. Final Answer:

    chgrp staff report.txt -> Option B
  4. Quick Check:

    Syntax: chgrp group file [OK]
Hint: Group name first, then file name in chgrp [OK]
Common Mistakes:
  • Swapping group and file arguments
  • Using invalid flags like -g or -u
  • Confusing chgrp syntax with chown
3. What will be the output or effect of the command:
sudo chgrp -R developers /home/project?
medium
A. Change the group ownership of /home/project and all its contents to developers
B. Change the user ownership of /home/project to developers
C. Change the group ownership of /home/project only, not its contents
D. Show an error because -R is invalid with chgrp

Solution

  1. Step 1: Understand the -R option

    The -R flag means recursive, so it applies the group change to the directory and all files and subdirectories inside it.
  2. Step 2: Understand the use of sudo

    sudo is used to run the command with root privileges, which is often needed to change group ownership on files you don't own.
  3. Final Answer:

    Change the group ownership of /home/project and all its contents to developers -> Option A
  4. Quick Check:

    chgrp -R changes group recursively [OK]
Hint: Use -R to change group recursively on directories [OK]
Common Mistakes:
  • Thinking -R only changes the directory itself
  • Confusing group change with user change
  • Assuming -R is invalid for chgrp
4. You run chgrp staff report.txt but get the error: chgrp: changing group of 'report.txt': Operation not permitted. What is the most likely fix?
medium
A. Use chmod instead of chgrp
B. Change the file name to staff
C. Run the command with sudo to get permission
D. Restart the system and try again

Solution

  1. Step 1: Understand the error message

    The error means you don't have permission to change the group ownership of the file.
  2. Step 2: Use elevated privileges

    Using sudo runs the command as root, which usually has permission to change group ownership.
  3. Final Answer:

    Run the command with sudo to get permission -> Option C
  4. Quick Check:

    Permission errors need sudo [OK]
Hint: Use sudo if permission denied on chgrp [OK]
Common Mistakes:
  • Trying to rename file instead of changing group
  • Using chmod which changes permissions, not group
  • Restarting system won't fix permission issues
5. You want to change the group ownership of all files with extension .log inside /var/logs and its subdirectories to adm. Which command will do this correctly?
hard
A. sudo find /var/logs -type f -name '*.log' -exec chgrp adm {} +
B. sudo chgrp -R adm /var/logs/*.log
C. chgrp adm /var/logs/*.log
D. sudo chgrp adm /var/logs -name '*.log'

Solution

  1. Step 1: Understand the requirement

    You need to change group ownership of all .log files inside /var/logs and all its subdirectories.
  2. Step 2: Analyze each option

    sudo find /var/logs -type f -name '*.log' -exec chgrp adm {} + uses find to locate all .log files recursively and runs chgrp adm on them, which is correct.
    sudo chgrp -R adm /var/logs/*.log tries to use chgrp -R on a wildcard which only expands in the current directory, not recursively.
    chgrp adm /var/logs/*.log lacks sudo and is not recursive.
    sudo chgrp adm /var/logs -name '*.log' uses invalid syntax with -name directly on chgrp.
  3. Final Answer:

    sudo find /var/logs -type f -name '*.log' -exec chgrp adm {} + -> Option A
  4. Quick Check:

    Use find with exec for recursive selective chgrp [OK]
Hint: Use find with exec to change group on specific files recursively [OK]
Common Mistakes:
  • Using chgrp -R with wildcards which is not recursive
  • Forgetting sudo for permission
  • Trying to use -name option with chgrp
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard