Networking / Beginners

The ls command

The ls command lists the contents of the current directory. Here's the syntax:

ls [options] directory

The following paragraphs describe the more important options for the ls command:

  • -a: Lists all the files in the directory, including files that start with a period.
  • -c: Sorts entries by the time the files were last modified.
  • -d: Lists only directory names.
  • -l: Displays in long format.
  • -r: Displays files in reverse order.
  • -R: Lists the contents of all subdirectories, and subdirectories of subdirectories, and subdirectories of subdirectories of subdirectories . . . in other words, lists subdirectories recursively.
  • -s: Displays file sizes.
  • -S: Sorts files by size.
  • -t: Sorts files by timestamp.
  • -u: Sorts files by the time the files were last accessed.
  • -X: Sorts files by their extensions.

Without arguments, the ls command lists all the files in the current directory, like this:

$ pwd
/etc/mail
$ ls
access 		helpfile 	 Makefile 	submit.cf
access.db 	local-host-names sendmail.cf 	submit.cf.bak
domaintable 	mailertable 	 sendmail.mc 	submit.mc
domaintable.db 	mailertable.db 	statistics 	trusted-users

virtusertable
virtusertable.db

You can limit the display to certain files by typing a filename, which can include wildcards. For example:

$ ls a*
access 		access.db

You can also specify the directory that you want to display, like this:

$ ls /etc/httpd
conf 	conf.d 	logs 	modules    run

To display detailed information about the files in the directory, use the -l switch, as in this example:

$ ls /etc/mail/s* -l
-rw-r--r-- 1 root root 57427 Jul 19 16:35 sendmail.cf
-rw-r--r-- 1 root root 5798 Feb 24 16:15 sendmail.mc
-rw------- 1 root root 628 Jul 24 17:21 statistics
-rw-r--r-- 1 root root 39028 Jul 19 17:28 submit.cf
-r--r--r-- 1 root root 39077 Feb 24 16:15 submit.cf.bak
-rw-r--r-- 1 root root 953 Feb 24 16:15 submit.mc
[Previous] [Contents] [Next]