16 linux command-line ls
我尝试使用该ls命令并出现错误:
bash: /bin/ls: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)
我可以用什么来代替这个命令?
Inv*_*ker 33
您可以使用echo或find命令代替ls:
echo *
Run Code Online (Sandbox Code Playgroud)
或者:
find -printf "%M\t%u\t%g\t%p\n"
Run Code Online (Sandbox Code Playgroud)
Doo*_*nob 25
您也可以使用printf命令,而不是 echo:
printf '%s\n' *
Run Code Online (Sandbox Code Playgroud)
printf优于echo在这种情况下,在echo没有不尊重“双破折号”( --)来表示参数列表的末尾(在某些系统上,包括Ubuntu 14.04这是我测试了):
llama@llama:~$ mkdir -p Misc/unix210948
llama@llama:~$ cd !$
cd Misc/unix210948
llama@llama:~/Misc/unix210948$ touch -- -n
llama@llama:~/Misc/unix210948$ ls
-n
llama@llama:~/Misc/unix210948$ echo *
llama@llama:~/Misc/unix210948$ echo -- *
-- -n
llama@llama:~/Misc/unix210948$ printf '%s\n' *
-n
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您无法使用echo(因为名为的文件-n被解释为选项,并且双破折号不起作用,因此您必须使用printf)。
请注意,在使用 处理未知数据时,应始终使用上述格式字符串printf,否则可能会收到意外结果(感谢@G-Man 在评论中指出这一点!):
llama@llama:~/Misc/unix210948$ rm ./-n
llama@llama:~/Misc/unix210948$ touch '\n'
llama@llama:~/Misc/unix210948$ ls
\n
llama@llama:~/Misc/unix210948$ printf -- *
llama@llama:~/Misc/unix210948$ printf '%s\n' *
\n
Run Code Online (Sandbox Code Playgroud)
调用的文件\n被解释为换行符printf。为了避免这种情况,我们为printf( %s)使用格式化字符串并将文件名传递给它(通过 globbing 扩展,像以前一样)。
这个printf+ 格式化字符串解决方案可以处理各种各样的文件名(并且还处理“隐藏”文件,即以 a 开头的文件,与.相同ls):
llama@llama:~/Misc/unix210948$ rm ./*
zsh: sure you want to delete all the files in /home/llama/Misc/unix210948/. [yn]? y
llama@llama:~/Misc/unix210948$ touch -- '-n' '\n' 'name with spaces' '.hidden'
llama@llama:~/Misc/unix210948$ ls
-n \n name with spaces
llama@llama:~/Misc/unix210948$ printf '%s\n' *
-n
\n
name with spaces
Run Code Online (Sandbox Code Playgroud)
如果您printf支持%q,您也可以使用那个 ( printf '%q\n' *)。如果文件名中有任何奇怪的字符,这将转义空格、换行符等。(感谢@muru 在聊天中指出这一点!)
您可能应该使用file和uname实用程序来更好地了解您的机器到底发生了什么。您的错误表明为与调用它的系统架构不兼容的系统架构编译的二进制可执行文件。
在我的机器上:
uname -m; file /bin/ls
Run Code Online (Sandbox Code Playgroud)
...印刷...
x86_64
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=..., stripped
Run Code Online (Sandbox Code Playgroud)
...因为那里的世界一切都很好。但是,这里有一些命令从我的 android 平板电脑ssh连接到我的桌面上运行......
#first copy android ls to desktop
scp /system/bin/ls "$ssh:arm_ls"
Run Code Online (Sandbox Code Playgroud)
ls 100% 151KB 151.4KB/s 00:00
Run Code Online (Sandbox Code Playgroud)
#next login to desktop and run the following
ssh "$ssh" '
HOME=./arm_ls
chmod +x ~
file ~
bash -c ~ ||
rm ~
'
Run Code Online (Sandbox Code Playgroud)
./arm_ls: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, stripped
bash: ./arm_ls: cannot execute binary file: Exec format error
Run Code Online (Sandbox Code Playgroud)
小智 5
使用 bash(或许多其他 shell),您可以使用制表符补全来列出文件:
$ thisisnotacommand ./public_html/<TAB>
acc/ papers/ android/ l/
sdr/ blast formalcss.html switch/
busy/ formalcss.tar.gz others/ together.jpg
Run Code Online (Sandbox Code Playgroud)