2020年2月2日 星期日

Ls Command in Linux (List Files and Directories)

Ls Command in Linux (List Files and Directories)


ls -l                         use a long listing format

Long Listing Format

The default output of the ls command shows only the names of the files and directories, which is not very informative.
The -l ( lowercase L) option causes ls to print files in a long listing format.
When the long listing format is used, the ls command will display 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
Consider the following 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 our example, the first character is - which indicates a regular file. Values for other file types are as follows:
  • - - Regular file
  • b - Block special file
  • c - Character special file
  • d - Directory
  • l - Symbolic link
  • n - Network file
  • p - FIFO
  • s - 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 chmod command. The permission character can take the following value:
  • r - Permission to read the file
  • w - Permission to write to the file
  • x - Permission to execute the file
  • s - setgid bit
  • t - sticky bit
In our example, rw-r--r-- means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters is the number of hard links to this file.
The next two fields root root are showing the file owner and the group, followed by the size of the file (337), shown in bytes. Use the -h option if you want to print sizes in a human-readable format. You can change the file owner using the chown command.
Oct 4 11:31 is the last file modification date and time.
The last column is the name of the file.

Show Hidden Files

By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (.).
To display all files including the hidden files use the -a option:
ls -la ~/
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_history
drwxr-xr-x  2 linuxize  linuxize  4096 Jul 20  2018  bin
drwxr-xr-x  2 linuxize  linuxize  4096 Jul 20  2018  Desktop
drwxr-xr-x  4 linuxize  linuxize  4096 Dec 12  2017 .npm
drwx------  2 linuxize  linuxize  4096 Mar  4  2018 .ssh

沒有留言:

張貼留言