I tried to use the ls command and got an error:
bash: /bin/ls: cannot execute binary file
What can I use instead of this command?
I tried to use the ls command and got an error:
bash: /bin/ls: cannot execute binary file
What can I use instead of this command?
You can use the echo or find commands instead of ls:
echo *
or:
find -printf "%M\t%u\t%g\t%p\n"
You can also use the printf command, instead of echo:
printf '%s\n' *
printf is superior to echo in this situation in that echo does not respect the "double dash" (--) to signify the end of the argument list (on some systems, including Ubuntu 14.04 which is what I tested it on):
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
In this case, you cannot achieve the desired result with echo (since a file called -n gets interpreted as an option, and the double dash doesn't work, so you must use printf).
Note that you should always use a format string like the above when dealing with unknown data with printf, since otherwise you could receive unexpected results (thanks to @G-Man for pointing this out in the comments!):
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
A file called \n is interpreted as a newline by printf. To avoid this, we use a formatting string for printf (%s) and pass it the names of the files (expanded via globbing, as before).
This printf + formatting string solution can handle a wide variety of filenames (and also treats "hidden" files, that is, those starting with a ., the same as 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
If your printf supports %q, you could also use that (printf '%q\n' *). This will escape spaces, newlines, etc. if there are any strange characters in your filenames. (Thanks to @muru in chat for pointing this out!)
You should probably use the file and uname utilities to get a better idea of just what the hell is going on with your machine. Your error is indicative of a binary executable compiled for a system architecture incompatible with that on which it is invoked.
On my machine:
uname -m; file /bin/ls
...prints...
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
...because all is right with the world there. However, here's a little run of some commands run from my android tablet on an ssh connection to my desktop...
#first copy android ls to desktop
scp /system/bin/ls "$ssh:arm_ls"
ls 100% 151KB 151.4KB/s 00:00
#next login to desktop and run the following
ssh "$ssh" '
HOME=./arm_ls
chmod +x ~
file ~
bash -c ~ ||
rm ~
'
./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
Using bash (or many other shells), you can use tab completion to list files:
$ thisisnotacommand ./public_html/<TAB>
acc/ papers/ android/ l/
sdr/ blast formalcss.html switch/
busy/ formalcss.tar.gz others/ together.jpg
If you want something more like ls -l (file size, permissions, owner, timestamps, etc), then stat -- * might be helpful:
$ ls -l
total 8
-rw-rw-r-- 1 ubuntu ubuntu 4 Jun 20 21:50 a.txt
-rw-rw-r-- 1 ubuntu ubuntu 279 Jun 20 21:50 b.txt
$ stat -- *
File: ‘a.txt’
Size: 4 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 787691 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ ubuntu) Gid: ( 1000/ ubuntu)
Access: 2015-06-20 21:50:23.395223851 -0700
Modify: 2015-06-20 21:50:23.395223851 -0700
Change: 2015-06-20 21:50:23.395223851 -0700
Birth: -
File: ‘b.txt’
Size: 279 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 844130 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ ubuntu) Gid: ( 1000/ ubuntu)
Access: 2015-06-20 21:50:31.763084155 -0700
Modify: 2015-06-20 21:50:51.066758216 -0700
Change: 2015-06-20 21:50:51.066758216 -0700
Birth: -
$
Alternatively if all you wanted to do is misspell ls, then try sl instead. ;-) (go on - try it... you might have to install it)
There is very similar utility available on many systems:
dir
According to the info pages it is equivalent to ls -C -b, but it is standalone binary file.
The benefit of using dir (instead of for example shell completion system) is availability of different options like sorting, showing hidden files, colouring etc., just like in the standard ls program.
BTW, I guess its name comes from windows (or rather dos) system.
use
echo -- *
find . -printf "%M\t%u\t%g\t%p\n"
ls -C -b