Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Imagine you were working on a system and someone accidentally deleted the ls command (/bin/ls). How could you get a list of the files in the current directory? Try it.

I tried many methods and also searched on the Internet but nothing. I want to ask beside ls command what command can we use to list out all the files.

share|improve this question

migrated from stackoverflow.com Apr 10 '11 at 18:26

This question came from our site for professional and enthusiast programmers.

11 Answers 11

echo *

... will show files in the current folder through file globbing on Bourne compatible shells.

This one exists for recursive listing (newer versions of some shells, see the comments on this answer):

echo **/*
share|improve this answer
7  
Beyond the homework exercise, echo * is useful in practice, when you've screwed up your system so that you can't execute any program (e.g. you removed libc.so or ld.so), but you still have a running shell. –  Gilles Apr 10 '11 at 18:30
3  
In bash4 (I think) and zsh, you can use echo **/* to do this recursively, too. –  Reid Apr 10 '11 at 20:07
    
@Reid: gonna test that. Nice find. –  0xC0000022L Apr 10 '11 at 20:18
    
Good one, @Gilles. Learned that from old DEC systems which could not have access to either /usr or /bin in certain boot conditions and boot scripts needed to make 'cat' and 'ls' functions themselves. –  Arcege Apr 11 '11 at 0:10
1  
nice, I first learned about echo * from ee.ryerson.ca/~elf/hack/recovery.html –  mmckinst Apr 11 '11 at 2:25
find -maxdepth 1 
find -maxdepth 1 -ls

less TAB TAB

finds -ls switch is independent of /bin/ls and has its own format, and displays detail information:

127432    0 drwxr-xr-x   2 stefan   stefan         48 Apr  8 22:51 ./temp/falsch/.hiddenfalsch
127447    0 lrwxrwxrwx   1 stefan   stefan          9 Apr  8 22:51 ./temp/falsch/linkfalsch -> subfalsch
127427    0 drwxr-xr-x   2 stefan   stefan         48 Apr  8 22:51 ./temp/.hiddenmusik

another, details showing possibility, is stat

stat *
  File: `halx0o'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 807h/2055d  Inode: 102701      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  stefan)   Gid: ( 1000/  stefan)
Access: 2011-04-08 22:38:18.000000000 +0200
Modify: 2009-07-23 03:16:15.000000000 +0200
Change: 2011-04-09 23:29:13.000000000 +0200
  File: `ho ho ho'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 807h/2055d  Inode: 115835      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  stefan)   Gid: ( 1000/  stefan)
Access: 2011-04-08 22:38:18.000000000 +0200
Modify: 2010-07-24 14:12:48.000000000 +0200
Change: 2011-04-09 23:29:13.000000000 +0200
share|improve this answer

To make it look like ls, I'd use Bash's for loop:

for i in *; do echo $i; done

If that doesn't work, I'd try Python ;) :

python -c "import glob; print '\n'.join(glob.glob('/home/*'))"
share|improve this answer
    
+1 for the Python code :) –  Nishant Apr 10 '11 at 23:14
    
import glob; print '\n'.join(glob.glob('/home/*')) would work just as well. –  Arcege Apr 11 '11 at 0:13

Good ol' vim ;) (with sorting, browsing, etc)

vim .
share|improve this answer

find .

Though you probably would want to enter whatever limiting parameters you need.

share|improve this answer

lsattr ./*

getfacl ./* will display permissions too

grep -l '.*' ./*

awk 'FNR==1 {print FILENAME}' ./* works with GNU awk

debugfs /dev/sdX replace sdX with whatever partition you want to do an ls on, then you can ls inside debugfs

debugfs:  cd /
debugfs:  ls -l
      2   40755 (2)      0      0    4096  6-Apr-2011 01:01 .
      2   40755 (2)      0      0    4096  6-Apr-2011 01:01 ..
     11   40700 (2)      0      0   16384  5-Jul-2010 09:59 lost+found
 2392065   40755 (2)      0      0    4096  5-Jul-2010 09:59 boot
 2228225   40755 (2)      0      0    4096  5-Jul-2010 09:59 sys
 1376257   40755 (2)      0      0    4096  5-Jul-2010 09:59 proc
 4915201   40755 (2)      0      0    4096  5-Jul-2010 09:59 dev
 3473409   40755 (2)      0      0   12288 10-Apr-2011 22:05 etc
  98305  100644 (1)      0      0       0  6-Jul-2010 12:05 .autofsck
 3342337   40755 (2)      0      0    4096  5-Apr-2011 15:05 var
 3932161   41777 (2)      0      0    4096 10-Apr-2011 22:11 tmp
  ..........
debugfs:  

lynx ./

mc assuming you have midnight commander installed

share|improve this answer

Well if I couldn't use ls within shell mode of emacs, I might switch to eshell mode or just a dired.

share|improve this answer

In bash, or a shell with similar tab-completion, you don't need to use a real command at all: wibble followed by two tabs will get you a list of files in the current directory, and wibble /bin/ followed by two tabs will show you what's left of /bin. I once found myself in this situation (actually, I'd killed /bin entirely along with some other things), and started off using the echo * approach @STATUS_ACCESS_DENIED suggested, but eventually found tab completion more convenient.

share|improve this answer

How about dir command? :)

/bin/dir

share|improve this answer

If I have bash shell started (or any other shell with completion), I would probably just type '*' then tab.

Or as I usually have X started, I may also just do 'nautilus .', but OK that's for humans.

I may also try /usr/bin/lsattr

But in the real world what I would really do if such thing occured to me would certainly be:

sudo apt-get install --reinstall coreutils
share|improve this answer

Similar to the echo version, but this prints out one entry per line for greater readability:

printf %s\\n ./*

or

printf '%s\n' ./*

Recursively (for bash 4+; other shells either need a different option to set it or have globstar enabled by default):

shopt -s globstar
printf %s\\n ./**/*
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.