What command can be used to list all the files in your current directory?

ls (list) – the ls command is used to list the directory contents in the Linux system. By default, the ls command displays the content of the current directory. The ls command is also available in EFI (Extensible Firmware Interface) shell.

Syntax

The general syntax of the ls command is as follow −

$ ls [OPTION]... [FILE]...

Brief description of option available in the ls command.

Sr.No.Option & Description1-a, --all
Display all files including hidden files
2--author
Display author of each file with -l option
3-B, --ignore-backups
Do not display backup file
4-d, --directory
List only directories, not their contents
5-l
Display long listing format
6-N, --literal
Display entry names without quoting
7-R, --recursive
Display subdirectories recursively
8-x 
Display list by lines instead of by columns
9-Z, --context
Print any security context of each file
10--help
Displays a help message and then exits.
11--version
Output version information and exit

To display the current directory content in the Linux/Unix system, we use the ls command as shown in below.

$ ls

Here, we will display the list contents of a directory in long listing format as well as the author of each file, date, and owner permission using -l option with the ls command in the Linux system.

vikash@tutorialspoint: ~ $ ls -l shadow
total 16
-rw-rw-r—1 vikash Vikash 34 Dec 26 19:28 file.txt
-rw-rw-r—1 vikash Vikash 34 Dec 26 19:28 file.txt
-rw-rw-r—1 vikash Vikash 34 Dec 26 19:38 file.txt
drwxrwxr-x2vikash vikash 4096 Dec 26 19:47 snow

dir (directory) – the dir command is used to list contents of the directory but the output of the dir command is not colored like ls command. In the Windows operating system the dir command is also used to list the directory contents. By default, the dir command lists the contents of a directory in a column and shorted vertically. The command is available in CLI (command line interface).

Syntax

The general syntax of the dir command

$ dir [OPTION]... [FILE]...

Brief description of options available in the dir command.

Sr.No.Option & Description1-a, --all
Display all files including hidden files
2--author
Display author of each file with -l option
3-B, --ignore-backups
Do not display backup file
4-d, --directory
List only directories, not their contents
5-l
Display long listing format
6-N, --literal
Display entry names without quoting
7-R, --recursive
Display subdirectories recursively
8-x 
Display list by lines instead of by columns
9-Z, --context
Print any security context of each file
10--help
Displays a help message and then exits.
11--version
Output version information and exit.

To display current directory contents, also we use the dir command. The output of the dir command is not colored whereas the output of the ls command is colored.

The

ls
3 command lists files and directories within the file system, and shows detailed information about them. It is a part of the GNU core utilities package which is installed on all Linux distributions.

This article will show you how to use the ls command through practical examples and detailed explanations of the most common ls options.

How to Use the ls3 Command #

The syntax for the

ls
3 command is as follows:

ls [OPTIONS] [FILES]

When used with no options and arguments,

ls
3 displays a list of the names of all files in the current working directory :

ls

The files are listed in alphabetical order in as many columns as can fit across your terminal:

cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp

To list files in a specific directory, pass the directory path as an argument to the

ls
3 command. For example, to list the contents of the
cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp
0 directory, you would type:

ls /etc

You can also pass multiple directories and files separated by space:

ls /etc /var /etc/passwd

If the user you are logged in with doesn’t have read permissions to the directory, you will get a message saying that

ls
3 can’t open the directory:

ls /root
ls: cannot open directory '/root': Permission denied

The

ls
3 command has a number of options. In the sections below, we will explore the most commonly used options.

Long Listing Format #

The default output of the

ls
3 command shows only the names of the files and directories, which is not very informative.

The

cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp
4 ( lowercase L) option tells
ls
3 to print files in a long listing format.

When the long listing format is used, you can see the following file information:

  • The file type.
  • The file permissions.
  • Number of hard links to the file.
  • File owner.
  • File group.
  • File size.
  • Date and Time.
  • File name.

Here is an example:

ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct  4 11:31 /etc/hosts

Let’s explain the most important columns of the output.

The first character shows the file type. In this example, the first character is

cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp
6, which indicates a regular file. Values for other file types are as follows:

  • cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp
    
    6 - Regular file.
  • cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp
    
    8 - Block special file.
  • cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp
    
    9 - Character special file.
  • ls /etc
    0 - Directory.
  • ls /etc
    1 - Symbolic link.
  • ls /etc
    2 - Network file.
  • ls /etc
    3 - FIFO.
  • ls /etc
    4 - Socket.

The next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. You can change the file permissions with the

ls /etc
5 command. The permission character can take the following value:

  • ls /etc
    6 - Permission to read the file.
  • ls /etc
    7 - Permission to write to the file.
  • ls /etc
    8 - Permission to execute the file.
  • ls /etc
    4 -
    ls /etc /var /etc/passwd
    0 bit.
  • ls /etc /var /etc/passwd
    1 -
    ls /etc /var /etc/passwd
    2 bit.

In our example,

ls /etc /var /etc/passwd
3 means that the user can read and write the file, and the group and others can only read the file. The number
ls /etc /var /etc/passwd
4 after the permission characters is the number of hard links to this file.

The next two fields

ls /etc /var /etc/passwd
5 are showing the file owner and the group, followed by the size of the file (
ls /etc /var /etc/passwd
6), shown in bytes. Use the
ls /etc /var /etc/passwd
7 option if you want to print sizes in a human-readable format. You can change the file owner using the
ls /etc /var /etc/passwd
8 command.

ls /etc /var /etc/passwd
9 is the last file modification date and time.

The last column is the name of the file.

Show Hidden Files #

By default, the

ls
3 command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (
ls /root
1).

To display all files including the hidden files use the

ls /root
2 option:

ls -la ~/
ls
0

Sorting the Output #

As we already mentioned, by default, the

ls
3 command is listing the files in alphabetical order.

The

ls /root
4 option allows you to sort the output by extension, size, time and version:

  • ls /root
    5 (or
    ls /root
    6 ) - sort alphabetically by extension.
  • ls /root
    7 (or
    ls /root
    8) - sort by file size.
  • ls /root
    9 ( or
    ls: cannot open directory '/root': Permission denied
    
    0) - sort by modification time.
  • ls: cannot open directory '/root': Permission denied
    
    1 (or
    ls: cannot open directory '/root': Permission denied
    
    2) - Natural sort of version numbers.

If you want to get the results in the reverse sort order, use the

ls: cannot open directory '/root': Permission denied
3 option.

For example, to sort the files in the

ls: cannot open directory '/root': Permission denied
4 directory by modification time in the reverse sort order you would use:

ls
1

It’s worth mentioning that the

ls
3 command does not show the total space occupied by the directory contents. To get the size of a directory , use the
ls: cannot open directory '/root': Permission denied
6 command.

List Subdirectories Recursively #

The

ls: cannot open directory '/root': Permission denied
7 option tells the
ls
3 command to display the contents of the subdirectories recursively:

What is the command for listing all the files in the current directory?

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

Which command should you use to list all files of your current directory in Linux?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How can I get a list of files in a directory?

Open File Explorer in Windows. ... .
Click in the address bar and replace the file path by typing cmd then press Enter..
This should open a black and white command prompt displaying the above file path..
Type dir /A:D. ... .
There should now be a new text file called FolderList in the above directory..

Which command is used to list all the files in your current directory in Linux * ls Dir PWD mkdir?

The list ( ls ) command is equivalent to the DOS DIR command, in that it lists files and directories. If you simply type ls at a prompt ( $ ), you'll see all non-hidden files in your current directory, which is your home directory when you first log into a Linux system.