Finding files with find

Remove files by modification date

This example shows how to remove all files with a modification date before 2016:

find -type f -not -newermt 20160101 | xargs rm

Move old files to another directory

This example shows how to move a large number of files to another directory:


find /var/www/userpics -name '*.jpg' -mtime +31 -print0 | xargs --null mv --target-directory=/backup/oldpics/

Find files by permission

Exact permission (read,write,execute for owner only):

find -perm 700

Find large files

$ du -sh -t 20M $(find /tmp/ -type f)
30M    /tmp/OF0dXjBgX3/0207.zip
56M    /tmp/lYZNO1PD09/0703.zip
29M    /tmp/aF_jPhgvwD/0307.zip

Use cat

Show file contents:

$ /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

This file should be present on any modern Linux distribution.

Use sed

Remove lines from output

Remove single line:

$ sed '1d' file

Remove range of lines:

$ sed '1,5d' file

Extract lines

Extract a single line for a file:

$ sed '123q;d' file

The q stops processing the file when the line is reached.

If you want to see a couple of lines from a CSV alongside with the header:

$ sed -n -e '1p' -e '809,820p' my.csv

Sort lines with sort

Sort by a column:

~# sort -k 1 myfile

Sort numeric:

~# sort -n myfile

Sort reverse:

~# sort -r myfile

Using date

Display date a month ago:

~# date -d "-1 month" +%Y-%m-%d

It automatically adjusts the day if the resulting date would be invalid.

Set time:

~# date +%T -s "12:12:00"

Epoch

Convert epoch to a date:

$ date +"%s"
1626247585

Convert date to epoch:

$ date -d @1626247585
Wed 14 Jul 09:26:25 CEST 2021

Using rsync

Copy files instead of symlinks

rsync -L /etc/letsencrypt/live/wildcard.linuxia.de/*.pem root@extern.linuxia.de:/etc/letsencrypt/live/wildcard.linuxia.de/.

Sync only files with a certain extension from one directory tree to another

rsync -av --include="*/" --include='*.status' --exclude='*' /samba/. .

Using ip

Show network interfaces with additional information (similar to ifconfig):

~# ip -a

Using lsof

Open files

Show open files in a directory:

~# lsof /mnt/backup

Network connections

~# lsof -i :80
COMMAND    PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   431729     root    6u  IPv4 7170923      0t0  TCP *:http (LISTEN)
nginx   431729     root    7u  IPv6 7170924      0t0  TCP *:http (LISTEN)
nginx   431730 www-data    6u  IPv4 7170923      0t0  TCP *:http (LISTEN)
nginx   431730 www-data    7u  IPv6 7170924      0t0  TCP *:http (LISTEN)

See also

An lsof Primer

Using lftp

Mirroring

Mirror from remote FTP server to local server

mirror -c source target

Please note that doesn't mirror dot files like .htaccess. To include these, adjust the default listing options:

set ftp:list-options -a

SSL

In case you really need to disable SSL (not recommended), use the following command:

set ftp:ssl-allow false

Using curl

Display the headers:

$ curl --head https://example.org

Skip certificate check:

# Shortcut: -k
$ curl --insecure https://example.org

Using wget

Display the headers:

$ wget --server-response https://example.org

Using convert

Combine images into a PDF file

$ convert 20220518_1450*.jpg photos.pdf

Encryption with GnuPG

Environment variables

GNUPGHOME

Select alternative home directory for GPG configuration and keys:

   export GNUPGHOME=~/.gnupg-old/