brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -printf '%P' prints paths without the starting folder

Find
~/tips/tip-find-printf-p-relative-paths

find /srv/site -type f -printf '%P\n'

Gives you paths relative to the folder you searched, ready for another command.

find prints every result with the starting folder in front. When you want a list to compare with another machine, or to feed into rsync or tar, that prefix is in the way.

find /srv/site -type f -printf '%P\n'

%P is the path with the starting point removed. So /srv/site/css/main.css comes out as css/main.css. Compare that with cd /srv/site && find ., which gives ./css/main.css, with a dot in front that some tools don’t like.

A use I like is comparing two copies of a tree:

diff <(find /srv/site -type f -printf '%P\n' | sort) <(find /mnt/backup/site -type f -printf '%P\n' | sort)

Because both lists have the same relative paths, diff shows only the files that are missing on one side.

There’s a related one, %f, which prints just the file name without any folder. Mind the newline: -printf doesn’t add one, so without \n everything ends up on one long line. And -printf is GNU only. On a Mac, the built-in find doesn’t have it.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link