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: xargs -d '\n' keeps file names with spaces in one piece

Shell
~/tips/tip-shell-xargs-d-newline

printf '%s\n' "holiday photo.jpg" "notes 2026.txt" | xargs -d '\n' -n1 echo

Makes xargs split its input only at newlines, so names with spaces stay whole.

By default xargs splits its input on blanks and newlines, and it treats quotes and backslashes as special. That is a bad match for a list of file names.

If your input has one name per line, tell xargs that the newline is the only separator:

printf '%s\n' "holiday photo.jpg" "notes 2026.txt" | xargs -d '\n' -n1 echo

Each line now arrives as exactly one argument, spaces included. Without -d, the same command would split holiday photo.jpg into two arguments. The man page also says that with -d quotes and backslashes are not special and every character is taken literally, so an apostrophe in a name stops being a problem too.

A more realistic use is a list you got from somewhere else, such as a text file with one path per line:

xargs -d '\n' -a /mnt/user/appdata/<app>/files.txt ls -ld

Here -a reads the items from the file instead of standard input. Swap ls -ld for something harmful and a wrong list does real damage, so test with echo in front first.

-d is a GNU option. The xargs on macOS may not have it, and there you would produce null-separated input and use -0. A file name that itself contains a newline still breaks the newline approach. For fully safe pipelines, let find print null-terminated names with -print0 and read them with xargs -0.

Further reading: xargs(1) manual page

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link