linux: ls command general usage

environment:

  • centos7.6
  • ubuntu21

General introduction to the ls command:

-i: 显示inode号
-a: 显示所有内容
-A: 也是显示所有内容,但不显示 '.' 和 '..'
-d: 仅显示目录
-l: 显示详情
-h: 文件大小以易读方式显示
-F: 添加后缀以区分不同的类型,如: '/' 表示目录,‘*'表示可执行文件等
--time-style: 指定时间输出格式,如: --time-style='+%Y-%m-%d %H:%M:%S',或: --time-style=long-iso

//排序相关
-S: 以文件大小倒叙排序
-t: 以时间倒叙排序
-r: 结合 -S 和 -t 反过来排序
-X: 以后缀名排序
--sort: 指定根据什么排序,如: --sort=t相当于-t, --sort=s相当于-S等
		有效的参数为:
		  - "none"
		  - "time"
		  - "size"
		  - "extension"
		  - "version"

--time: 使用的时间格式,如: --time=ctime,--time=atime,不同的linux支持的格式不同,比如: ext4文件系统支持创建时间,那么还可以有: --time=birth

	jackletter@ubuntu:~$ ls --time=1
	ls: invalid argument ‘1’ for ‘--time’
	Valid arguments are:
	  - ‘atime’, ‘access’, ‘use’
	  - ‘ctime’, ‘status’
	  - ‘birth’, ‘creation’
	Try 'ls --help' for more information.
	 如果是centos7,那么将只有前两行

1. Briefly list the content:

insert image description here

2. List the details

We found it too rudimentary to just list the names above, so we wanted to list as much information as possible:

insert image description here

Actually, llthe command is an alias:
insert image description here

3. List all files

Although the details are listed above, some files are not listed, for example: "Hidden files":
insert image description here
Now the hidden files are also displayed!

4. Display human-readable file sizes

The above shows that the size of the file or directory is B, can it be KB/MBdisplayed?

insert image description here
As you can see, the file size is also easier to read.

5. Display different file types

There is another problem. Although the above files are distinguished by different colors, they still cannot be distinguished very well. What should I do?

insert image description here
Can it be distinguished now 文件, 目录or is it an executable file?

  • A directory ending with '/'
  • Executable files end with '*'
  • Others are various other files

6. Display time in human-readable format

The time displayed above does not conform to the habits of our people. We hope to 2022-10-05 14:04:01display it in a format. What should I do?

insert image description here

7. Only show folders

What if we only want to see the files in this directory, not the subdirectories?
Or, do we just want to watch files and not subdirectories?
insert image description here
In fact, this command is to filter the previous output. As long as there is a '/', it is considered a directory, which is generally accurate.
insert image description here
This command is similar to the one above, but because -vthe parameter is used to do reverse filtering, the output is non-directory.

8. Arrange by size

insert image description here
What if they are arranged in positive order of size?
insert image description here

9. Listed by access time

What if you want to use -t
insert image description here
from small to large?

insert image description here

10. Hide listed .and..

We need to areplaceA
insert image description here

Summarize:

The commonly used options are listed above, and we can make several aliases for them for easy calling:

# 显示inode节点、全部文件(除了'.'和'..')、大小和时间格式化输出,不同的文件类型格式化输出
alias ls-info="ls -iAlhF --time-style='+%Y-%m-%d %H:%M:%S'"

# 按修改时间倒序排列
alias ls-info-t-des="ls -iAlhFt --time-style='+%Y-%m-%d %H:%M:%S'"

# 按修改时间正序排列
alias ls-info-t="ls -iAlhFtr --time-style='+%Y-%m-%d %H:%M:%S'"

# 按文件大小正序排列
alias ls-info-s="ls -iAlhFSr --time-style='+%Y-%m-%d %H:%M:%S'"

# 按文件大小倒序排列
alias ls-info-s-des="ls -iAlhFS --time-style='+%Y-%m-%d %H:%M:%S'"

In order to facilitate permanent use, it can be written in the user's .bashrc:

insert image description here
Use without logging out: source ~/.bashrcmake it take effect

Attached alias:

alias x="y": create an alias
unalias x: cancel alias
alias: view all aliases of the established number
alias x: view the real command of x

Attached file time:

Before ext4, files had three kinds of time:

  • atime: access time, the time when the file content was last read
  • mtime: modification time, that is, the time when the content of the file was last modified
  • ctime: change status time, that is, the time when the file attributes, such as permissions, were last modified.

Added creation time after ext4, crtime

If you are on centos7, then stat file1 will have:
insert image description here
not displayed on centos7, it is because the xfs file system it uses does not support it.
insert image description here

If you are on ubuntu21, then stat file1 will have:

insert image description here

Although the time zone display is wrong, it does not affect the problem we are talking about here. . .

Guess you like

Origin blog.csdn.net/u010476739/article/details/127126986