1. 程式人生 > >linux命令之文本查看

linux命令之文本查看

download col coo 練習 單詞 txt errors sas ror

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/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
[zyj@localhost ~]$

more:顯示一個版面,按空格繼續顯示,並顯示百分比,按q退出查看。

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

[zyj@localhost ~]$ 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行,查看新增日誌文件。可以通過-加數字選擇查看尾部具體的行數;

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

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/null 2>&1
        fi
    fi
done

unset i
unset -f pathmunge
1 [zyj@localhost ~]$ tail -5  /etc/profile    //顯示尾部後5行
2     fi
3 done
4 
5 unset i
6 unset -f pathmunge
7 [zyj@localhost ~]$ 

head:默認顯示前10行,可以通過-加數字選擇查看前面具體的行數;

[zyj@localhost ~]$ 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.

[zyj@localhost ~]$ 
1 [zyj@localhost ~]$ 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 [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 ~]$ grep root /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 ~]$ 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......

[zyj@localhost ~]$ 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.
[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 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
[zyj@localhost ~]$ 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
[zyj@localhost ~]$ 
[zyj@localhost ~]$ 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
[zyj@localhost ~]$ ls /etc/ | grep passwd //在etc下的文件中查找有passwd的內容;
passwd
passwd-
passwd.OLD
[zyj@localhost ~]$ find /etc/pass* 
/etc/passwd
/etc/passwd-
/etc/passwd.OLD

linux命令之文本查看