Linux基础命令(二).md

目录

  • Linux常用基础命令(二)
    • 1、cat:拼接文件内容并输出至标准输出(屏幕)
    • 2、tac:连接文件并倒序打印内容至标准输出
    • 3、more:全屏查看文本文件内容,只能从前往后看,看完自动退出
    • 4、less:全屏查看文本文件内容,可从前往后看亦可从后往前看,看完不会自动退出
    • 5、head:从文件首部开始打印文件内容,默认打印10行
    • 6、tail:从文件尾部开始打印文件内容,默认打印10行
    • 7、wc:文本统计
    • 8、du:查看文件或目录占用的磁盘空间大小
    • 9、df:报告文件系统磁盘空间使用情况
    • 10、hostname:查看或临时修改主机名,重开终端有效,重启失效
    • 11、hostnamectl:查看或永久修改主机名,重开终端有效,重启依然有效
    • 12、whoami:显示当前登录用户
    • 13、w:显示当前在线用户并显示其正在运行的命令
    • 14、who:查看当前在线用户
    • 15、which:显示指定命令的绝对路径
    • 16、cal:打印日历
    • 17、ldd:查看指定程序有哪些依赖库文件
    • 18、date:显示或设置日期与时间
    • 19、如何获取命令帮助?

Linux常用基础命令(二)

1、cat:拼接文件内容并输出至标准输出(屏幕)

-n:显示行号

[root@localhost ~]# ls
a abc anaconda-ks.cfg c d d.sql
[root@localhost ~]# cat -n anaconda-ks.cfg 
     1  #version=DEVEL
     2  # System authorization information
     3  auth --enableshadow --passalgo=sha512
     4  repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
     5  repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
     6  # Use CDROM installation media
     7  cdrom
     8  # Use graphical install
     9  graphical
    10  # Run the Setup Agent on first boot
    11  firstboot --enable
    12  ignoredisk --only-use=sda
    13  # Keyboard layouts
    14  keyboard --vckeymap=cn --xlayouts='cn'
    15  # System language
    16  lang zh_CN.UTF-8

2、tac:连接文件并倒序打印内容至标准输出

[root@localhost ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@localhost ~]# tac /etc/issue

Kernel \r on an \m
\S

3、more:全屏查看文本文件内容,只能从前往后看,看完自动退出

[root@localhost ~]# ls
a abc anaconda-ks.cfg c d d.sql
[root@localhost ~]# more anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network information
network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$n0NG4sjieXl4/neS$RawZaKgan9Vnxckj9ZrGewgLXPU/ieXW/DSCHHqd4HbB.YIIJ4bKP2QycSh3t
YxNFODhBUZm.qPyH3Wm3Upni.
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core
chrony
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

4、less:全屏查看文本文件内容,可从前往后看亦可从后往前看,看完不会自动退出

[root@localhost ~]# ls 
a abc anaconda-ks.cfg c d d.sql
[root@localhost ~]# less anaconda-ks.cfg 




#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network information
network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$n0NG4sjieXl4/neS$RawZaKgan9Vnxckj9ZrGewgLXPU/ieXW/DSCHHqd4HbB.YIIJ4bKP2QycSh3t
YxNFODhBUZm.qPyH3Wm3Upni.
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core
chrony
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

5、head:从文件首部开始打印文件内容,默认打印10行

-n 15:查看文件前15行的内容

[root@localhost ~]# ls
a abc anaconda-ks.cfg c d d.sql
[root@localhost ~]# cat -n anaconda-ks.cfg |head -15
     1  #version=DEVEL
     2  # System authorization information
     3  auth --enableshadow --passalgo=sha512
     4  repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
     5  repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
     6  # Use CDROM installation media
     7  cdrom
     8  # Use graphical install
     9  graphical
    10  # Run the Setup Agent on first boot
    11  firstboot --enable
    12  ignoredisk --only-use=sda
    13  # Keyboard layouts
    14  keyboard --vckeymap=cn --xlayouts='cn'
    15  # System language

6、tail:从文件尾部开始打印文件内容,默认打印10行

-n 15:查看文件尾部15行的内容
-f:实时查看文件的更新

[root@localhost ~]# cat -n anaconda-ks.cfg |tail -15
    36  @core
    37  chrony
    38  kexec-tools
    39 
    40  %end
    41 
    42  %addon com_redhat_kdump --enable --reserve-mb='auto'
    43 
    44  %end
    45 
    46  %anaconda
    47  pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    48  pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    49  pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    50  %end
[root@localhost ~]# tail -f d.sql 

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

7、wc:文本统计

-c:统计文本字节数
-w:统计文本单词数
-l:统计文本行数

[root@localhost ~]# ls
a abc anaconda-ks.cfg c d d.sql
[root@localhost ~]# wc -c anaconda-ks.cfg 
1451 anaconda-ks.cfg
[root@localhost ~]# wc -w anaconda-ks.cfg 
128 anaconda-ks.cfg
[root@localhost ~]# wc -l anaconda-ks.cfg 
50 anaconda-ks.cfg

8、du:查看文件或目录占用的磁盘空间大小

-s:显示总的占用空间大小
-h:单位转换,以更友好的方式显示大小

[root@localhost ~]# ls
a abc anaconda-ks.cfg c d d.sql
[root@localhost ~]# du c
0   c/d/e/f/g/h
0   c/d/e/f/g
0   c/d/e/f
0   c/d/e
0   c/d
0   c
[root@localhost ~]# du -s c
0   c
[root@localhost ~]# du -sh c
0   c

9、df:报告文件系统磁盘空间使用情况

-h:单位转换,以更友好的方式显示大小

[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17811456 969896 16841560 6% /
devtmpfs 922396 0 922396 0% /dev
tmpfs 933512 0 933512 0% /dev/shm
tmpfs 933512 8784 924728 1% /run
tmpfs 933512 0 933512 0% /sys/fs/cgroup
/dev/sda1 1038336 146048 892288 15% /boot
tmpfs 186704 0 186704 0% /run/user/0
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 948M 17G 6% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.6M 904M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 183M 0 183M 0% /run/user/0

10、hostname:查看或临时修改主机名,重开终端有效,重启失效

[root@localhost ~]# hostname
localhost.localdomain

11、hostnamectl:查看或永久修改主机名,重开终端有效,重启依然有效

[root@localhost ~]# hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 6853500258764b1fae8e52afc08a5ff9
           Boot ID: 77d9345ffc2d4f6a9360e9dd9eee6ea5
    Virtualization: vmware
  Operating System: Red Hat Enterprise Linux Server 7.4 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.4:GA:server
            Kernel: Linux 3.10.0-693.el7.x86_64
      Architecture: x86-64

12、whoami:显示当前登录用户

[root@localhost ~]# whoami
root

13、w:显示当前在线用户并显示其正在运行的命令

[root@localhost ~]# w
 17:50:02 up 8:47, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.124.1 09:02 2.00s 0.16s 0.01s w

14、who:查看当前在线用户

[root@localhost ~]# who
root pts/0 2019-09-10 09:02 (192.168.124.1)

15、which:显示指定命令的绝对路径

[root@localhost ~]# which ls
alias ls='ls --color=auto'
 /usr/bin/ls

16、cal:打印日历

[root@localhost ~]# cal
      九月 2019     
日 一 二 三 四 五 六
 1 2 3 4 5 6 7
 8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

17、ldd:查看指定程序有哪些依赖库文件

[root@localhost ~]# ldd /usr/bin/ls
 linux-vdso.so.1 => (0x00007ffc15df9000)
 libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fd834903000)
 libcap.so.2 => /lib64/libcap.so.2 (0x00007fd8346fe000)
 libacl.so.1 => /lib64/libacl.so.1 (0x00007fd8344f4000)
 libc.so.6 => /lib64/libc.so.6 (0x00007fd834131000)
 libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fd833ecf000)
 libdl.so.2 => /lib64/libdl.so.2 (0x00007fd833cca000)
 /lib64/ld-linux-x86-64.so.2 (0x0000560d9fd6c000)
 libattr.so.1 => /lib64/libattr.so.1 (0x00007fd833ac5000)
 libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd8338a9000)

18、date:显示或设置日期与时间

-s:以字符串方式设置时间
格式化输出时间:+
%Y:年
%m:月
%d:日
%H:时
%M:分
%S:秒

[root@localhost ~]# date
2019年 09月 10日 星期二 17:59:26 CST
[root@localhost ~]# date -s '2019-09-10 18:01:00'
2019年 09月 10日 星期二 18:01:00 CST
[root@localhost ~]# touch all-$(date '+%Y%m%d')
[root@localhost ~]# ll
总用量 8
drwxr-xr-x. 2 root root 19 9月 7 16:23 a
-rw-r--r--. 1 root root 0 9月 7 16:16 abc
-rw-r--r--. 1 root root 0 9月 10 18:04 all-20190910
-rw-------. 1 root root 1451 9月 6 19:48 anaconda-ks.cfg
drwxr-xr-x. 3 root root 15 9月 7 15:46 c
drwxr-xr-x. 2 root root 19 9月 7 16:13 d
-rw-------. 1 root root 1451 9月 7 16:13 d.sql

19、如何获取命令帮助?

内部命令:
help COMMAND

[root@localhost ~]# type cd
cd 是 shell 内嵌
[root@localhost ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR. The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR. Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory. If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be a variable name. If that variable has a value,
    its value is used for DIR.
    
    Options:
        -L  force symbolic links to be followed
        -P  use the physical directory structure without following symbolic
     links
        -e  if the -P option is supplied, and the current working directory
     cannot be determined successfully, exit with a non-zero status
    
    The default is to follow symbolic links, as if `-L' were specified.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

外部命令:
COMMAND --help

[root@localhost ~]# type ls
ls 是 `ls --color=auto' 的别名
[root@localhost ~]# ls --help
用法:ls [选项]... [文件]...
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 不隐藏任何以. 开始的项目
  -A, --almost-all  列出除. 及.. 以外的任何项目
      --author  与-l 同时使用时列出每个文件的作者
  -b, --escape  以八进制溢出序列表示不可打印的字符
      --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 format 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 'never', 'auto',
                               or 'always' (the default); 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    类似-l,但不列出所有者
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group    以一个长列表的形式,不输出组名
  -h, --human-readable  与-l 一起,以易于阅读的格式输出文件大小
    (例如 1K 234M 2G)
      --si  同上面类似,但是使用1000 为基底而非1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --indicator-style=WORD append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode print the index number of each file
  -I, --ignore=PATTERN do not list implied entries matching shell PATTERN
  -k, --kibibytes default to 1024-byte blocks for disk usage
  -l    使用较长格式列出信息
  -L, --dereference 当显示符号链接的文件信息时,显示符号链接所指示
    的对象而并非符号链接本身的信息
  -m    所有项目以逗号分隔,并填满整行行宽
  -n, --numeric-uid-gid 类似 -l,但列出UID 及GID 号
  -N, --literal 输出未经处理的项目名称 (如不特别处理控制字符)
  -o    类似 -l,但不列出有关组的信息
  -p, --indicator-style=slash   对目录加上表示符号"/"
  -q, --hide-control-chars print ? instead of nongraphic characters
      --show-control-chars show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name enclose entry names in double quotes
      --quoting-style=WORD use quoting style WORD for entry names:
                               literal, locale, shell, shell-always, c, escape
  -r, --reverse 逆序排列
  -R, --recursive   递归显示子目录
  -s, --size    以块数形式显示每个文件分配的尺寸
  -S sort by file size
      --sort=WORD sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u)
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time
      --time-style=STYLE with -l, show times using style STYLE:
                               full-iso, long-iso, iso, locale, or +FORMAT;
                               FORMAT is interpreted like in 'date'; if FORMAT
                               is FORMAT1FORMAT2, then FORMAT1 applies
                               to non-recent files and FORMAT2 to recent files;
                               if STYLE is prefixed with 'posix-', STYLE
                               takes effect only outside the POSIX locale
  -t sort by modification time, newest first
  -T, --tabsize=COLS assume tab stops at each COLS instead of 8
  -u with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time
  -U do not sort; list entries in directory order
  -v natural sort of (version) numbers within text
  -w, --width=COLS assume screen width instead of current value
  -x list entries by lines instead of by columns
  -X sort alphabetically by entry extension
  -1 list one file per line

SELinux options:

  --lcontext Display security context. Enable -l. Lines
                             will probably be too wide for most displays.
  -Z, --context Display security context so it fits on most
                             displays. Displays only mode, user, group,
                             security context and file name.
  --scontext Display only security context and file name.
      --help    显示此帮助信息并退出
      --version 显示版本信息并退出

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。
使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。
LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。


退出状态:
 0 正常
 1 一般问题 (例如:无法访问子文件夹)
 2 严重问题 (例如:无法使用命令行参数)

GNU coreutils online help: 
请向 报告ls 的翻译错误
要获取完整文档,请运行:info coreutils 'ls invocation'

man手册:
man COMMAND

[root@localhost ~]# man ls


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 alpha‐
       betically 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

转载于:https://www.cnblogs.com/liping0826/p/11527806.html

你可能感兴趣的:(操作系统,shell,运维)