Search code examples
linuxxargs

Lab for my Alt OS class and I'm unsure of this Linux Command


I'm currently doing a lab for my Alt OS class and the professor gives multiple commands that you have to explain their function for. The one I'm stuck on is

find /home/ -user bob | xargs -d “\n” chown bill:bill 

I understand that we are finding any items within bob's home folder and piping that to xargs which is delimiting something. I'm just unsure what the "\n" portion is doing. At the end, I understand we are taking whatever those results are and changing permissions to bill.


Solution

  • From man xargs:

    --delimiter=delim, -d delim

    Input items are terminated by the specified character. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf command. Multibyte characters are not supported. When processing the input, quotes and backslash are not special; every character in the input is taken lit‐ erally. The -d option disables any end-of-file string, which is treated like any other argument. You can use this option when the input consists of simply newline-separated items, although it is almost always better to design your program to use --null where this is possi‐ ble.

    The \n escape sequence in C means a newline. The -d '\n' is typically used in xargs to delimite items by newlines - read one item per line. There is a significant difference as to quote handling:

    $ echo "quote'not terminated" | xargs
    xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
    

    vs

    $ echo "quote'not terminated" | xargs -d'\n'
    quote'not terminated
    

    On cppreference escape sequences you may find C escape sequences.