Linux :: Files

Files

Linux File Structure

man 7 hier # traditional file structure
man 7 file-hierarchy    # systemd

/ - The root directory of the entire filesystem hierarchy, everything is nestled under this directory.
/bin - Essential ready-to-run programs (binaries), includes the most basic commands such as ls and cp.
/boot - Contains kernel boot loader files.
/dev - Device files.
/etc - Core system configuration directory, should hold only configuration files and not any binaries.
/home - Personal directories for users, holds your documents, files, settings, etc.
/lib - Holds library files that binaries can use.
/media - Used as an attachment point for removable media like USB drives.
/mnt - Temporarily mounted filesystems.
/opt - Optional application software packages.
/proc - Information about currently running processes.
/root - The root user's home directory.
/run - Information about the running system since the last boot.
/sbin - Contains essential system binaries, usually can only be ran by root.
/srv - Site-specific data which are served by the system.
/tmp - Storage for temporary files
/usr - This is unfortunately named, most often it does not contain user files in the sense of a home folder.
    This is meant for user installed software and utilities, however that is not to say you can't add personal directories in there.
    Inside this directory are sub-directories for /usr/bin, /usr/local, etc.
/var - Variable directory, it's used for system logging, user tracking, caches, etc.
    Basically anything that is subject to change all the time.

file

echo "string" > /path/to/file # create or overwrite existing file
cat /path/to/file1 >> /path/to/file2 # append file1 to file 2
STDIN 0
STDOUT 1
STDERR 2
2>&1 # output error onto standard output 1
&> # short for 2>&1
file name.ext # identify type of file
head -n 15 /path/to/file # first 15 lines
tail -n 10 /path/to/file
tail -f /path/to/file # follow a file

ln -s vs. ln # sim link vs. hardlink

pipe and tee

ls | tee results.txt # print out to STDOUT and both the file
tee -a file # append rather than overwrite
echo "text" | sudo tee -a file # append a line to a file requiring root permission

xargs command # receive STDIN as arguments
echo /etc | xargs ls  # pass arguments to commands that do not accept STDIN
find . -type d | xargs rm # remove all dirs
# find files named core, processing filenames containing whitespace or newlines will be handled by -0 (NUL)
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
# Generate a compact listing of all users (in a horizontal line rather than vertical list)
cut -d: -f1 < /etc/passwd | sort | xargs echo
grep -v "string" # exclude string
find . -name ".DS_Store" -delete # find all .DS_Store in current and subfolders and delete
find . -empty -type d -delete # find all empty dirs and delete
find . -maxdepth 1 -type f -name test ! -name exclude_name -printf '%P\n'
find . -maxdepth 1 ! -path . -type d    # exclude .
diff -qr dir1 dir2 # recurssively find differences in dir1 and dir2
awk -F: '/Keyword/ { print $1 }' file
sed # edit file

permissions

chmod ug+x /path/to/file
    u: user
    g: group
    o: other
chomd 755 /path/to/file
    4: read permission
    2: write permission
    1: execute permission

umask # get current settin, per session
umask 022 # use 777-022=755 which is default permission
chown -R usr /path/to/dir # recursive
chown usr:grp /path/to/file # change user and group at the same time
chgrp usr /path/to/dir

# env
$USER
$UID
$GID  # group id
id -gn  # find primary group name

users

useradd -d homedir -g groupname -m -s shell -u usrid accountname
passwd usrid
usermod
userdel -r usrid # remove directory
groupadd
groupmod
groupdel

borg

borg init --encryption=none/repokey/keyfile /path/to/repo #repokey stores key in repo; keyfile stores in $HOME folder
borg create --compression lz4/zstd/zlib/lzma,0-9 /path/to/repo::archiveName /source1 /source2 #lz4 fastest
borg list /path/to/repo
borg list /path/to/repo::archive1
borg mount /path/to/repo::archive1 /mnt/tmp # need llfuse
borg umount /mnt/tmp
borg extract /path/to/repo::arvhive1
borg diff /path/to/repo::archive1 archive2
borg extract usr@server:/path/to/repo /dir
borg prune
--verbose
--progress

btrfs

# all options can be shortened as long as it's unambiguous
btrfs dev scan
btrfs fi df
btrfs sub show

## format
# -L label; -m metadata -d data -f force
mkfs.btrfs -L label /dev/sdaX
mkfs.btrfs -m raid1 -d raid1 /dev/sdb /dev/sdc
mkfs.btrfs -d raid0 /dev/sdb /dev/sdc
mkfs.btrfs -d single /dev/sdb /dev/sdc

## mount
mount -o compress=zstd:3,subvol=@home,suvolid=266 /dev/sdaX /home # mount any one if multiple devices

## check status
btrfs device scan [/dev/sdb]
btrfs device stat /path/to/disk

## usage
btrfs filesystem df /path
btrfs filesystem usage /path
btrfs fi du -s /path # -s summary
btrfs filesystem show
btrfs filesystem defragment /path

## property
btrfs property set -ts /path/to/snapshot ro false # -ts subvolume set/get/list
btrfs property get -tf / label # get the label for the filesystem
btrfs property list -tf / # get the list of properties so to get or set

Subvolume and snapshot

btrfs subvolume create /path/to/@subvol
btrfs subvolume list /path
btrfs subvolume show /path/to/@subvol
btrfs subvolume delete /path/to/@subvol
btrfs suvolume snapshot -r /path/to/@subvol /path/to/@snapshot-$(date +%Y%m%d) # -r readonly


toplevel         (volume root directory, not to be mounted by default)
  +-- @root       (subvolume root directory, to be mounted at /)
  +-- @home       (subvolume root directory, to be mounted at /home)
  +-- @snapshots  (subvolume root directory, to be mounted at /snapshots)

## make snapshots
btrfs subvolume snapshot -r / /snapshots/root/20221008-040001/
btrfs subvolume snapshot -r /home /snapshots/home/20221008-050001
toplevel    (volume root dir, not mounted)
    +-- @root   (mounted at /)
    +-- @home   (mounted at /home)
    /snapshots  (dir)
        +-- root (dir)
            +-- 20221008-040001     (root dir of snapshot of subvol @root)
        +-- home (dir)
            +-- 20221008-050001     (root dir of snapshot of subvol @home)

## restore a snapshot of home
### mount top level
mount -o compress=zstd /dev/sda /mnt
### rename current @home
mv /mnt/@home /mnt/@home-tmp
### move snapshot to @home if the snapshot is rw
mv /mnt/@snapshots/home/20221008-050001 /mnt/@home
### alternatively, make another snapshot
btrfs sub snapshot /mnt/@snapshots/home/20221008-050001 /mnt/@home
### reboot to verify everything works
btrfs sub del @home-tmp

## restore a snapshot of root filesystem
mount -o compress=zstd /dev/sda /mnt
mv /mnt/@root /mnt/@root-tmp
mv /mnt/@snapshots/root/20221008-050001 /mnt/@root  # or make another snapshot
# reboot
btrfs subdel @root-tmp
## send/receive
### full backup
sudo btrfs send /.snapshot/back1 | sudo btrfs receive /path/to/back

### incremental
sudo btrfs send -p /.snapshot/back1 /.snapshot/back2 | sudo btrfs receive /path/to/back