linux-cheatsheet

A cheatsheet with common linux commands and tools

View on GitHub

Files and folders

Learn how to manage files and folders.

Folders (directories)

Always wrap path names with spaces in quotes. (e. g. "/users/pi/my folder/ex.txt")

Hierarchy

The file system hierarchy is not identical on all distributions but thanks to the Filesystem Hierarchy Standard (FHS), there is a typical structure found on almost all distributions.

Current version is FHS 3.0 since 2015.

/ is the root folder and represents the start of the hierarchy

Information

File permissions

(Image source: www.ics.uci.edu - check out file permissions to understand the permission bits)

Manipulation

Files

In Linux, a lot of things are files / file handles. This includes devices, pipes, sockets and symlinks.

Manipulation

Editing

Reading

Information

Finding

Archives

Permissions

Every file/folder is owned by a user and a group.

File/Folder permissions

Example permissions: -rwxrwxrwx+

The first positions stands for file (-), folder (d) or link (l). The three rwx triplets stand for the three permission groups user, group and others.

If a + is present at the end, it means that an ACL (Access Control List) is active.

Special permissions

More on this topic: here (external link)

SetUID & SetGID

Files that have the setUID or setGID bits set will be executed with the permissions of the owner (user/group).

Files created in folders that have the setUID or setGID bits set will not belong to the creator but to the owner (user/group) of the parent folder.

In ls -l, this is displayed in place of the x bit of the user/group (first/second rwx triplet): s = with execute permission; S = without execute permission

Sticky bit

The sticky bit shall only be applied to folders. If it is set, only the owner of a file in the folder can delete or rename it.

In ls -l, this is displayed in place of the x bit of others (third rwx triplet): t = with execute permission; T = without execute permission

Finding files with special permissions set

Numeric permissions

Permissions can also be represented using the octal (base 8) system.

Each of the three wxr triplets (owner user, owner group, others) is represented as one octal integer. w stands for 4, x for 2 and r for 1.

Special permissions are represented by one octal digit before the others.

Numeric values:

Group (Special perms) User Group Others
Perm setUIDsetGIDsticky bit rwx rwx rwx
Value 400020001000 400200100 402010 421

Change file permissions

File permissions can be changed via chmod <perm/num> <path> command.

(no letter before the symbol or a stands for all of them)

File ownership

Umask setting

The umask setting defines, what permissions are removed by default on file/folder creation. Special permissions cannot be changed by the umask setting.

The umask can be set via umask command:

ACL (Access Control Lists)

ACLs can be used to allow more users to access a file or folder.

PERMS is a list of entries separated by a comma. Every entry consists of type, name and permissions. If no type is specified, u for user is assumed.

ACLs are applied from top to bottom. The first matching rule will be applied!



This cheatsheet was created by Rafael Urben. Found a mistake? Feel free to create a PR or open an issue on GitHub!