Written by Chris Gregg, with modifications by Nick Troccoli
Click here for a walkthrough video.
The ls
command lists files and directories. You will use it every time you log onto the myth computers.
In its no-argument form, it simply lists all the files in the current directory:
$ ls
assign0 assign1 assign2 assign3 file1.txt file2.txt
$
There are many, many arguments you can use with ls
. The most common is probably the -l
flag, which lists files with extra information:
$ ls -l
total 12
drwx------ 2 cgregg operator 2048 Sep 6 11:42 assign0
drwx------ 4 cgregg operator 2048 Sep 6 08:42 assign1
drwx------ 3 cgregg operator 2048 Sep 6 08:43 assign2
drwx------ 2 cgregg operator 2048 Sep 6 09:44 assign3
-rw------- 1 cgregg operator 18 Sep 6 11:50 file1.txt
-rw------- 1 cgregg operator 46 Sep 6 11:50 file2.txt
$
The first field of the listing shows whether or not the file is a directory ('d'), and this is followed by the "permissions" for the file. The permissions are not that important to know right now, but the "rwx" simply means that you have the ability to (r)ead, (w)rite, and (e)xecute a file or the files in a directory.
The next important field is the size of the file (directories on myth have 2048 bytes by default). So, file1.txt
is 18 bytes long, and file2.txt
is 46 bytes.
The date field tells you when the file was last modified, and it will contain the time if in the current year, and the year otherwise.
Finally, the file name is listed.
Another common flag is -a
, which lists all files, including "hidden" files, which begin with a period. You can use flags together, and they combine effects. E.g.,
$ ls -al
total 16
drwx------ 6 cgregg operator 2048 Sep 6 12:02 .
drwx------ 7 cgregg operator 2048 Sep 6 08:46 ..
drwx------ 2 cgregg operator 2048 Sep 6 11:42 assign0
drwx------ 4 cgregg operator 2048 Sep 6 08:42 assign1
drwx------ 3 cgregg operator 2048 Sep 6 08:43 assign2
drwx------ 2 cgregg operator 2048 Sep 6 09:44 assign3
-rw------- 1 cgregg operator 4 Sep 6 12:02 .do_not_look.txt
-rw------- 1 cgregg operator 18 Sep 6 11:54 file1.txt
-rw------- 1 cgregg operator 46 Sep 6 11:55 file2.txt
-rw------- 1 cgregg operator 22 Sep 6 12:01 .this_is_hidden.txt
There are four "hidden" files above: .
, ..
, .do_not_look.txt
, and .this_is_hidden.txt
.
If you want to have a short listing that tells you which names are files, which are executable, and which are directories, use the -F
flag:
$ ls -F
hello* hello.c hello.c~ innerFolder/ readme.txt
$
Names that end in *
are executable, names that end in /
are directories, and all other names are files. (Note: the ~
at the end of hello.c~
is the designation for a backup file from the vim
editor, and not part of the shell (the "shell" is the program that runs the terminal and interprets commands).
The same directory with the -al
flags:
$ ls -al
total 32
drwx------ 3 cgregg operator 2048 Sep 6 12:06 .
drwx------ 6 cgregg operator 2048 Sep 6 12:02 ..
-rwx------ 1 cgregg operator 8558 Sep 6 08:42 hello
-rw------- 1 cgregg operator 99 Sep 6 08:42 hello.c
-rw------- 1 cgregg operator 4 Sep 6 12:05 hello.c~
drwx------ 2 cgregg operator 2048 Sep 6 12:06 innerFolder
-rw------- 1 cgregg operator 15015 Sep 6 12:05 readme.txt
$
Note that the hello
program has the x
attribute, meaning that it is executable, or runnable (it is a program).
You can see all the possible arguments using the man ls
command (q
to quit). For instance, if you want to see a directory list that shows all the files with the most recently modified files at the end, for example, you can do this:
$ ls -alrt
total 32
-rw------- 1 cgregg operator 99 Sep 6 08:42 hello.c
-rwx------ 1 cgregg operator 8558 Sep 6 08:42 hello
drwx------ 6 cgregg operator 2048 Sep 6 12:02 ..
-rw------- 1 cgregg operator 4 Sep 6 12:05 hello.c~
-rw------- 1 cgregg operator 15015 Sep 6 12:05 readme.txt
drwx------ 2 cgregg operator 2048 Sep 6 12:06 innerFolder
drwx------ 3 cgregg operator 2048 Sep 6 12:06 .