CompTIA A+ Core 2
Linux chmod, chown, cp, rm and mv explained for A+ Core 2
chmod sets read, write and execute permissions for the owner, the group and everyone else, either as octal digits (4 read, 2 write, 1 execute) or as symbols such as u+x. chown changes a file's owner, group or both. cp and rm need -r to work on a folder and its contents, and mv renames or moves without leaving a copy.
chmod: octal and symbolic modes
Each permission digit is the sum of read (4), write (2) and execute (1). In a three-digit mode the digits are, in order, the owning user, the file's group, and all other users.
Worked example: chmod 755 backup.sh gives the owner 7 (4+2+1, read, write and execute), the group 5 (4+1, read and execute) and others 5 (read and execute). chmod 640 gives the owner read and write, the group read only, and others nothing.
Symbolic modes use u (owner), g (group), o (others) and a (all), with + to add bits, - to remove them, and = to set exactly those bits and remove the rest. chmod u+x backup.sh adds execute for the owner only and leaves group and others as they were. chmod a+x would add execute for everyone.
chown: owner and group
chown maria file makes maria the owner and leaves the group unchanged.
chown maria:design file changes the owner to maria and the group to design. chown maria: file makes maria the owner and sets the group to maria's login group.
chown :design file changes only the group and keeps the current owner. In this form chown does the same job as chgrp. Add -R to apply the change to a folder's contents.
cp, rm and mv
cp copies files. To copy a folder with its subfolders, add -r (or -R, --recursive). -i prompts before overwriting.
rm removes files but, by default, not directories. rm -r oldbuild removes the folder and everything in it. Running it with sudo does not change this; the -r option does.
The rm manual warns that a removed file's contents might still be recoverable with enough expertise or time, and points to shred for greater assurance. rm alone is not a way to sanitize sensitive data.
mv report.txt report-old.txt renames a file in place. mv with a directory as the target moves the files into it. No copy is left behind.
fsck
fsck checks and optionally repairs one or more Linux filesystems. The filesystem can be named by device, such as /dev/sdb2, by mount point, by label or by UUID.
Where the exam catches people
- Believing the middle digit of chmod 755 is for other users: the order is owner, group, others, so the group gets 5, read and execute.
- Believing chmod a+x only affects the owner: a means all; u+x is the owner-only change.
- Believing chown design file changes the group: a name with no colon sets the owner, and :design is needed to change only the group.
- Believing rm on a folder fails for lack of root rights: rm skips directories by default and needs -r.
- Believing rm makes deleted data unrecoverable: the manual says contents may still be recoverable and suggests shred.