mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-28 01:01:09 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
b17270c7a8
@ -1,212 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (11 ways to list and sort files on Linux)
|
||||
[#]: via: (https://www.networkworld.com/article/3572590/11-ways-to-list-and-sort-files-on-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
11 ways to list and sort files on Linux
|
||||
======
|
||||
Linux commands can provide details on files and show options for customizing file listings, but can also reach as deeply into a file system as you care to look.
|
||||
Thinkstock
|
||||
|
||||
There are many ways to list files and display information about them on Linux systems. This post reviews a number of commands that provide details on files and provides options for customizing your file listings to meet your needs.
|
||||
|
||||
Most of the commands list files within individual directories while others reach as deeply into a file system as you care to look.
|
||||
|
||||
The primary command for listing files is, of course, **ls**. This command, however, has an extensive number of options for finding and listing just the files you want to see. Plus, there's always **find** for helping with very specific file searches.
|
||||
|
||||
### Listing files by name
|
||||
|
||||
The easiest way to list files by name is simply to list them using the **ls** command. Listing files by name (alphanumeric order) is, after all, the default. You can choose the **ls** (no details) or **ls -l** (lots of details) to determine your view.
|
||||
|
||||
```
|
||||
$ ls | head -6
|
||||
8pgs.pdf
|
||||
Aesthetics_Thank_You.pdf
|
||||
alien.pdf
|
||||
Annual_Meeting_Agenda-20190602.pdf
|
||||
bigfile.bz2
|
||||
bin
|
||||
$ ls -l | head -6
|
||||
-rw-rw-r-- 1 shs shs 10886 Mar 22 2019 8pgs.pdf
|
||||
-rw-rw-r-- 1 shs shs 284003 May 11 2019 Aesthetics_Thank_You.pdf
|
||||
-rw-rw-r-- 1 shs shs 38282 Jan 24 2019 alien.pdf
|
||||
-rw-rw-r-- 1 shs shs 97358 May 19 2019 Annual_Meeting_20190602.pdf
|
||||
-rw-rw-r-- 1 shs shs 18115234 Apr 16 17:36 bigfile.bz2
|
||||
drwxrwxr-x 4 shs shs 8052736 Jul 10 13:17 bin
|
||||
```
|
||||
|
||||
Pipe the output of **ls** to the **more** command if you want to review your listing a screenful at a time.
|
||||
|
||||
### Listing files in reverse name order
|
||||
|
||||
To reverse the listing of files by name, add the **-r** (reverse) option. This will be like turning the normal listing upside down.
|
||||
|
||||
```
|
||||
$ ls -r
|
||||
$ ls -lr
|
||||
```
|
||||
|
||||
### Listing files by file extension
|
||||
|
||||
The **ls** command doesn’t analyze file types by content, but works with file names. There is, however, a command option that can list files by extension. If you add the **-X** option, **ls** will sort files by name within each extension category. For example, it will list files without extensions first (in alphanumeric order) followed by files with extensions like .1, .bz2, .c and so on.
|
||||
|
||||
### Listing directories only
|
||||
|
||||
By default, the **ls** command will show both files and directories. If you want to list _only_ directories, you can use the **-d** option. You will get a listing like this one:
|
||||
|
||||
```
|
||||
$ ls -d */
|
||||
1/ backups/ modules/ projects/ templates/
|
||||
2/ html/ patches/ public/ videos/
|
||||
bin/ new/ private/ save/
|
||||
```
|
||||
|
||||
### Listing files by size
|
||||
|
||||
If you want to list files in size order, add the **-S** option. Note, however, that this won't actually show you the sizes (along with other file details) unless you also add the **-l** (long listing) option. When listing files by size, it's generally helpful to see that the command is doing what you asked. Notice that the default is to show the largest files first. Add the **-r** option to reverse this (i.e., **ls -lSr**).
|
||||
|
||||
```
|
||||
$ ls -lS
|
||||
total 959492
|
||||
-rw-rw-r-- 1 shs shs 357679381 Sep 19 2019 sav-linux-free-9.tgz
|
||||
-rw-rw-r-- 1 shs shs 103270400 Apr 16 17:38 bigfile
|
||||
-rw-rw-r-- 1 shs shs 79117862 Oct 5 2019 Nessus-8.7.1-ubuntu1110_amd64.deb
|
||||
```
|
||||
|
||||
### Listing files by owner
|
||||
|
||||
If you want to list files by owner (e.g., in a shared directory), you can pass the output of the **ls** command to **sort** and pick out the owner column by adding **-k3** to sort on the third field.
|
||||
|
||||
```
|
||||
$ ls -l | sort -k3 | more
|
||||
total 56
|
||||
-rw-rw-r-- 1 dory shs 0 Aug 23 12:27 tasklist
|
||||
drwx------ 2 gdm gdm 4096 Aug 21 17:12 tracker-extract-files.121
|
||||
srwxr-xr-x 1 root root 0 Aug 21 17:12 ntf_listenerc0c6b8b4567
|
||||
drwxr-xr-x 2 root root 4096 Aug 21 17:12 hsperfdata_root
|
||||
^
|
||||
|
|
||||
```
|
||||
|
||||
In fact, you can sort on any field this way (e.g., year). Just be careful to add an “n” – **-k5n** – if you’re sorting on a field which is numeric or you'll be sorting in alphanumeric order. This sorting technique is useful for sorting file contents as well, not just listing files.
|
||||
|
||||
### Listing files by age
|
||||
|
||||
Use the **-t** option to list files in order of age – how new they are. Add the **-r** option to get the most recently updated files showing up last in the list. I use this alias to show me a list of the files that I’ve most recently updated:
|
||||
|
||||
```
|
||||
$ alias recent='ls -ltr | tail -8'
|
||||
```
|
||||
|
||||
Note that file change and modification times are different. The **-c** (change) and **-t** (modification) options will not always give the same results. If you change permissions on a file and nothing else, **-c** will put that files at the top of the **ls** output while **-t** will not. If you’re curious about the difference, look at the output of the **stat** command.
|
||||
|
||||
```
|
||||
$ stat ckacct
|
||||
File: ckacct
|
||||
Size: 200 Blocks: 8 IO Block: 4096 regular file
|
||||
Device: 801h/2049d Inode: 829041 Links: 1
|
||||
Access: (0750/-rwxr-x---) Uid: ( 1000/ shs) Gid: ( 1000/ shs)
|
||||
Access: 2020-08-20 16:10:11.063015008 -0400
|
||||
Modify: 2020-08-17 07:26:34.579922297 -0400 <== content changes
|
||||
Change: 2020-08-24 09:36:51.699775940 -0400 <== content or permissions changes
|
||||
Birth: -
|
||||
```
|
||||
|
||||
### Listing files by group
|
||||
|
||||
To sort files by the associated groups, you can pass the output from a long listing to the **sort** command and tell it to sort on column 4.
|
||||
|
||||
```
|
||||
$ ls -l | sort -k4
|
||||
```
|
||||
|
||||
### Listing files by access date
|
||||
|
||||
To list files by access date (most recently accessed first), use the **-ltu** options. The "u" enforces the "by access date" listing order.
|
||||
|
||||
```
|
||||
$ ls -ltu
|
||||
total 959500
|
||||
-rwxr-x--- 1 shs shs 200 Aug 24 09:42 ckacct <== most recently used
|
||||
-rw-rw-r-- 1 shs shs 1335 Aug 23 17:45 lte
|
||||
```
|
||||
|
||||
### Listing multiple files on a single line
|
||||
|
||||
Sometimes a condensed file listing is more appropriate for the task at hand. The **ls** command even has options for that. To list files on as few lines as possible, you can use **\--format=comma** to separate file names with commas as in this command:
|
||||
|
||||
```
|
||||
$ ls --format=comma
|
||||
1, 10, 11, 12, 124, 13, 14, 15, 16pgs-landscape.pdf, 16pgs.pdf, 17, 18, 19,
|
||||
192.168.0.4, 2, 20, 2018-12-23_OoS_2.pdf, 2018-12-23_OoS.pdf, 20190512_OoS.pdf,
|
||||
'2019_HOHO_application working.pdf' …
|
||||
```
|
||||
|
||||
Prefer white space? Use **\--format=across** instead.
|
||||
|
||||
```
|
||||
$ ls --format=across z*
|
||||
z zip zipfiles zipfiles1.bat zipfiles2.bat
|
||||
zipfiles3.bat zipfiles4.bat zipfiles.bat zoom_amd64.deb zoomap.pdf
|
||||
zoom-mtg
|
||||
```
|
||||
|
||||
### Adding depth to your searches
|
||||
|
||||
While **ls** generally lists files in a single directory, you can choose instead to recursively list files by using the **-R** options, delving into the entire depth of directories:
|
||||
|
||||
```
|
||||
$ ls -R zzzzz | grep -v "^$"
|
||||
zzzzz:
|
||||
zzzz
|
||||
zzzzz/zzzz:
|
||||
zzz
|
||||
zzzzz/zzzz/zzz:
|
||||
zz
|
||||
zzzzz/zzzz/zzz/zz:
|
||||
z
|
||||
zzzzz/zzzz/zzz/zz/z:
|
||||
sleeping
|
||||
```
|
||||
|
||||
Alternately, you can use a **find** command with a limited or unlimited depth specification. In this command, we instruct the **find** command to look in only three levels of directories:
|
||||
|
||||
```
|
||||
$ find zzzzz -maxdepth 3
|
||||
zzzzz
|
||||
zzzzz/zzzz
|
||||
zzzzz/zzzz/zzz
|
||||
zzzzz/zzzz/zzz/zz
|
||||
```
|
||||
|
||||
### Choosing ls vs find
|
||||
|
||||
When you need to list files that meet a very selective list of requirements, the **find** command is probably a better tool to use than **ls**.
|
||||
|
||||
Unlike **ls**, the **find** command looks as deeply as possible unless you constrain it. It also has many other options and an **-exec** command that allows you to take some specific action once you find the files you’re looking for.
|
||||
|
||||
### Wrap-Up
|
||||
|
||||
The **ls** command has a lot of options for listing files. Check them out. You might find some that you’ll love.
|
||||
|
||||
Join the Network World communities on [Facebook][1] and [LinkedIn][2] to comment on topics that are top of mind.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3572590/11-ways-to-list-and-sort-files-on-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.facebook.com/NetworkWorld/
|
||||
[2]: https://www.linkedin.com/company/network-world
|
@ -1,228 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Glances – A Versatile System Monitoring Tool for Linux Systems)
|
||||
[#]: via: (https://itsfoss.com/glances/)
|
||||
[#]: author: (Chinmay https://itsfoss.com/author/chinmay/)
|
||||
|
||||
Glances – A Versatile System Monitoring Tool for Linux Systems
|
||||
======
|
||||
|
||||
The most commonly used command line [tools for process monitoring on Linux][1] are top and its colorful, feature rich cousin [htop][2] .
|
||||
|
||||
To [monitor temperature on Linux][3], you can use [lm-sensors][4]. Similarly, there are many utilities to monitor other real-time metrics such as Disk I/O, Network Stats and others.
|
||||
|
||||
[Glances][5] is a system monitoring tool which ties them all together and offers a lot more features. What I like the most is that you can use run Glances on a remote Linux server and monitor the system resources on your local system or monitor it through your web browser.
|
||||
|
||||
Here’s what it looks like. The terminal in the below screenshot has been [beautified with Pywal tool that automatically changes the color based on wallpaper][6].
|
||||
|
||||
![][7]
|
||||
|
||||
You can also integrate it to a tool like [Grafana][8] to monitor the stats in an intuitive dashboard.
|
||||
|
||||
It is written in Python which means that it can be used on most platforms with almost all features.
|
||||
|
||||
### Features of Glances
|
||||
|
||||
![Glances Data In Grafana Dashboard][9]
|
||||
|
||||
Let’s have a quick glance over the main feature Glances provides (pun intended).
|
||||
|
||||
* Can monitor 15+ metrics on a system (including docker containers).
|
||||
* Flexible usage modes: standalone, client-server, over SSH and web mode.
|
||||
* Versatile REST API and XML-RPC available for integrations.
|
||||
* Readily supports exporting data to different services and databases.
|
||||
* Highly configurable and adaptable to different needs.
|
||||
* Very comprehensive Documentation.
|
||||
|
||||
|
||||
|
||||
### Installing Glances on Ubuntu and other Linux distributions
|
||||
|
||||
Glances is available in the official repositories of many Linux distributions. This means you can use your distribution’s package manager to install it easily.
|
||||
|
||||
On Debian/Ubuntu based distributions, you can use the following command:
|
||||
|
||||
```
|
||||
sudo apt install glances
|
||||
```
|
||||
|
||||
You can also install the latest Glances using snap package:
|
||||
|
||||
```
|
||||
sudo snap install glances
|
||||
```
|
||||
|
||||
Since Glances is based on Python, you can also use PIP to install it on most Linux distributions. [Install PIP][10] first and then use it to install Glances:
|
||||
|
||||
```
|
||||
sudo pip3 install glances
|
||||
```
|
||||
|
||||
If nothing else, you can always use the auto install script provided by Glances developer. Though we at It’s FOSS don’t recommend running random scripts directly on your system. It’s entirely your decision.
|
||||
|
||||
```
|
||||
curl -L https://bit.ly/glances | /bin/bash
|
||||
```
|
||||
|
||||
You can check out other ways to install Glances from their [documentation][11], you can also install it as a docker container.
|
||||
|
||||
### Using Glances to monitor Linux system resources on local system (standalone mode)
|
||||
|
||||
You can easily launch Glances to monitor your local machine by running this command n the terminal.
|
||||
|
||||
```
|
||||
glances
|
||||
```
|
||||
|
||||
You can instantly observe that it integrates a lot of different information in one single screen. I like that it shows the public and private IP’s of the computer right at the top.
|
||||
|
||||
![][12]
|
||||
|
||||
Glances is also interactive, meaning you can use commands to interact with it while it is running. You can press **“s”** to bring up the sensors onto the screen; **“k”** to bring up the TCP connections list; **“1”** to expand the CPU stats to show individual threads.
|
||||
|
||||
You can also use the arrow keys to move around the list of processes and sort the table by different metrics.
|
||||
|
||||
You can launch Glances with various command line options. There are plenty more interactive commands as well. You can find the complete list in their [comprehensive documentation][13].
|
||||
|
||||
Press Ctrl+C to exit Glances.
|
||||
|
||||
### Using Glances to monitor remote Linux systems (Client – Server mode)
|
||||
|
||||
To monitor a remote computer you can use Glances in client-server mode. You need to have Glances installed on both systems.
|
||||
|
||||
On the remote Linux system, you’ll have to launch glances in server mode using the -s option.
|
||||
|
||||
```
|
||||
glances -s
|
||||
```
|
||||
|
||||
On the client system, use this command to launch Glances in client mode and connect to the server.
|
||||
|
||||
```
|
||||
glances -c server_ip_address
|
||||
```
|
||||
|
||||
You can also SSH into any computer and launch Glances, which works flawlessly. More information about client-server mode [here][14].
|
||||
|
||||
### Monitor Linux system resources in web browser using Glances (Web Mode)
|
||||
|
||||
Glances can also run in web mode. This means that you can use a web browser to access Glances. Unlike the previous client-server mode, you don’t need to install Glances on the client system.
|
||||
|
||||
To start Glances in web mode, use the -w option.
|
||||
|
||||
```
|
||||
glances -w
|
||||
```
|
||||
|
||||
Keep in mind that it may display “Glances Web User Interface started on <http://0.0.0.0:61208”> even on a Linux server when it is actually using the server’s IP address.
|
||||
|
||||
The main part is that it uses 61208 port number and you can use it to access Glances via web browser. Just type the server’s IP address followed by the port number like <http://123.123.123.123:61208>.
|
||||
|
||||
You can also use it on the local system using <http://0.0.0.0:61208/> or <https://localhost:61208/>.
|
||||
|
||||
![][15]
|
||||
|
||||
The web mode also emulates the terminal look. The web version is built with responsive design principles in mind and looks good even on phones.
|
||||
|
||||
You may want to protect the web mode with a password so that only authorized people could use it. The default username is glances.
|
||||
|
||||
```
|
||||
[email protected]:~# glances -w --password
|
||||
Define the Glances webserver password (glances username):
|
||||
Password (confirm):
|
||||
Do you want to save the password? [Yes/No]: n
|
||||
Glances Web User Interface started on http://0.0.0.0:61208/
|
||||
```
|
||||
|
||||
You can find more information on configuring password in the [quickstart guide][16].
|
||||
|
||||
### Export Glances data to different services
|
||||
|
||||
One of the biggest advantage of using Glances is the out of the box support to export data to various databases, services and integration into various data pipelines seamlessly.
|
||||
|
||||
You can export to CSV while monitoring with this command.
|
||||
|
||||
```
|
||||
glances --export csv --export-csv-file /tmp/glances.csv
|
||||
```
|
||||
|
||||
‘/tmp/glances.csv’ is the location of the file. The data is filled in neatly as time series.
|
||||
|
||||
![][17]
|
||||
|
||||
You can export to powerful applications like [Prometheus][18] to enable conditional triggers and notifications.
|
||||
|
||||
It can directly plug into messaging services like RabbitMQ, MQTT, streaming platforms like Kafka and export time series data to databases like InfluxDB and visualize using Grafana.
|
||||
|
||||
You can check out the whole list of services and export options [here][19].
|
||||
|
||||
### Use REST API to integrate Glances with other services
|
||||
|
||||
This is my favorite feature in the whole stack. Glances not only brings together various metrics together, it also exposes them via APIs.
|
||||
|
||||
This simple yet powerful feature makes it very easy to build custom applications, services and middleware applications for any specific use cases.
|
||||
|
||||
REST API server stars automatically when you launch Glances in web mode. To launch it in API server only mode you can use the following command.
|
||||
|
||||
```
|
||||
glances -w --disable-webui
|
||||
```
|
||||
|
||||
The [REST API][20] documentation is comprehensive and the responses are easy to integrate with a web application. This makes it easy to build a unified dashboard to monitor multiple servers with a tool like [Node-RED][21].
|
||||
|
||||
![][22]
|
||||
|
||||
Glances also provides an XML-RPC server, you can check out the documentation [here][23].
|
||||
|
||||
### Closing thoughts on Glances
|
||||
|
||||
Glances uses the [psutil][24] python library to access different system stats. I had built a simple API server using the same library to retrieve CPU usage back in 2017. I was able to monitor all the Raspberry Pi’s in a cluster using a dashboard built with Node-RED.
|
||||
|
||||
Glances could have saved me some time while offering way more features, unfortunately I didn’t know about it back then.
|
||||
|
||||
I did try to install Glances on my Raspberry Pi’s while writing this article, unfortunately it failed with some errors with all the installation methods. I’ll update the article when I succeed or maybe write another article with steps to install on a Raspberry Pi.
|
||||
|
||||
I wish Glances offered a way to kill processes like top or htop. Let’s hope that we get it in upcoming releases.
|
||||
|
||||
I hope this gave you a good deal of information on Glances. What system monitoring tools do you guys use? let me know in the comments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/glances/
|
||||
|
||||
作者:[Chinmay][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/chinmay/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://itsfoss.com/linux-system-monitoring-tools/
|
||||
[2]: https://hisham.hm/htop/
|
||||
[3]: https://itsfoss.com/monitor-cpu-gpu-temp-linux/
|
||||
[4]: https://github.com/lm-sensors/lm-sensors
|
||||
[5]: https://nicolargo.github.io/glances/
|
||||
[6]: https://itsfoss.com/pywal/
|
||||
[7]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/glances-linux.png?resize=800%2C510&ssl=1
|
||||
[8]: https://grafana.com/
|
||||
[9]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/08/glances-data-in-grafana-dashboard.jpg?resize=800%2C472&ssl=1
|
||||
[10]: https://itsfoss.com/install-pip-ubuntu/
|
||||
[11]: https://github.com/nicolargo/glances/blob/master/README.rst#installation
|
||||
[12]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-11-54-18.png?resize=800%2C503&ssl=1
|
||||
[13]: https://glances.readthedocs.io/en/latest/cmds.html
|
||||
[14]: https://glances.readthedocs.io/en/latest/quickstart.html#central-client
|
||||
[15]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-16-49-11.png?resize=800%2C471&ssl=1
|
||||
[16]: https://glances.readthedocs.io/en/stable/quickstart.html
|
||||
[17]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-12-25-40.png?resize=800%2C448&ssl=1
|
||||
[18]: https://prometheus.io/
|
||||
[19]: https://glances.readthedocs.io/en/latest/gw/index.html
|
||||
[20]: https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API
|
||||
[21]: https://nodered.org/
|
||||
[22]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-17-49-41.png?resize=800%2C468&ssl=1
|
||||
[23]: https://github.com/nicolargo/glances/wiki
|
||||
[24]: https://pypi.org/project/psutil/
|
@ -0,0 +1,210 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (11 ways to list and sort files on Linux)
|
||||
[#]: via: (https://www.networkworld.com/article/3572590/11-ways-to-list-and-sort-files-on-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
把 Linux 上的文件列表和排序玩出花来
|
||||
======
|
||||
|
||||
> Linux 命令可以提供文件的详细信息,也可以自定义显示的文件列表,甚至可以深入到文件系统的目录中,只要你愿意看。
|
||||
|
||||
在 Linux 系统上,有许多方法可以列出文件并显示它们的信息。这篇文章回顾了一些提供文件细节的命令,并提供了自定义文件列表的选项,以满足你的需求。
|
||||
|
||||
大多数命令都会列出单个目录中的文件,而其他命令则可以深入到文件系统的目录中,只要你愿意看。
|
||||
|
||||
当然,最主要的文件列表命令是 `ls`。然而,这个命令有大量的选项,可以只查找和列出你想看的文件。另外,还有 `find` 可以帮助你进行非常具体的文件搜索。
|
||||
|
||||
### 按名称列出文件
|
||||
|
||||
最简单的方法是使用 `ls` 命令按名称列出文件。毕竟,按名称(字母数字顺序)列出文件是默认的。你可以选择 `ls`(无细节)或 `ls -l`(大量细节)来决定你看到什么。
|
||||
|
||||
```
|
||||
$ ls | head -6
|
||||
8pgs.pdf
|
||||
Aesthetics_Thank_You.pdf
|
||||
alien.pdf
|
||||
Annual_Meeting_Agenda-20190602.pdf
|
||||
bigfile.bz2
|
||||
bin
|
||||
$ ls -l | head -6
|
||||
-rw-rw-r-- 1 shs shs 10886 Mar 22 2019 8pgs.pdf
|
||||
-rw-rw-r-- 1 shs shs 284003 May 11 2019 Aesthetics_Thank_You.pdf
|
||||
-rw-rw-r-- 1 shs shs 38282 Jan 24 2019 alien.pdf
|
||||
-rw-rw-r-- 1 shs shs 97358 May 19 2019 Annual_Meeting_20190602.pdf
|
||||
-rw-rw-r-- 1 shs shs 18115234 Apr 16 17:36 bigfile.bz2
|
||||
drwxrwxr-x 4 shs shs 8052736 Jul 10 13:17 bin
|
||||
```
|
||||
|
||||
如果你想一次查看一屏的列表,可以将 `ls` 的输出用管道送到 `more` 命令中。
|
||||
|
||||
### 按相反的名字顺序排列文件
|
||||
|
||||
要按名称反转文件列表,请添加 `-r`(<ruby>反转<rt>Reverse</rt></ruby>)选项。这就像把正常的列表倒过来一样。
|
||||
|
||||
```
|
||||
$ ls -r
|
||||
$ ls -lr
|
||||
```
|
||||
|
||||
### 按文件扩展名列出文件
|
||||
|
||||
`ls` 命令不会按内容分析文件类型,它只会处理文件名。不过,有一个命令选项可以按扩展名列出文件。如果你添加了 `-X` (<ruby>扩展名<rt>eXtension</rt></ruby>)选项,`ls` 将在每个扩展名类别中按名称对文件进行排序。例如,它将首先列出没有扩展名的文件(按字母数字顺序),然后是扩展名为 `.1`、`.bz2`、`.c` 等的文件。
|
||||
|
||||
### 只列出目录
|
||||
|
||||
默认情况下,`ls` 命令将同时显示文件和目录。如果你想只列出目录,你可以使用 `-d`(<ruby>目录<rt>Directory</rt></ruby>)选项。你会得到一个像这样的列表:
|
||||
|
||||
```
|
||||
$ ls -d */
|
||||
1/ backups/ modules/ projects/ templates/
|
||||
2/ html/ patches/ public/ videos/
|
||||
bin/ new/ private/ save/
|
||||
```
|
||||
|
||||
### 按大小排列文件
|
||||
|
||||
如果你想按大小顺序列出文件,请添加 `-S`(<ruby>大小<rt>Size</rt></ruby>)选项。但请注意,这实际上不会显示文件的大小(以及其他文件的细节),除非你还添加 `-l`(<ruby>长列表<rt>Long listing</rt></ruby>)选项。当按大小列出文件时,一般来说,看到命令在按你的要求做事情是很有帮助的。注意,默认情况下是先显示最大的文件。添加 `-r` 选项可以反过来(即 `ls -lSr`)。
|
||||
|
||||
```
|
||||
$ ls -lS
|
||||
total 959492
|
||||
-rw-rw-r-- 1 shs shs 357679381 Sep 19 2019 sav-linux-free-9.tgz
|
||||
-rw-rw-r-- 1 shs shs 103270400 Apr 16 17:38 bigfile
|
||||
-rw-rw-r-- 1 shs shs 79117862 Oct 5 2019 Nessus-8.7.1-ubuntu1110_amd64.deb
|
||||
```
|
||||
|
||||
### 按属主列出文件
|
||||
|
||||
如果你想按属主列出文件(例如,在一个共享目录中),你可以把 `ls` 命令的输出传给 `sort`,并通过添加 `-k3` 来按第三个字段排序,从而挑出属主一栏。
|
||||
|
||||
```
|
||||
$ ls -l | sort -k3 | more
|
||||
total 56
|
||||
-rw-rw-r-- 1 dory shs 0 Aug 23 12:27 tasklist
|
||||
drwx------ 2 gdm gdm 4096 Aug 21 17:12 tracker-extract-files.121
|
||||
srwxr-xr-x 1 root root 0 Aug 21 17:12 ntf_listenerc0c6b8b4567
|
||||
drwxr-xr-x 2 root root 4096 Aug 21 17:12 hsperfdata_root
|
||||
^
|
||||
|
|
||||
```
|
||||
|
||||
事实上,你可以用这种方式对任何字段进行排序(例如,年份)。只是要注意,如果你要对一个数字字段进行排序,则要加上一个 `n`,如 `-k5n`,否则你将按字母数字顺序进行排序。这种排序技术对于文件内容的排序也很有用,而不仅仅是用于列出文件。
|
||||
|
||||
### 按年份排列文件
|
||||
|
||||
使用 `-t`(<ruby>修改时间<rt>Time modified</rt></ruby>)选项按年份顺序列出文件 —— 它们的新旧程度。添加 `-r` 选项,让最近更新的文件在列表中最后显示。我使用这个别名来显示我最近更新的文件列表。
|
||||
|
||||
```
|
||||
$ alias recent='ls -ltr | tail -8'
|
||||
```
|
||||
|
||||
请注意,文件的更改时间和修改时间是不同的。`-c`(<ruby>更改时间<rt>time Changed</rt></ruby>)和 `-t`(修改时间)选项的结果并不总是相同。如果你改变了一个文件的权限,而没有改变其他内容,`-c` 会把这个文件放在 `ls` 输出的顶部,而 `-t` 则不会。如果你想知道其中的区别,可以看看 `stat` 命令的输出。
|
||||
|
||||
```
|
||||
$ stat ckacct
|
||||
File: ckacct
|
||||
Size: 200 Blocks: 8 IO Block: 4096 regular file
|
||||
Device: 801h/2049d Inode: 829041 Links: 1
|
||||
Access: (0750/-rwxr-x---) Uid: ( 1000/ shs) Gid: ( 1000/ shs)
|
||||
Access: 2020-08-20 16:10:11.063015008 -0400
|
||||
Modify: 2020-08-17 07:26:34.579922297 -0400 <== content changes
|
||||
Change: 2020-08-24 09:36:51.699775940 -0400 <== content or permissions changes
|
||||
Birth: -
|
||||
```
|
||||
|
||||
### 按组别列出文件
|
||||
|
||||
要按关联的组别对文件进行排序,你可以将一个长列表的输出传给 `sort` 命令,并告诉它在第 4 列进行排序。
|
||||
|
||||
```
|
||||
$ ls -l | sort -k4
|
||||
```
|
||||
|
||||
### 按访问日期列出文件
|
||||
|
||||
要按访问日期(最近访问的日期在前)列出文件,使用 `-ltu` 选项。`u` 强制“按访问日期”排列顺序。
|
||||
|
||||
```
|
||||
$ ls -ltu
|
||||
total 959500
|
||||
-rwxr-x--- 1 shs shs 200 Aug 24 09:42 ckacct <== most recently used
|
||||
-rw-rw-r-- 1 shs shs 1335 Aug 23 17:45 lte
|
||||
```
|
||||
|
||||
### 单行列出多个文件
|
||||
|
||||
有时,精简的文件列表更适合手头的任务。`ls` 命令甚至有这方面的选项。为了在尽可能少的行上列出文件,你可以使用 `--format=comma` 来用逗号分隔文件名,就像这个命令一样:
|
||||
|
||||
```
|
||||
$ ls --format=comma
|
||||
1, 10, 11, 12, 124, 13, 14, 15, 16pgs-landscape.pdf, 16pgs.pdf, 17, 18, 19,
|
||||
192.168.0.4, 2, 20, 2018-12-23_OoS_2.pdf, 2018-12-23_OoS.pdf, 20190512_OoS.pdf,
|
||||
'2019_HOHO_application working.pdf' …
|
||||
```
|
||||
|
||||
喜欢用空格?使用 `--format=across` 代替。
|
||||
|
||||
```
|
||||
$ ls --format=across z*
|
||||
z zip zipfiles zipfiles1.bat zipfiles2.bat
|
||||
zipfiles3.bat zipfiles4.bat zipfiles.bat zoom_amd64.deb zoomap.pdf
|
||||
zoom-mtg
|
||||
```
|
||||
|
||||
### 增加搜索的深度
|
||||
|
||||
虽然 `ls` 一般只列出单个目录中的文件,但你可以选择使用 `-R` 选项(<ruby>递归<rt>Recursively</rt></ruby>)地列出文件,深入到整个目录的深处。
|
||||
|
||||
```
|
||||
$ ls -R zzzzz | grep -v "^$"
|
||||
zzzzz:
|
||||
zzzz
|
||||
zzzzz/zzzz:
|
||||
zzz
|
||||
zzzzz/zzzz/zzz:
|
||||
zz
|
||||
zzzzz/zzzz/zzz/zz:
|
||||
z
|
||||
zzzzz/zzzz/zzz/zz/z:
|
||||
sleeping
|
||||
```
|
||||
|
||||
另外,你也可以使用 `find` 命令,对深度进行限制或不限制。在这个命令中,我们指示 `find` 命令只在三个层次的目录中查找:
|
||||
|
||||
```
|
||||
$ find zzzzz -maxdepth 3
|
||||
zzzzz
|
||||
zzzzz/zzzz
|
||||
zzzzz/zzzz/zzz
|
||||
zzzzz/zzzz/zzz/zz
|
||||
```
|
||||
|
||||
### 选择 ls 还是 find
|
||||
|
||||
当你需要列出符合具体要求的文件时,`find` 命令可能是比 `ls` 更好的工具。
|
||||
|
||||
与 `ls` 不同的是,`find` 命令会尽可能地深入查找,除非你限制它。它还有许多其他选项和一个 `-exec` 子命令,允许在找到你要找的文件后采取一些特定的行动。
|
||||
|
||||
### 总结
|
||||
|
||||
`ls` 命令有很多用于列出文件的选项。了解一下它们。你可能会发现一些你会喜欢的选项。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3572590/11-ways-to-list-and-sort-files-on-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.facebook.com/NetworkWorld/
|
||||
[2]: https://www.linkedin.com/company/network-world
|
@ -0,0 +1,226 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Glances – A Versatile System Monitoring Tool for Linux Systems)
|
||||
[#]: via: (https://itsfoss.com/glances/)
|
||||
[#]: author: (Chinmay https://itsfoss.com/author/chinmay/)
|
||||
|
||||
Glances:多功能 Linux 系统监控工具
|
||||
======
|
||||
|
||||
Linux 上最常用的[命令行进程监控工具][1]是 `top` 和它那色彩斑斓、功能丰富的表弟 [htop][2]。
|
||||
|
||||
要[监控 Linux 上的温度][3],可以使用 [lm-sensors][4]。同样,还有很多实用工具可以监控其他实时指标,如磁盘 I/O、网络统计等。
|
||||
|
||||
[Glances][5] 是一个系统监控工具,它把这些都联系在一起,并提供了更多的功能。我最喜欢的是,你可以在远程 Linux 服务器上运行 Glances 来监控本地系统的系统资源,也可以通过 Web 浏览器监控。
|
||||
|
||||
下面是它的外观。下面截图中的终端已经[用 Pywal 工具美化过,可以根据壁纸自动改变颜色][6]。
|
||||
|
||||
![][7]
|
||||
|
||||
你也可以将它集成到像 [Grafana][8] 这样的工具中,在一个直观的仪表盘中监控统计数据。
|
||||
|
||||
它是用 Python 编写的,这意味着它的绝大多数功能都可以在大多数平台上使用。
|
||||
|
||||
### Glances 的功能
|
||||
|
||||
![Glances Data In Grafana Dashboard][9]
|
||||
|
||||
让我们快速浏览一下 Glances 提供的主要功能:
|
||||
|
||||
* 可以监控系统上的 15 个之多的指标(包括 Docker 容器)。
|
||||
* 灵活的使用模式:单机模式、客户端-服务器模式、通过 SSH 和 Web 模式。
|
||||
* 可用于集成的各种 REST API 和 XML-RPC API。
|
||||
* 支持将数据轻松导出到不同的服务和数据库。
|
||||
* 高度的可配置性和适应不同的需求。
|
||||
* 非常全面的文档。
|
||||
|
||||
### 在 Ubuntu 和其他 Linux 发行版上安装 Glances
|
||||
|
||||
Glances 在许多 Linux 发行版的官方软件库中都有。这意味着你可以使用你的发行版的软件包管理器来轻松安装它。
|
||||
|
||||
在基于 Debian/Ubuntu 的发行版上,你可以使用以下命令:
|
||||
|
||||
```
|
||||
sudo apt install glances
|
||||
```
|
||||
|
||||
你也可以使用 snap 包安装最新的 Glances:
|
||||
|
||||
```
|
||||
sudo snap install glances
|
||||
```
|
||||
|
||||
由于 Glances 是基于 Python 的,你也可以使用 PIP 在大多数 Linux 发行版上安装它。先[安装 PIP][10],然后用它来安装 Glances:
|
||||
|
||||
```
|
||||
sudo pip3 install glances
|
||||
```
|
||||
|
||||
如果没有别的办法,你还可以使用 Glances 开发者提供的自动安装脚本。虽然我们不建议直接在你的系统上随便运行脚本,但这完全取决于你自己:
|
||||
|
||||
```
|
||||
curl -L https://bit.ly/glances | /bin/bash
|
||||
```
|
||||
|
||||
你可以从他们的[文档][11]中查看其他安装 Glances 的方法,甚至你还可以把它作为一个 Docker 容器来安装。
|
||||
|
||||
### 使用 Glances 监控本地系统上的 Linux 系统资源(独立模式)
|
||||
|
||||
你可以通过在终端上运行这个命令,轻松启动 Glances 来监控你的本地机器:
|
||||
|
||||
```
|
||||
glances
|
||||
```
|
||||
|
||||
你可以立即观察到,它将很多不同的信息整合在一个屏幕上。我喜欢它在顶部显示电脑的公共和私人 IP:
|
||||
|
||||
![][12]
|
||||
|
||||
Glances 也是交互式的,这意味着你可以在它运行时使用命令与它互动。你可以按 `s` 将传感器显示在屏幕上;按 `k` 将 TCP 连接列表显示在屏幕上;按 `1` 将 CPU 统计扩展到显示单个线程。
|
||||
|
||||
你也可以使用方向键在进程列表中移动,并按不同的指标对表格进行排序。
|
||||
|
||||
你可以通过各种命令行选项来启动 Glances。此外,它还有很多交互式命令。你可以在他们的[丰富的文档][13]中找到完整的列表。
|
||||
|
||||
按 `Ctrl+C` 键退出 Glances。
|
||||
|
||||
### 使用 Glances 监控远程 Linux 系统(客户端-服务器模式)
|
||||
|
||||
要监控远程计算机,你可以在客户端-服务器模式下使用 Glances。你需要在两个系统上都安装 Glances。
|
||||
|
||||
在远程 Linux 系统上,使用 `-s` 选项在服务器模式下启动 Glances:
|
||||
|
||||
```
|
||||
glances -s
|
||||
```
|
||||
|
||||
在客户端系统中,使用下面的命令在客户端模式下启动 Glances 并连接到服务器:
|
||||
|
||||
```
|
||||
glances -c server_ip_address
|
||||
```
|
||||
|
||||
你也可以通过 SSH 进入任何一台电脑,然后启动 Glances,它可以完美地工作。更多关于客户端-服务器模式的信息请看[这里][14]。
|
||||
|
||||
### 使用 Glances 在 Web 浏览器中监控 Linux 系统资源(Web 模式)
|
||||
|
||||
Glances 也可以在 Web 模式下运行。这意味着你可以使用 Web 浏览器来访问 Glances。与之前的客户端-服务器模式不同,你不需要在客户端系统上安装 Glances。
|
||||
|
||||
要在 Web 模式下启动 Glances,请使用 `-w` 选项:
|
||||
|
||||
```
|
||||
glances -w
|
||||
```
|
||||
|
||||
请注意,即使在 Linux 服务器上,它也可能显示 “Glances Web User Interface started on http://0.0.0.0:61208”,而实际上它使用的是服务器的 IP 地址。
|
||||
|
||||
最主要的是它使用的是 61208 端口号,你可以用它来通过网络浏览器访问 Glances。只要在服务器的 IP 地址后面输入端口号,比如 <http://123.123.123.123:61208>。
|
||||
|
||||
你也可以在本地系统中使用 <http://0.0.0.0:61208/> 或 <https://localhost:61208/> 访问。
|
||||
|
||||
![][15]
|
||||
|
||||
Web 模式也模仿终端的样子。网页版是根据响应式设计原则打造的,即使在手机上也很好看。
|
||||
|
||||
你可能想用密码来保护 Web 模式,这样只有授权的人才能使用它。默认的用户名是 `glances`。
|
||||
|
||||
```
|
||||
root@localhost:~# glances -w --password
|
||||
Define the Glances webserver password (glances username):
|
||||
Password (confirm):
|
||||
Do you want to save the password? [Yes/No]: n
|
||||
Glances Web User Interface started on http://0.0.0.0:61208/
|
||||
```
|
||||
|
||||
你可以在[快速入门指南][16]中找到关于配置密码的更多信息。
|
||||
|
||||
### 导出 Glances 数据到不同的服务
|
||||
|
||||
使用 Glances 最大的优势之一就是开箱即用,它支持将数据导出到各种数据库、服务,并无缝集成到各种数据管道中。
|
||||
|
||||
你可以在监控的同时用这个命令导出到 CSV:
|
||||
|
||||
```
|
||||
glances --export csv --export-csv-file /tmp/glances.csv
|
||||
```
|
||||
|
||||
`/tmp/glances.csv` 是文件的位置。数据以时间序列的形式整齐地填入。
|
||||
|
||||
![][17]
|
||||
|
||||
你也可以导出到其它大型应用程序,如 [Prometheus][18],以启用条件触发器和通知。
|
||||
|
||||
它可以直接插入到消息服务(如 RabbitMQ、MQTT)、流媒体平台(如 Kafka),并将时间序列数据导出到数据库(如 InfluxDB),并使用 Grafana 进行可视化。
|
||||
|
||||
你可以在[这里][19]查看服务和导出选项的整个列表。
|
||||
|
||||
### 使用 REST API 将 Glances 与其他服务进行整合
|
||||
|
||||
这是整个栈中我最喜欢的功能。Glances 不仅可以将各种指标汇集在一起,还可以通过 API 将它们暴露出来。
|
||||
|
||||
这个简单而强大的功能使得为任何特定的用例构建自定义应用程序、服务和中间件应用程序变得非常容易。
|
||||
|
||||
当你在 Web 模式下启动 Glances 时,REST API 服务器会自动启动。要在 API 服务器模式下启动它,你可以使用以下命令:
|
||||
|
||||
```
|
||||
glances -w --disable-webui
|
||||
```
|
||||
|
||||
[REST API][20] 的文档很全面,其响应也很容易与 Web 应用集成。这使得使用类似 [Node-RED][21] 这样的工具可以很容易地构建一个统一的仪表盘来监控多个服务器。
|
||||
|
||||
![][22]
|
||||
|
||||
Glances 也提供了一个 XML-RPC 服务器,你可以在[这里][23]查看文档。
|
||||
|
||||
### 关于 Glances 的结束语
|
||||
|
||||
Glances 使用 [psutil][24] Python 库来访问不同的系统统计数据。早在 2017 年,我就曾使用相同的库构建了一个简单的 API 服务器来检索 CPU 的使用情况。我能够使用 Node-RED 构建的仪表盘监控一个集群中的所有树莓派。
|
||||
|
||||
Glances 可以为我节省一些时间,同时提供更多的功能,可惜我当时并不知道它。
|
||||
|
||||
在写这篇文章的时候,我确实尝试着在我的树莓派上安装 Glances,可惜所有的安装方法都出现了一些错误,失败了。当我成功后,我会更新文章,或者可能再写一篇文章,介绍在树莓派上安装的步骤。
|
||||
|
||||
我希望 Glances 能提供一种顶替 `top` 或 `htop` 等的方法。让我们希望在即将到来的版本中得到它。
|
||||
|
||||
我希望这能给你提供大量关于 Glances 的信息。你们使用什么系统监控工具呢,请在评论中告诉我。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/glances/
|
||||
|
||||
作者:[Chinmay][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/chinmay/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://itsfoss.com/linux-system-monitoring-tools/
|
||||
[2]: https://hisham.hm/htop/
|
||||
[3]: https://itsfoss.com/monitor-cpu-gpu-temp-linux/
|
||||
[4]: https://github.com/lm-sensors/lm-sensors
|
||||
[5]: https://nicolargo.github.io/glances/
|
||||
[6]: https://itsfoss.com/pywal/
|
||||
[7]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/glances-linux.png?resize=800%2C510&ssl=1
|
||||
[8]: https://grafana.com/
|
||||
[9]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/08/glances-data-in-grafana-dashboard.jpg?resize=800%2C472&ssl=1
|
||||
[10]: https://itsfoss.com/install-pip-ubuntu/
|
||||
[11]: https://github.com/nicolargo/glances/blob/master/README.rst#installation
|
||||
[12]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-11-54-18.png?resize=800%2C503&ssl=1
|
||||
[13]: https://glances.readthedocs.io/en/latest/cmds.html
|
||||
[14]: https://glances.readthedocs.io/en/latest/quickstart.html#central-client
|
||||
[15]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-16-49-11.png?resize=800%2C471&ssl=1
|
||||
[16]: https://glances.readthedocs.io/en/stable/quickstart.html
|
||||
[17]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-12-25-40.png?resize=800%2C448&ssl=1
|
||||
[18]: https://prometheus.io/
|
||||
[19]: https://glances.readthedocs.io/en/latest/gw/index.html
|
||||
[20]: https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API
|
||||
[21]: https://nodered.org/
|
||||
[22]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/08/Screenshot-from-2020-08-13-17-49-41.png?resize=800%2C468&ssl=1
|
||||
[23]: https://github.com/nicolargo/glances/wiki
|
||||
[24]: https://pypi.org/project/psutil/
|
Loading…
Reference in New Issue
Block a user