linux命令之文本查看

Posted 七彩蜗牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux命令之文本查看相关的知识,希望对你有一定的参考价值。

vi掌握练习:

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


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

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

[[email protected] ~]$ 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:倒序读文件

[[email protected] ~]$ 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/sync
lp: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
[[email protected] ~]$

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

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

[[email protected] ~]$ more /etc/profile
# /etc/profile

# System wide environment and startup programs, for login 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:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -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行,查看新增日志文件。可以通过-加数字选择查看尾部具体的行数;

[[email protected] ~]$ tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done

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

unset i
unset -f pathmunge
1 [[email protected] ~]$ tail -5  /etc/profile    //显示尾部后5行
2     fi
3 done
4 
5 unset i
6 unset -f pathmunge
7 [[email protected] ~]$ 

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

[[email protected] ~]$ head /etc/profile
# /etc/profile

# System wide environment and startup programs, for login 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.

[[email protected] ~]$ 
1 [[email protected] ~]$ head -5 /etc/profile  //显示前5行
2 # /etc/profile
3 
4 # System wide environment and startup programs, for login setup
5 # Functions and aliases go in /etc/bashrc
6 
7 [[email protected] ~]$ 

查找文件命令find

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

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

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

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

[[email protected] ~]$ head -9 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login 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.
[[email protected]
~]$ head -9 /etc/profile | tail -1 //前9行的倒数第一行即第9行; # will prevent the need for merging in future updates.
[[email protected] ~]$ ls -al | more //显示多的时候可以通过管道后配置more、less使用
total 156
drwx------. 24 zyj  zyj  4096 Apr 24 09:46 .
drwxr-xr-x.  4 root root 4096 Apr 15 09:23 ..
-rw-------.  1 zyj  zyj  3108 Apr 24 09:46 .ICEauthority
-rw-------.  1 zyj  zyj  1319 Apr 24 09:32 .bash_history
-rw-r--r--.  1 zyj  zyj    18 Mar 22  2017 .bash_logout
-rw-r--r--.  1 zyj  zyj   176 Mar 22  2017 .bash_profile
-rw-r--r--.  1 zyj  zyj   142 Apr 14 09:11 .bashrc
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 02:57 .cache
drwxr-xr-x.  4 zyj  zyj  4096 Apr 14 09:13 .config
drwx------.  3 zyj  zyj  4096 Apr 14 02:57 .dbus
-rw-r--r--.  1 zyj  zyj    32 Apr 24 09:46 .dmrc
-rw-------.  1 zyj  zyj    16 Apr 14 02:57 .esd_auth
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 02:57 .fontconfig
drwx------.  4 zyj  zyj  4096 Apr 24 09:46 .gconf
drwxr-xr-x.  5 zyj  zyj  4096 Apr 14 02:57 .gnome2
drwxrwxr-x.  3 zyj  zyj  4096 Apr 14 02:57 .gnote
drwx------.  2 zyj  zyj  4096 Apr 24 09:46 .gnupg
-rw-rw-r--.  1 zyj  zyj   127 Apr 24 09:46 .gtk-bookmarks
drwx------.  2 zyj  zyj  4096 Apr 14 02:57 .gvfs
-rw-------.  1 zyj  zyj    56 Apr 14 10:39 .lesshst
drwxr-xr-x.  3 zyj  zyj  4096 Apr 14 02:57 .local
drwxr-xr-x.  4 zyj  zyj  4096 Apr 14 02:39 .mozilla
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 02:57 .nautilus
drwx------.  2 zyj  zyj  4096 Apr 14 02:57 .pulse
-rw-------.  1 zyj  zyj   256 Apr 14 02:57 .pulse-cookie
drwx------.  2 zyj  zyj  4096 Apr 15 06:26 .ssh
-rw-------.  1 zyj  zyj  2130 Apr 17 10:57 .viminfo
-rw-rw-r--.  1 zyj  zyj    20 Apr 17 10:07 .vimrc
-rw-------.  1 zyj  zyj  3177 Apr 24 09:51 .xsession-errors
-rw-------.  1 zyj  zyj  4317 Apr 24 09:42 .xsession-errors.old
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 09:13 Desktop
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 09:13 Documents
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 09:13 Downloads
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 09:13 Music
drwxr-xr-x.  2 zyj  zyj  4096 Apr 14 09:13 Pictures
--more
[[email protected] ~]$ ps aux | grep tty //查找包含tty的进程
root       2484  0.0  0.0   4068   536 tty2     Ss+  09:45   0:00 /sbin/mingetty /dev/tty2
root       2486  0.0  0.0   4068   532 tty3     Ss+  09:45   0:00 /sbin/mingetty /dev/tty3
root       2488  0.0  0.0   4068   532 tty4     Ss+  09:45   0:00 /sbin/mingetty /dev/tty4
root       2490  0.0  0.0   4068   536 tty5     Ss+  09:45   0:00 /sbin/mingetty /dev/tty5
root       2492  0.0  0.0   4068   532 tty6     Ss+  09:45   0:00 /sbin/mingetty /dev/tty6
root       2514  0.1  2.4 176436 24456 tty1     Ss+  09:45   0:03 /usr/bin/Xorg :0 -br -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-cxJWaZ/database -nolisten tcp vt1
zyj        3947  0.0  0.0 103308   752 pts/1    S+   10:21   0:00 grep tty
[[email protected] ~]$ 
[[email protected] ~]$ ps aux | grep tty | grep tty2 //命令1|命令2
root 2484 0.0 0.0 4068 536 tty2 Ss+ 09:45 0:00 /sbin/mingetty /dev/tty2
zyj 4160 0.0 0.0 103304 724 pts/1 S+ 10:33 0:00 grep tty2
[[email protected] ~]$ ls /etc/ | grep passwd //在etc下的文件中查找有passwd的内容;
passwd
passwd-
passwd.OLD
[[email protected] ~]$ find /etc/pass* 
/etc/passwd
/etc/passwd-
/etc/passwd.OLD

 


以上是关于linux命令之文本查看的主要内容,如果未能解决你的问题,请参考以下文章

linux sed 之 查看指定时间段的日志

linux中怎么查看mysql数据库版本

1.linux命令之cat和tac

linux-12基本命令之 cat,more,head, tail ,tr,od,wc,cut,diff

linux命令之----sort命令用于将文本文件内容加以排序

linux基本功系列之cat命令实战