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/
56M    /tmp/lYZNO1PD09/
29M    /tmp/aF_jPhgvwD/

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="11 (bullseye)"

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"


Display epoch

$ date +"%s"

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/*.pem

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 dev eth0


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
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 lftp


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


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

Skip certificate check:

# Shortcut: -k
$ curl --insecure

Connect without DNS:

$ curl --connect-to

Using wget

Display the headers:

$ wget --server-response

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_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


Select alternative home directory for GPG configuration and keys:

   export GNUPGHOME=~/.gnupg-old/