What to use when the “ls” command doesn’t work?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use the echo or find commands instead of ls:

echo *

or:
find -printf "%Mt%ut%gt%pn"

Method 2

You can also use the printf command, instead of echo:

printf '%sn' *

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):
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c70707d717d5c70707d717d">[email protected]</a>:~$ mkdir -p Misc/unix210948
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e9e9e4e8e4c5e9e9e4e8e4">[email protected]</a>:~$ cd !$
cd Misc/unix210948
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3cfcfc2cec2e3cfcfc2cec2">[email protected]</a>:~/Misc/unix210948$ touch -- -n
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5c9c9c4c8c4e5c9c9c4c8c4">[email protected]</a>:~/Misc/unix210948$ ls
-n
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="345858555955745858555955">[email protected]</a>:~/Misc/unix210948$ echo *
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ff3f3fef2fedff3f3fef2fe">[email protected]</a>:~/Misc/unix210948$ echo -- *
-- -n
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bdd1d1dcd0dcfdd1d1dcd0dc">[email protected]</a>:~/Misc/unix210948$ printf '%sn' *
-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!):

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adc1c1ccc0ccedc1c1ccc0cc">[email protected]</a>:~/Misc/unix210948$ rm ./-n
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="137f7f727e72537f7f727e72">[email protected]</a>:~/Misc/unix210948$ touch 'n'
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd91919c909cbd91919c909c">[email protected]</a>:~/Misc/unix210948$ ls
n
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f13131e121e3f13131e121e">[email protected]</a>:~/Misc/unix210948$ printf -- *

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2bebeb3bfb392bebeb3bfb3">[email protected]</a>:~/Misc/unix210948$ printf '%sn' *
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):

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b4b4b9b5b998b4b4b9b5b9">[email protected]</a>:~/Misc/unix210948$ rm ./*
zsh: sure you want to delete all the files in /home/llama/Misc/unix210948/. [yn]? y
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c10101d111d3c10101d111d">[email protected]</a>:~/Misc/unix210948$ touch -- '-n' 'n' 'name with spaces' '.hidden'
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a26262b272b0a26262b272b">[email protected]</a>:~/Misc/unix210948$ ls
-n  n  name with spaces
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f69a9a979b97b69a9a979b97">[email protected]</a>:~/Misc/unix210948$ printf '%sn' *
-n
n
name with spaces

If your printf supports %q, you could also use that (printf '%qn' *). This will escape spaces, newlines, etc. if there are any strange characters in your filenames. (Thanks to @muru in chat for pointing this out!)

Method 3

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

Method 4

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

Method 5

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)

Method 6

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.

Method 7

use

 echo -- *
    find . -printf "%Mt%ut%gt%pn"
    ls -C -b


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments