Josh Thompson     about     archive     office hours

`ls` command to show directory contents

Table of Contents

I like to use the tree command on my local machine when trying to peek into the structure and contents of a given directory.

tree -L 2

will [L]ist recursively everything [2] levels deep from your current directory. The output is nicely formatted like this:

> tree -L 2
├── cargo
│   ├──
│   ├──
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├──
│   ├── appveyor.yml
│   ├── rustfmt.toml
│   ├── src
│   ├── target
│   └── tests
├── fastly
│   └── fastly-test-blog
├── learning_aws
├── learning_elixir
│   ├── hellow
│   ├── sample01.exs
│   └── sling_clone
├── learning_over_the_wire
│   ├── bandit
│   └── leviathan
├── learning_react
│   └── react_tutorial
├── learning_ruby_rails
│   ├── InstaClone
│   ├── blocks_practice.rb
│   ├── chris_pine_ruby_lessons
│   ├── eloquent_ruby
│   ├── email_sender

If you’ve SSHed into a linux box, however, and you’re trying to look around a bit, you won’t have tree available to you. How can you list out the contents of directories?

Easy. The good ‘ol ls command:

ls -1d ./*/*

results in:

> ls -1d ./*/*

ls is obviously “list”. What about that -1d thing?

according to the man page:

-1   (The numeric digit ``one''.)  Force output to be one entry per line.  This is the
      default when output is not to a terminal.
-d   Directories are listed as plain files (not searched recursively).

to achieve a certain number of directories in depth, just repeat the ./*./* pattern, one /* per directory depth you’d like to view.

The output isn’t always particularly readable - sometimes you get a ton of results.

If this is the case, you could always pipe the output to head, or grep the output. That said, I’ve used this general pattern regularly, and it’s been helpful.

Are you a software developer who:

And wants to do all this in the most time-efficient way possible?

Great. Punch in your email:

Of course, you don't have to be a software developer to subscribe. It's easy to unsubscribe at any point. 😉