Some system-level values are output with a null byte (the bytes value is 00) and might need to read those values seperately. This Null byte delimitation can be worked around using xargs and its -0 argument.

For Example:

# When you run ps aux. You see all of the processes and their attributes.

$ ps aux
...skip...
root     31413 99.4 16.4 699836 690828 ?       R    01:31  62:39 xz -9 /root/backup/full_infotinks_backup-20220814_013002.tar 
...skip...

# Note the PID we are interested in is 31413

# Now you can also get this information from /proc/PID/cmd
$ cat /proc/31413/cmdline
xz-9/root/backup/full_infotinks_backup-20220814_013002.tar

# The output can be read by a person, but if you need to code with that, its hard to tell where one argument begins and another ends.
# So here is a trick to do it with xargs. It has a split by 0 byte option if you use -0 that is very helpful

$ cat /proc/31413/cmdline | xargs -0 echo
xz -9 /root/backup/full_infotinks_backup-20220814_013002.tar

# now we see each of the arguments space seperated
# so we split by the 0 byte and output to a single echo
# you might be familiar with find -print0 | xargs -0 <something>

# what if some of the arguments were also space seperated?
# well then you can seperate the arguments with a new line

$  cat /proc/31413/cmdline | xargs -0 -I % echo %
xz
-9
/root/backup/full_infotinks_backup-20220814_013002.tar

# So now we know the first argument is xz, the second is -9, and the third is the path
# This works the same as before, xargs splits on the 0 byte, but then it takes each variable and called it %, and pumps each to a seperate echo statement
# so its like echo <arg1>; echo <arg2>; ... so on until its done

To summarize the trick is

# to space seperate your 0 bytes delimited items
CommandWhichHasNullBytes | xargs -0 echo

# to newline seperate your 0 byte delimited items
CommandWhichHasNullBytes | xargs -0 -I% echo %

Leave a Reply

Your email address will not be published. Required fields are marked *