当前位置:首页 > 操作系统 > Linux

linux命令之文本查看

vi掌握练习:

英文文档,相同的单词复制粘贴光标移动编辑等操作;


cat:显示文件所有内容,小文件查看时使用。

缺点:文件大时不方便查看,文件很大时,会抢占系统资源,会出现命令崩溃。

[zyj@localhost ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zyj:x:500:500: python_full:/home/zyj:/bin/bash

tac:倒序读文件

[zyj@localhost ~]$ tac /etc/passwd
zyj:x:500:500: python_full:/home/zyj:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/synclp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[zyj@localhost ~]$

more:显示一个版面,按空格继续显示,并显示百分比,按q退出查看。

less:一行一行显示,和more差不多

[zyj@localhost ~]$ more /etc/profile
# /etc/profile

# System wide environment and startup programs, forlogin setup
# Functions and aliases go in /etc/bashrc

# Its NOT a good idea to change this file unless you know what you
# are doing. Its much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case":${PATH}:"in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1else
                PATH=$1:$PATH
            fiesac
}


if [ -x /usr/bin/id ]; thenif [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"fi

# Path manipulation
--More--(47%)

 tail:查看文件的末尾,默认显示尾部10行,查看新增日志文件。可以通过-加数字选择查看尾部具体的行数;

[zyj@localhost ~]$ tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"else
            . "$i" >/dev/null2>&1fifidone

unset i
unset -f pathmunge
[zyj@localhost ~]$ 
            [zyj@localhost ~]$ tail-f  /etc/profile   //查看实时日志,新增的日志追加显示出来。
-bash: tail-f: command not found
[zyj@localhost ~]$ tail -f  /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"else
            . "$i" >/dev/null2>&1fifidone

unset i
unset -f pathmunge
            1 [zyj@localhost ~]$ tail -5  /etc/profile    //显示尾部后5行
2fi3done45unset i
6 unset -f pathmunge
7 [zyj@localhost ~]$ 

 head:默认显示前10行,可以通过-加数字选择查看前面具体的行数;

[zyj@localhost ~]$ head /etc/profile
# /etc/profile

# System wide environment and startup programs, forlogin setup
# Functions and aliases go in /etc/bashrc

# Its NOT a good idea to change this file unless you know what you
# are doing. Its much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

[zyj@localhost ~]$ 
            1 [zyj@localhost ~]$ head -5 /etc/profile  //显示前5行2 # /etc/profile
34 # System wide environment and startup programs, forlogin setup
5 # Functions and aliases go in /etc/bashrc
67 [zyj@localhost ~]$ 

查找文件命令find

[zyj@localhost home]$ find h* //在当前目录下查找以h开头的文件hello.txt
[zyj@localhost home]$ find /etc/pass* //etc目录下查找以pass开头的文件
/etc/passwd
/etc/passwd-
/etc/passwd.OLD
[zyj@localhost ~]$ find /etc/pass* -print //etc目录下查找以pass开头的文件,并显示出来
/etc/passwd
/etc/passwd-
/etc/passwd.OLD

 在文件内容中查找关键字grep

[zyj@localhost ~]$ greproot /etc/passwd//查找passwd中包含root的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[zyj@localhost ~]$ grep^root /etc/passwd//以root作为行的开始的内容
root:x:0:0:root:/root:/bin/bash
[zyj@localhost ~]$ grepbash$ /etc/passwd//查找以bash结尾的内容   
root:x:0:0:root:/root:/bin/bash
zyj:x:500:500: python_full:/home/zyj:/bin/bash

 管道:把管道左边的命令执行的结果传给右边的命令;命令1|命令2|命令3......

[zyj@localhost ~]$ head -9 /etc/profile
# /etc/profile

# System wide environment and startup programs, forlogin setup
# Functions and aliases go in /etc/bashrc

# Its NOT a good idea to change this file unless you know what you
# are doing. Its much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
[zyj@localhost
~]$ head -9 /etc/profile | tail -1//前9行的倒数第一行即第9行; # will prevent the need for merging in future updates.
[zyj@localhost ~]$ ls -al | more //显示多的时候可以通过管道后配置more、less使用
total 156
drwx------. 24 zyj  zyj  4096 Apr 2409:46 .
drwxr-xr-x.  4 root root 4096 Apr 1509:23 ..
-rw-------.  1 zyj  zyj  3108 Apr 2409:46 .ICEauthority
-rw-------.  1 zyj  zyj  1319 Apr 2409:32 .bash_history
-rw-r--r--.  1 zyj  zyj    18 Mar 222017 .bash_logout
-rw-r--r--.  1 zyj  zyj   176 Mar 222017 .bash_profile
-rw-r--r--.  1 zyj  zyj   142 Apr 1409:11 .bashrc
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1402:57 .cache
drwxr-xr-x.  4 zyj  zyj  4096 Apr 1409:13 .config
drwx------.  3 zyj  zyj  4096 Apr 1402:57 .dbus
-rw-r--r--.  1 zyj  zyj    32 Apr 2409:46 .dmrc
-rw-------.  1 zyj  zyj    16 Apr 1402:57 .esd_auth
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1402:57 .fontconfig
drwx------.  4 zyj  zyj  4096 Apr 2409:46 .gconf
drwxr-xr-x.  5 zyj  zyj  4096 Apr 1402:57 .gnome2
drwxrwxr-x.  3 zyj  zyj  4096 Apr 1402:57 .gnote
drwx------.  2 zyj  zyj  4096 Apr 2409:46 .gnupg
-rw-rw-r--.  1 zyj  zyj   127 Apr 2409:46 .gtk-bookmarks
drwx------.  2 zyj  zyj  4096 Apr 1402:57 .gvfs
-rw-------.  1 zyj  zyj    56 Apr 1410:39 .lesshst
drwxr-xr-x.  3 zyj  zyj  4096 Apr 1402:57 .local
drwxr-xr-x.  4 zyj  zyj  4096 Apr 1402:39 .mozilla
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1402:57 .nautilus
drwx------.  2 zyj  zyj  4096 Apr 1402:57 .pulse
-rw-------.  1 zyj  zyj   256 Apr 1402:57 .pulse-cookie
drwx------.  2 zyj  zyj  4096 Apr 1506:26 .ssh
-rw-------.  1 zyj  zyj  2130 Apr 1710:57 .viminfo
-rw-rw-r--.  1 zyj  zyj    20 Apr 1710:07 .vimrc
-rw-------.  1 zyj  zyj  3177 Apr 2409:51 .xsession-errors
-rw-------.  1 zyj  zyj  4317 Apr 2409:42 .xsession-errors.old
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1409:13 Desktop
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1409:13 Documents
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1409:13 Downloads
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1409:13 Music
drwxr-xr-x.  2 zyj  zyj  4096 Apr 1409:13 Pictures
--more
[zyj@localhost ~]$ ps aux | grep tty //查找包含tty的进程
root       24840.00.04068536 tty2     Ss+  09:450:00 /sbin/mingetty /dev/tty2
root       24860.00.04068532 tty3     Ss+  09:450:00 /sbin/mingetty /dev/tty3
root       24880.00.04068532 tty4     Ss+  09:450:00 /sbin/mingetty /dev/tty4
root       24900.00.04068536 tty5     Ss+  09:450:00 /sbin/mingetty /dev/tty5
root       24920.00.04068532 tty6     Ss+  09:450:00 /sbin/mingetty /dev/tty6
root       25140.12.417643624456 tty1     Ss+  09:450:03 /usr/bin/Xorg :0 -br -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-cxJWaZ/database -nolisten tcp vt1
zyj        39470.00.0103308752 pts/1    S+   10:210:00grep tty
[zyj@localhost ~]$ 
[zyj@localhost ~]$ ps aux | grep tty | grep tty2 //命令1|命令2
root 24840.00.04068536 tty2 Ss+ 09:450:00 /sbin/mingetty /dev/tty2
zyj 41600.00.0103304724 pts/1 S+ 10:330:00grep tty2
[zyj@localhost ~]$ ls /etc/ | greppasswd//在etc下的文件中查找有passwd的内容;passwdpasswd-
passwd.OLD
[zyj@localhost ~]$ find /etc/pass* 
/etc/passwd
/etc/passwd-
/etc/passwd.OLD

 

原文:https://www.cnblogs.com/zhaoyujiao/p/8933418.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐