我想创建一个只有2个字节的文件,文本"00">
当我运行命令echo -n ok > myFile
在'myFile'中只有2个字节时,我在运行ls -l
后得到6个字节
事实上,它在字节部分显示我有6个字节,但我只想要2个字节。
使用printf
:
printf '%s' ok >myFile
…或者更简单(但不太可靠,因为这并不总是工作正确,如果你的ok
是一个字符串与%
s,文字反斜杠等):
printf ok >myFile
您可以使用dd
utility
dd if=/dev/zero of=twobytes.txt count=2 bs=1
2+0 records in
2+0 records out
2 bytes copied, 0.000128232 s, 15.6 kB/s
cat twobytes.txt | hexdump
0000000 0000
0000002
ls -l twobytes.txt
-rw-r--r-- 1 lmc users 2 Aug 4 17:39 twobytes.txt
这也可以(谢谢@CharlesDuffy)
tr -d 'n' <<<'00' > twobytes.txt
不推荐,因为echo
的行为可能在某些实现中有所不同(参见下面的注释)
echo -ne "