How to "cut" into null?

The Unix command 'file' has the -0 option to output the null character after the file name. This is supposed to be convenient for use with cut.

From man file :

 -0, --print0 Output a null character '\0' after the end of the filename. Nice to cut(1) the output. This does not affect the separator which is still printed. 

(Note: in my Linux, the separator '-F' is NOT printed, which makes more sense to me.)

How can you use 'cut' to extract a file name from the output of a 'file'?

This is what I want to do:

 find . "*" -type f | file -n0iNf - | cut -d<null> -f1 

where <null> is the NUL character.

Well, this is what I'm trying to do, what I want to do is get all the file names from a directory tree with a specific MIME type. I am using grep (not shown).

I want to process all legal file names and not get stuck file names with colons, for example, on their behalf. Consequently, NUL will be excellent.

I think the futile solutions are great too, but I don't want to give up a simple idea.

+6
source share
1 answer

Just specify an empty delimiter:

 cut -d '' -f1 

(NB: the space between -d and '' is important, so -d and the empty string are passed as separate arguments, if you write -d'' , then it will be passed as just -d , and then cut will think that you trying to use -f1 as the delimiter that he will complain about, with the error message that "the delimiter should be the only character".)

+9
source

Source: https://habr.com/ru/post/911548/


All Articles