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 ncdu

ncdu: NCurses Disk Usage

Use cat

Show file contents

$ cat /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.

Show whitespace

$ cat -vet

Use tail

Show file content without the first line:

$ tail -n +2 test.csv

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

Display epoch

$ date +"%s"
1626247585

Convert epoch to a date

$ 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

$ ip -a

Add IP address

$ sudo ip a add 192.168.36.11/24 dev eth0

IPv6

Show global reachable addresses:

$ ip -6 addr show scope global

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)

It works with service as well, e.g. lsof -i:ntp.

See also

An lsof Primer

Using ncat

Retrieve CheckMK agent output:

$ ncat agent.example.org 6556 < /dev/null

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

Connect without DNS:

$ curl https://example.org --connect-to example.org:93.184.216.34

Using wget

Display the headers:

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

Using convert

Reduce file size of a JPG image

$ convert photo-large.jpg -quality 82 photo-small.jpg

Rotate image clockwise

$ convert image.jpg -rotate 90 image2.jpg

Combine images into a PDF file

$ convert 20220518_1450*.jpg photos.pdf

Convert SVG file to PNG with transparency

$ convert -background none image.svg image.png

Using pdfjam

Produce PDF from image and convert to A4 format:

$ convert image.jpg image.pdf
$ pdfjam --paper a4paper --outfile image-a4.pdf image.pdf

Using zip

Create password protected archive

$ zip -e sccl2.zip sccl2_2.9-4_all.deb sccl2-2.9-4.noarch.rpm
Enter password:
Verify password:
  adding: sccl2_2.9-4_all.deb (deflated 0%)
  adding: sccl2-2.9-4.noarch.rpm (deflated 26%)

Using timeout

Run dhclient with a time limit:

$ timeout 10 dhclient -6

Encryption with GnuPG

Environment variables

GNUPGHOME

Select alternative home directory for GPG configuration and keys:

   export GNUPGHOME=~/.gnupg-old/