今天看啥  ›  专栏  ›  彼岸的灵魂

Linux常用命令(3)

彼岸的灵魂  · CSDN  ·  · 2019-11-05 14:16

主要介绍 pwd、cd、ls、cat、more、head、tail、tr、wc、stat、cut、diff 命令

pwd

格式:pwd [选项]
作用:显示用户当前所处的工作目录

[root@localhost dev]# pwd
/dev

cd

格式:cd [目录名称]
作用:切换工作路径

[root@localhost /]# cd dev
参数 作用
cd ~ 切换到当前用户的家目录
cd - 返回到上一次所处的目录
cd … 进入上级目录
[root@localhost dev]# cd -
/
[root@localhost /]# 

ls

格式:ls [选项] [文件]
作用:显示目录中的文件信息
使用 -a 参数可以得到全部文件(包括隐藏文件)-l 参数查看文件的属性、大小等详细信息 -d 参数查看目录属性位置

[root@localhost ~]# ls
anaconda-ks.cfg       公共  视频  文档  音乐
initial-setup-ks.cfg  模板  图片  下载  桌面
[root@localhost ~]# ls -al
总用量 44
dr-xr-x---. 14 root root 4096 11月  5 05:07 .
dr-xr-xr-x. 17 root root  233 11月  3 20:58 ..
-rw-------.  1 root root 1906 11月  3 20:59 anaconda-ks.cfg
-rw-------.  1 root root  181 11月  3 22:00 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
drwx------. 10 root root  210 11月  4 04:43 .cache
drwxr-xr-x. 15 root root  276 11月  4 04:43 .config
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  3 root root   25 11月  3 21:01 .dbus
-rw-------.  1 root root   16 11月  4 04:42 .esd_auth
-rw-------.  1 root root  620 11月  5 05:07 .ICEauthority
-rw-r--r--.  1 root root 1999 11月  3 21:01 initial-setup-ks.cfg
drwx------.  3 root root   19 11月  4 04:42 .local
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
drwxr-xr-x.  2 root root    6 11月  4 04:42 公共
drwxr-xr-x.  2 root root    6 11月  4 04:42 模板
drwxr-xr-x.  2 root root    6 11月  4 04:42 视频
drwxr-xr-x.  2 root root    6 11月  4 04:42 图片
drwxr-xr-x.  2 root root    6 11月  4 04:42 文档
drwxr-xr-x.  2 root root    6 11月  4 04:42 下载
drwxr-xr-x.  2 root root    6 11月  4 04:42 音乐
drwxr-xr-x.  2 root root    6 11月  4 04:42 桌面
[root@localhost ~]# ls -ld
dr-xr-x---. 14 root root 4096 11月  5 05:07 .

cat

格式:cat [选项] [文件]
作用:查看纯文本文件(内容较少的小文件)
加 -n 参数可显示行数

[root@localhost ~]# cat -n initial-setup-ks.cfg 
     1	#version=DEVEL
     2	# X Window System configuration information
     3	xconfig  --startxonboot
     4	# License agreement
     5	eula --agreed
     6	repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
     7	repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
     8	# System authorization information
     9	auth --enableshadow --passalgo=sha512
    10	# Use CDROM installation media
    
...省略之后信息

more

格式:more [选项] [文件]
作用:查看纯文本文件(内容较多的大文件)
用空格或回车进行翻页

[root@localhost ~]# more initial-setup-ks.cfg 
#version=DEVEL
# X Window System configuration information
xconfig  --startxonboot
# License agreement
eula --agreed
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/
HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/
ResilientStorage
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
# System services
services --enabled="chronyd"
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8
--More--(32%)

head

格式:head [选项] [文件]
作用:查看纯文本文档的前N行

[root@localhost ~]# head -n 10 initial-setup-ks.cfg 
#version=DEVEL
# X Window System configuration information
xconfig  --startxonboot
# License agreement
eula --agreed
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media

tail

格式:tail [选项] [文件]
作用:查看纯文本的后N行或持续刷新内容
如果要查看最新日志消息,命令格式为 tail -f 文件名

[root@localhost ~]# tail -n 10 initial-setup-ks.cfg 
%addon com_redhat_subscription_manager 
%end
%addon com_redhat_kdump --enable --reserve-mb=auto
%end

%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
[root@localhost ~]# tail -f /var/log/messages 
Nov  5 11:15:57 localhost systemd: Started Fingerprint Authentication Daemon.
Nov  5 11:16:01 localhost gnome-keyring-daemon[2780]: couldn't initialize slot with master password: The password or PIN is incorrect
Nov  5 11:16:01 localhost dbus[766]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Nov  5 11:16:01 localhost dbus-daemon: dbus[766]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Nov  5 11:16:01 localhost systemd: Starting Hostname Service...
Nov  5 11:16:02 localhost dbus[766]: [system] Successfully activated service 'org.freedesktop.hostname1'
Nov  5 11:16:02 localhost dbus-daemon: dbus[766]: [system] Successfully activated service 'org.freedesktop.hostname1'
Nov  5 11:16:02 localhost systemd: Started Hostname Service.
Nov  5 11:16:22 localhost rhsmd: In order for Subscription Manager to provide your system with updates, your system must be registered with the Customer Portal. Please enter your Red Hat login to ensure your system is up-to-date.
Nov  5 11:16:27 localhost fprintd: ** Message: No devices in use, exit

tr

格式:tr [原始字符] [目标字符]
作用:替换文本文件中的字符
可以通过 cat+管道符+tr 命令快速替换

将小写字母全部替换为大写字母

[root@localhost ~]# cat anaconda-ks.cfg | tr [a-z ][A-Z]
#UDQRHNM=DEVEL
# SXRSDL [TSGNQHY[SHNM HMENQL[SHNM
[TSG --DM[AKDRG[CNV --O[RR[KFN=RG[512
QDON --M[LD="SDQUDQ-HHFGAU[HK[AHKHSX" --A[RDTQK=EHKD:///QTM/HMRS[KK/QDON/[CCNMR/HHFGAU[HK[AHKHSX
QDON --M[LD="SDQUDQ-RDRHKHDMSSSNQ[FD" --A[RDTQK=EHKD:///QTM/HMRS[KK/QDON/[CCNMR/RDRHKHDMSSSNQ[FD

wc

格式:wc [参数] [文本]
作用:统计指定文本的行数、字数、字节数

参数 作用
-l 只显示行数
-w 只显示单词数
-c 只显示字节数
[root@localhost ~]# wc -l anaconda-ks.cfg 
66 anaconda-ks.cfg

stat

格式:stat [文件名称]
作用:查看文件的具体存储信息和时间等信息

[root@localhost ~]# stat anaconda-ks.cfg 
  文件:"anaconda-ks.cfg"
  大小:1906      	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:33582147    硬链接:1
权限:(0600/-rw-------)  Uid:(    0/    root)   Gid:(    0/    root)
环境:system_u:object_r:admin_home_t:s0
最近访问:2019-11-05 11:20:19.887943339 +0800
最近更改:2019-11-03 20:59:04.736995172 +0800
最近改动:2019-11-03 20:59:04.736995172 +0800
创建时间:-

cut

格式:cut [参数] [文本]
作用:按列提取文本字符

提取以冒号为间隔符号的第一列内容

[root@localhost ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm

diff

格式:diff [参数] [文件]
作用:比较多个文本文件的差异
加入 --brief 可显示比较后的结果
加入 -c 可描述文件内容具体的不同




原文地址:访问原文地址
快照地址: 访问文章快照