CodeStride
The Command Line

Listing files in Git & Command Line

Topic 3 of 32 · 8 minFree lesson

You've used ls to see the folders you can move into. It can do a lot more. It can look inside any folder without going there, show files that are usually hidden, and tell you how big each file is.

These are the moves you'll use every day to find your way around a project.

Listing another folder

Give ls the path of a folder, and it lists that folder instead of the one you're in. The terminal stays where it was:

Example 1 · Terminal
$ ls notesideas.txt shopping.txt todo.txt$ ls ~/projects/websiteindex.html style.css$ pwd/home/learner

You looked inside notes and website, but pwd shows you never left your home folder. Any path works here, relative or absolute.

Several folders at once

Give ls more than one folder, and it lists each one under its name, with a blank line between them:

Example 2 · Terminal
$ ls photos recipesphotos:beach.jpg cat.jpg mountains.pngrecipes:pancakes.txt soup.txt

The names in front of the colons tell you which list is which.

Hidden files

A name that starts with a dot is a hidden file: ls leaves it out, so it doesn't clutter the list. Programs often keep their settings in hidden files.

An option is an argument that starts with - and changes how a command works. The -a option, short for "all", makes ls show hidden files too:

Example 3 · Terminal
$ ls -a notes. .. .reminder.txt ideas.txt shopping.txt todo.txt

There's a hidden .reminder.txt in notes. The list also starts with . and .., which every folder has. You met .., the folder above, in the last topic. A single . means "this folder".

Most of the time you don't need those two. -A, with a capital A, shows hidden files but leaves out . and ..:

Example 4 · Terminal
$ ls -A.gitconfig notes photos projects recipes

Your home folder has a hidden file too: .gitconfig holds settings for Git, which you'll meet later in this course.

The long format

-l, short for "long", shows one file per line, with details:

Example 5 · Terminal
$ ls -l notestotal 12-rw-r--r-- 1 learner learner 61 Mar 3 09:00 ideas.txt-rw-r--r-- 1 learner learner 37 Mar 3 09:00 shopping.txt-rw-r--r-- 1 learner learner 53 Mar 3 09:00 todo.txt

Read each line from left to right:

  • The first character is the type: - for a file, d for a folder.
  • The next nine letters say who may read, change or run the file. Skip them, and the small number after them, for now.
  • learner learner is the account that owns the file, and its group.
  • The number before the date is the size in bytes. A byte holds one plain letter, space or line break, so todo.txt holds 53 of them.
  • Then comes the date and time it last changed, and finally its name.

The total line at the top counts disk space. You can ignore it.

One name per line

ls fits names into columns to save space. -1 (the number one) puts each name on its own line instead, so a long list reads from top to bottom:

Example 6 · Terminal
$ ls -1 recipespancakes.txtsoup.txt

Files or folders?

Plain ls shows files and folders in the same way. ls -l tells them apart: folders start with d.

Example 7 · Terminal
$ ls -l projectstotal 4drwxr-xr-x 2 learner learner 4096 Mar 3 09:00 website

website is a folder. Its size, 4096, is the room a folder keeps for its list of names, not the size of what's inside.

If you give ls the path of a file, it prints the file's name back. With -l, you get that file's details:

Example 8 · Terminal
$ ls notes/todo.txtnotes/todo.txt$ ls -l recipes/soup.txt-rw-r--r-- 1 learner learner 80 Mar 3 09:00 recipes/soup.txt
Common mistakeAn option needs a dash, and a space between it and the command. Without the space, the shell looks for a command called ls-a. Without the dash, ls looks for a file called a.
Example 9 · Terminal
$ ls-abash: ls-a: command not found$ ls als: cannot access 'a': No such file or directory
TipYou can put options together. ls -la is the same as ls -l -a: the long format, with hidden files too.

Try it yourself

Run the code in the editor and answer 8 practice questions on listing files. It's free, you only need an account.