Files and folders
Learn how to manage files and folders.
Folders (directories)
Navigation
pwd
: print working directorycd <path>
: change directory- Use
../
to navigate upwards / to parent directory - Use
/<...>
for an absolute path (relative to root folder) - Use
~/<...>
for a path relative to the current user home
- Use
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
/bin
: Essential user command binaries- e. g.
cat
,chmod
,echo
, etc.
- e. g.
/boot
: Static files of the bootloader/dev
: Device files/dev/null
: “black hole”/dev/zero
: infinite stream of zeroes/dev/(u)random
: infinite stream of random bytes
/etc
: Editable text configuration / config files/home
: User home directories/home/tux
: would betux
’s home folder
/lib
: Essential shared libraries and kernel modules/media
: Mount point for removable media/mnt
: Mount point for temporarily mounted filesystems/opt
: Add-on application software packages/proc
: Virtual filesystems documenting kernel and process status as text files/root
: Home directory for the root user/run
: Run-time variable data/sbin
: System binaries/srv
: Data for services provided by this system/sys
: Information about devices, drivers and some kernel features/tmp
: Temporary files; often cleared on reboot/usr
: Read-only user data; (Multi-)User utilities and applications; “User”/”Unix System Resources”/usr/bin
: user commands/usr/include
: header files, included by programs/usr/lib
: libraries/usr/local
: programs which are independent of the distro/usr/local/bin
/var
: Variable files/var/cache
: Application cache data/var/log
: Logs/var/run
: Link to/run
/var/tmp
: Temporary files preserved between reboots
Information
ls [folder]
: list the items in the currect or specified folderls -l [folder]
: same, but with detailsls -ld [folder]
: get infos about the current or specified folder
(Image source: www.ics.uci.edu - check out file permissions to understand the permission bits)
Manipulation
mkdir <path>
: create a directoryrmdir <path>
: delete a directory
Files
In Linux, a lot of things are files / file handles. This includes devices, pipes, sockets and symlinks.
Manipulation
touch <path>
: create an empty filerm <path>
: delete a filecp <path_old> <path_new>
: copy a file/dirscp <username@hostname>:<path_source> <path_target>
: copy file from remote to localmv <path_old> <path_new>
: move/rename a file/dir
Editing
nano [+<line>] <path>
: edit (and create) a file with the nano editorCtrl+o ENTER Ctrl+x
to save and exit
vi <path>
: edit (and create) a file with the vi(m) editorI
to enter insert modeEsc
to exit insert mode / enter command mode:wq
to save and exit:q!
to exit without save
Reading
cat <path>
: print a file to consoleless <path>
: read file in scrollable viewer (scroll up and down)more <path>
: read file in scrollable viewer (scroll only down)tail <path>
: print the end of a file-n <N>
: print the last N lines-f
: follow/watch the file for changes and print them continuously
b cat <path>
: print an image to console with the butterfly launcher (if installed)
Information
wc <file>
: get word countwc -l <file>
: get line count
Finding
find <folder> <search_params>
: Searches the file system-empty
: Only empty files-name '*searchstring*'
: Search by name-user USERNAME
: Owned by user-type X
: Filter by type (f
= regular file,l
= link,d
= directory,p
= pipe,s
= socket)-exec CMD {} \;
: Run command for every item found (\;
marks end of-exec
section,{}
the path placeholder)
grep searchstring /var/myfirstfile
: Searches for the pattern searchstring from the contents of /var/myfirstfile.-i
: ignore case-R
: read all files under directories recursiveley and follow symbolic links-n
: print line number
Archives
tar -czvf <filename>.tar.gz /var/myfirstdirectory
: compress a directory into an archivetar -xzvf <filename>.tar.gz
: extract the archivezip [options] <target.zip> <source-files>
: zip files-r
: recursively zip directory
unzip [options] <file>
: unzip a file-j
: into current directory
Permissions
Every file/folder is owned by a user and a group.
File/Folder permissions
- Permission groups:
u
ser,g
roup ando
thers. - Permission types:
r
ead,w
rite and ex
ecute.
Example permissions: -rwxrwxrwx+
The first positions stands for file (-
), folder (d
) or link (l
).
The three rwx
triplets stand for the three permission
groups u
ser, g
roup and o
thers.
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
find / -perm /4000 2>/dev/null
: find files with setUID setfind / -perm /2000 2>/dev/null
: find files with setGID setfind / -perm /1000 2>/dev/null
: find files with sticky bit 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.
- Example 1:
rwxr-xr-x
= 755 - Example 2:
rwxr-x---
= 750
Special permissions are represented by one octal digit before the others.
Numeric values:
Group | (Special perms) | User | Group | Others | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Perm | setUID | setGID | sticky bit | r | w | x | r | w | x | r | w | x |
Value | 4000 | 2000 | 1000 | 400 | 200 | 100 | 40 | 20 | 10 | 4 | 2 | 1 |
Change file permissions
File permissions can be changed via chmod <perm/num> <path>
command.
chmod +x <path>
: add execute permission to all three groupschmod g+w <path>
: add write permission to the owner groupchmod o=r <path>
: only allow others to readchmod o-wx <path>
: remove write and execute permissions from otherschmod -w <path>
: make the file read-onlychmod u+s <path>
: set setUID bitchmod g+s <path>
: set setGID bitchmod +t <path>
: set sticky bitchmod NUM <path>
: set numeric permissions
(no letter before the symbol or a
stands for all of them)
File ownership
chown <username> <path>
: transfer user ownershipchown <username>:<group> <path>
: transfer user and group ownershipchown :<group> <path>
: transfer group ownershipchgrp <group> <path>
: transfer group 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.
- File permissions = 0777 - umask
- Folder permissions = 0666 - umask
The umask can be set via umask
command:
umask <num>
ACL (Access Control Lists)
ACLs can be used to allow more users to access a file or folder.
getfacl -a -e <file/folder>
setfacl -m PERMS <file/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.
[u:]USERNAME:PERMISSIONS,g:GROUPNAME:PERMISSIONS,...
u:uStudent3:rwx,g:gClass2:rx
g:gClass2:r-x
tux:-
ACLs are applied from top to bottom. The first matching rule will be applied!