一、前沿

目前遇到的Linux命令越来越多,自己打算每周学会三个Linux命令,并作记录。

二、学习记录

2.1、前提

学习每一个命令之前,都需要用 man 命令 看一下都有什么功能
1、输入命令:

man ls

2、结果如下:

LS(1)                                                        User Commands                                                        LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the  FILEs  (the  current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor
       --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters

       --block-size=SIZE
              scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE  for‐
              mat below

       -B, --ignore-backups
              do not list implied entries ending with ~

       -c     with  -lt:  sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort
              by name; otherwise: sort by ctime, newest first

       -C     list entries by columns

       --color[=WHEN]
              colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never'; more info below

       -d, --directory
              list directories themselves, not their contents

       -D, --dired
              generate output designed for Emacs' dired mode

       -f     do not sort, enable -aU, disable -ls --color

       -F, --classify
              append indicator (one of */=>@|) to entries

       --file-type
              likewise, except do not append '*'

       --format=WORD
              across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C

       --full-time
              like -l --time-style=full-iso

       -g     like -l, but do not list owner

3、解释如下:
在这里插入图片描述

2.2、查看当前目录下文件

1、输入命令

ls -l

2、结果

total 16  # 显示总共大小,单位kb
drwxrwxr-x  4 xxx xxx 4096 Oct 27 17:47 ./
drwxr-xr-x 32 xxx xxx 4096 Oct 27 16:12 ../  
-rw-rw-r--  1 xxx xxx    0 Oct 27 17:47 55.txt # 以 - 开头的代表文件
drwxrwxr-x  3 xxx xxx 4096 Oct 27 17:48 66/  # 以 d 开头的代表目录
drwxrwxr-x  2 xxx xxx 4096 Oct 27 17:47 77/
结论:
	以 - 开头的代表文件、以 d 开头的代表目录
	终端输出的结果是一行一行的字符,每一行字符对应一个目录或者是文件
	如果是文件的话,该行的字符串信息的第一个字符显示的是“-”;
	如果是目录的话,该行的字符的第一个显示的是"d",意即directory,找到这两者之间的区别,运行能够判别

2.3、显示目录中的文件

1、输入命令:

ls -l | grep "^-"
其中“^-”表示字符串的第一个字符为"-"

2、结果如下:

-rw-rw-r-- 1 xxx xxx 0 Oct 27 17:47 55.txt

2.4、统计目录和文件个数

1、我们可以用wc命令进行统计,输入命令

man wc

2、结果如下

WC(1)                                                        User Commands                                                        WC(1)

NAME
       wc - print newline, word, and byte counts for each file

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F

DESCRIPTION
       Print  newline,  word,  and  byte  counts  for  each  FILE,  and  a  total line if more than one FILE is specified.  A word is a
       non-zero-length sequence of characters delimited by white space.

       With no FILE, or when FILE is -, read standard input.

       The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte,
       maximum line length.

       -c, --bytes
              print the byte counts

       -m, --chars
              print the character counts

       -l, --lines
              print the newline counts

       --files0-from=F
              read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

       -L, --max-line-length
              print the maximum display width

       -w, --words
              print the word counts

       --help display this help and exit

       --version
              output version information and exit

结论:
参数:
	-l 仅列出行数;
	-w 仅列出多少字(英文单字);
	-m 多少字符;

3、那么统计文件个数,输入命令:

ls -l|grep "^-" | wc -l
结果:
	1

4、统计目录个数,输入命令:

ls -l|grep "^d" | wc -l
结果:
	4

5、统计文件夹下文件个数,包括子文件

ls -lR|grep "^-" | wc -l
结果:
	2

5、统计文件夹下目录个数,包括子目录

ls -lR|grep "^d" | wc -l
结果:
	3

2.5、统计目录和文件的大小

1、查看当前目录下文件的大小

ls -lht
结论:
	total 8.0K
	drwxrwxr-x 3 xxx xxx 4.0K Oct 27 17:48 66
	drwxrwxr-x 2 xxx xxx 4.0K Oct 27 17:47 77
	-rw-rw-r-- 1 xxx xxx 0 Oct 27 17:47 55.txt

2、递归查看当前目录下所有文件的大小

ls -lRht
结论:
	.:
	total 8.0K
	drwxrwxr-x 3 xxx xxx 4.0K Oct 27 17:48 66
	drwxrwxr-x 2 xxx xxx  4.0K Oct 27 17:47 77
	-rw-rw-r-- 1 xxx xxx 0 Oct 27 17:47 55.txt
	
	./66:
	total 4.0K
	-rw-rw-r-- 1 xxx xxx 0 Oct 27 17:48 22.txt
	drwxrwxr-x 2 xxx xxx 4.0K Oct 27 17:47 44
	
	./66/44:
	total 0
	
	./77:
	total 0

3、查看当前文件夹的大小

du -sh 
结论:
	16K	.

三、参考链接:

https://blog.csdn.net/sganchang/article/details/91432435
https://blog.csdn.net/mr_wangning/article/details/90923176
https://www.cnblogs.com/claireyuancy/p/6853607.html

四、后记

希望每周可以坚持打卡,不足之处欢迎批评指正,3Q。

Logo

更多推荐