31

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.

12 Answers 12

36
echo *

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

This lists all files down one level:

echo */*

In Bash, if globstar is set (set with shopt -s globstar, unset with shopt -u globstar), this will list all files recursively:

echo **
8
  • 11
    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. Commented Apr 10, 2011 at 18:30
  • 3
    In bash4 (I think) and zsh, you can use echo **/* to do this recursively, too.
    – Reid
    Commented Apr 10, 2011 at 20:07
  • @Reid: gonna test that. Nice find. Commented Apr 10, 2011 at 20:18
  • 1
    nice, I first learned about echo * from ee.ryerson.ca/~elf/hack/recovery.html Commented Apr 11, 2011 at 2:25
  • 1
    I suppose this is also marginally faster, since it avoids the invocation of the ls executable.
    – intuited
    Commented Apr 19, 2011 at 5:40
23
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
11

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/*'))"
1
  • import glob; print '\n'.join(glob.glob('/home/*')) would work just as well.
    – Arcege
    Commented Apr 11, 2011 at 0:13
6

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

vim .
6
find .

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

4

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 ./**/*
1
  • No need for the '/*', a simple ** will recurse into all directories if globstar is set. Just change to printf '%s' **.
    – user79743
    Commented Jul 28, 2015 at 4:39
3

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

2

How about dir command? :)

/bin/dir

1

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

1

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
1

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.

0

Another way with tree, not mentioned anywhere here, it goes recursively and unlike find or ls you don't have any errors (like: Permission denied, Not a directory) you also get the absolute path in case you want to feed the files to xargs or other command

tree -fai /pathYouWantToList >listOfFiles.list

the options meaning:

-a     All  files  are  printed.  By default tree does not print hidden files (those beginning with a dot
       `.').  In no event does tree print the file system constructs `.'  (current  directory)  and  `..'
       (previous directory).

-i     Makes tree not print the indentation lines, useful when used in conjunction with the -f option.

-f     Prints the full path prefix for each file.

You must log in to answer this question.