Skip to content

Files

Glossary

squashfs

Squashfs is a compressed read-only filesystem for Linux using zlib compression for files, inodes, and directories.

SGID

When the set-group-ID bit for a directory is set, all files created therein are assigned to the directory's group and not to the file owner's default group.

This is intended to facilitate file sharing. In this scenario, users are assigned to a group, and the group is assigned to shared directories with the SGID bit set.

Sticky bit

When the sticky bit is set on a directory, only root, the directory owner and the owner of a file can remove files in that directory.

SUID

The set-user-ID bit allows a file to be executed with the privileges of the file's owner.

Commands

chage

chage

Expire password in 30 days
chage -E $(date -d +30days +%Y-%m-%d) $USER

chgrp

chgrp

Change ownership of $FILE to $USER and $GROUP

chgrp $USER:$GROUP $FILE

chmod

chmod

chmod +t $FILE # Sticky bit
chmod g+s file # SGID
chmod u+s file # SUID
chmod -t $FILE # Sticky bit
chmod g-s file # SGID
chmod u-s file # SUID

chown

chown

Change a file or directory's ownership.

To change the user and group owner of a file to $USER and $GROUP:

chown $USER:$GROUP $file

Recursively grant $USER ownership to $PATH

chown -R $USER $PATH

Use a reference file to match the configuration of a particular file

chown -vR --reference=. $PATH

--preserve-root prevents changes to files in the root directory but only when used together with --recursive

chown -cfR --preserve-root $USER 

cp

du

du

du does not double-count hard-linked files, so it can be used to analyze deduplication in app distribution solutions like Flatpak.

Here the second command will display a smaller value for the 21.08 version of the freedesktop Platform runtime, indicating that hard-linked files have not been double-counted.

du -sh /var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/21.08
du -sh /var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/21.08 /var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/20.08

find

find

Search for files in a directory hierarchy

Find all files owned by user
find . -user $USER

-exec allows a command to be executed for every foudn file, which has to be terminated with an escaped semicolon, i.e. \;.

Remove whitespace from filenames
find . -type f -name "* *" -exec bash -c 'mv "$0" "${0// /_}"' {} \;

Find recently modified files/folders

There are 3 timestamps associated with files in Linux

  • atime "access time": last time file was accessed by a command or application
  • mtime "modify time": last time file's contents were modified
  • ctime "change time": last time file's attribute was modified

Numerical arguments can be specified in 3 ways:

  • +n greater than {n} days ago
  • -n less than {n} days ago
  • n exactly n days ago
# Find only files that were modified more than 120 days ago
find . -type f -mtime +120 -ls

# Modified less than 15 days ago 
find . -type f -mtime -15 -ls

# Modified exactly 10 days ago 
find . -type f -mtime 10 -ls 

# Find files modified over the past day
find . -type f -newermt "1 day ago" -ls
find . -type f -newermt "-24 hours" -ls
find . -type f -newermt "yesterday" -ls

# Find files created today
find . -type f -ctime -1 -ls 

mv

rename

rename

Use regular expressions to rename multiple files

# Renaming file.old to file.new
rename 's/old/new/' this.old

# Use globbing to rename all matching files
rename 's/old/new/' *.old
rename 's/report/review/' *

# Change all uppercase letters to lowercase
rename 'y/A-Z/a-z/' *

rsync

rsync

  a b     e   g         l     o p   r   t   v       z

Copy $FILE locally

rsync -zvr $FILE $PATH

Copy $FILE to $PATH on remote $HOST

rsync $FILE $HOST:$PATH

Copy $FILE from $HOST to local $PATH

rsync $HOST:$FILE $PATH

Copy $DIR recursively

rsync -zvr $DIR $PATH
rsync -avz $DIR $PATH

Copy to remote systems over SSH

rsync -zvre ssh $DIR $HOST:$REMOTEPATH
rsync -avze ssh $DIR $HOST:$REMOTEPATH

Synchronize only specific file type

rsync -zvre ssh --include '*.php' --exclude '*' $PATH

facl

getfacl

setfacl

setfacl

The effect of ACLs can be illustrated with a web server. This command removes read access from a file which would otherwise be served by the Apache/httpd web server daemon.

setfacl -m u:apache:- /var/www/html/index.html

This can be resolved by granting read to the apache service account (or removing the entry altogether)

setfacl -m u:apache:r /var/www/html/index.html
setfacl -x u:apache /var/www/html/index.html
setfacl -b /var/www/html/index.html

attr

A family of commands exists to change file attributes on Linux file systems.

lsattr

chattr

Make file immutable
chattr +i /etc/resolv.conf