Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of listing Files by using ls Command in Linux

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the method of listing files using the ls command in Linux. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

How do I use the ls command?

The syntax of the ls command is:

Ls [OPTIONS] [FILES]

The ls command has many [OPTIONS] options, and here are some commonly used options:

●-l: you can make ls list files under the long list format.

●-a: displays all files and directories, including the resulting files.

●-t: lists files in the order of creation (modification).

●-r: displays documents in reverse order (originally in alphabetical order).

●-R: if there are files in the directory, the files are listed in order.

When no parameters are used, the ls command lists the names of all files in the current working directory in alphabetical order.

Ls

Output:

Cache empty games lib local mail opt run

To list files in a specific directory, you need to pass the path to the directory to the ls command. For example, to list the contents of the directory / etc, type:

Ls / etc

You can also pass multiple directories and files to the space-separated ls command:

Ls / etc/ var / etc/passwd

If the logged-in user does not have read access to the directory, the user will receive a message indicating that the ls command cannot open the directory:

Ls / root

Output:

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

How to list the long list format?

The default output of the ls command shows only the name of the file, which is not very informative. However, when using the long list format, the ls command displays the following file information:

● file type

● file permissions

Number of hard links to ● files

● file owner

● filegroup

● file size

● date and time

● file name

We can use the-l (lowercase L) option to make ls output file information in a long list format. For example:

Ls-l / etc/hosts

Output:

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

Description: let's explain the output information.

1. The first character'- 'shows the file type

In our example, the first character is -, which represents a regular file. The values for other file types are as follows:

● -: general Fil

● b: block special files

● c: role-specific files

● d: directory

● l: symbolic links

● n: network fil

● p:FIFO

● s: socket

2. Nine characters (rw-r--r--) display file permissions

The first three characters are for users, the last three are for groups, and the last three characters are for others. We can use the chmod command to change file permissions; permission characters can take the following values:

● r: permissions to read files

● w: permission to write to the file

● x: permissions to execute files

● s:setgid bit

● t: sticky

In our example, rw-r--r-- means that users can read and write files, while groups and others can only read files. However, the number of nine characters (rw-r--r--) after the number of characters 1 allowed after the hard link to the file.

3 、 root root 337

Two fields, root root, show the file owner and group, followed by the file size (337), in bytes. If you want to output dimensions in a readable format, you need to use the-h option. You can use the chmod command to change the owner of the file.

4 、 Oct 4 11:31

Oct 4 11:31 is the date and time when the file was last modified.

5 、 / etc/hosts

/ etc/hosts is the name of the file.

How do I show hidden files?

In Linux, hidden files are any files that begin with a dot (.); by default, the ls command does not show hidden files.

To show all files, including hidden files, you need to use the-an option:

Ls-la ~ /

Output:

Drwxr-x--- 10 linuxize linuxize 4096 Feb 12 16:28 .drwxr-xr-x 18 linuxize linuxize 4096 Dec 26 09:21.-rw- 1 linuxize linuxize 1630 Nov 18 2017 .bash _ historydrwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 bindrwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 Desktopdrwxr-xr-x 4 linuxize linuxize 4096 Dec 12 2017 .npmdrwx-2 linuxize linuxize 4096 Mar 4 2018 .ssh

We also use the-l option to display the details of all files.

How to modify the ls output order?

By default, the ls command lists files in alphabetical order, but we can also change the output order:

●-- sort=extension (or-X): sort alphabetically by entry extension

●-- sort=size (or-S): sort by file size

●-- sort=time (or-t): sort by modification time

●-- sort=version (or-v): natural type (version) numbers in text

If you want to get the results in reverse sort order, you need to use the-r option.

For example, to sort files in the / var directory by modification time in reverse sort order, use:

Ls-ltr / var

It is worth mentioning that the ls command does not show the total space occupied by the contents of the directory.

How to recursively list subdirectories?

Use the-R option to tell the ls command to display the contents of the subdirectory:

Ls-R above are all the contents of the method of listing files using the ls command in Linux. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 243

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report