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

How to implement file operation on Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to achieve file operation on Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.

List files by name

The easiest way is to use the ls command to list files by name. After all, listing files by name (in alphanumeric order) is the default. You can choose ls (no details) or ls-l (lots of details) to decide what you see.

$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

If you want to view the list one screen at a time, you can pipe the output of ls to the more command.

Arrange files in reverse order of name

To reverse the list of files by name, add the-r (reverse (Reverse)) option. It's like turning a normal list upside down.

$ls-r $ls-lr

List files by file extension

The ls command does not analyze file types by content, it only processes file names. However, there is a command option to list files by extension. If you add the-X (extension (eXtension)) option, ls will sort the files by name in each extension category. For example, it will first list files without an extension (in alphanumeric order), followed by files with extensions of .1, .bz2, .c, and so on.

List only catalogs

By default, the ls command displays both files and directories. If you want to list only directories, you can use the-d (Directory) option. You'll get a list like this:

$ls-d * / 1 / backups/ modules/ projects/ templates/ 2 / html/ patches/ public/ videos/ bin/ new/ private/ save/

Arrange files by size

If you want to list files in size order, add the-S (Size) option. Note, however, that this does not actually show the size of the file (and the details of other files) unless you also add the-l (Long listing) option. When listing files by size, it is generally helpful to see that commands are doing what you want. Note that by default, the largest file is displayed first. Add the-r option in reverse (that is, 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

List documents by owner

If you want to list files by owner (for example, in a shared directory), you can pass the output of the ls command to sort and sort by the third field by adding-K3 to pick out the owner column.

$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 any field in this way (for example, year). Just note that if you want to sort a number field, add an n, such as-k5n, otherwise you will sort it alphabetically. This sorting technique is also useful for sorting the contents of files, not just for listing files.

Arrange files by year

Use the-t (modification time (Time modified)) option to list files in chronological order-- how old and new they are. Add the-r option to make the most recently updated files appear at the end of the list. I use this alias to display a list of my recently updated files.

$alias recent='ls-ltr | tail-8'

Note that the change time and modification time of the file are different. The results of the-c (change time (time Changed)) and-t (modification time) options are not always the same. If you change the permissions of a file without changing anything else,-c will put the file at the top of the ls output, while-t will not. If you want to know the difference, look at the output of the stat command.

$stat ckacct File: ckacct Size: 200Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 829041 Links: 1 Access: (0750Mawxrwxrwxr -) Uid: (1000 / shs) Gid: (1000 / shs) Access: 2020-08-20 16Jiang 1015 11.063015008-0400 Modify: 2020-08-17 0726Rwxr 26Rwx297-0400

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: 0

*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