一些非常有用的命令能让命令行的生活更满足

使用 Linux 命令工作可以获得许多乐趣,但是如果您使用一些命令,它们可以减少您的工作或以有趣的方式显示信息时,您将获得更多的乐趣。在今天的文章中,我们将介绍六个命令,它们可能会使你用在命令行上的时间更加值当。

watch

​watch​​​​watch who​​ 命令或者 ​​watch -n 15 who​


$                 watch                 -        n                 5                 who

Every 5.0s : who stinkbug : Wed Aug 23 14 : 52 : 15 2017



shs pts / 0 2017 - 08 - 23 14 : 45 ( 192.168.0.11 )

zoe pts / 1 2017 - 08 - 23 08 : 15 ( 192.168.0.19 )

您也可以使用它来查看日志文件。如果您显示的数据没有任何变化,则只有窗口角落里的日期和时间会发生变化。




$                 watch         tail                 /        var        /        log        /        syslog


Every 2.0s : tail / var / log / syslog stinkbug : Wed Aug 23 15 : 16 : 37 2017





Aug 23 14 : 45 : 01 stinkbug CRON [ 7214 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu


ll && debian - sa1 1 1 )


Aug 23 14 : 45 : 17 stinkbug systemd [ 1 ] : Started Session 179 of user shs .


Aug 23 14 : 55 : 01 stinkbug CRON [ 7577 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu


ll && debian - sa1 1 1 )


Aug 23 15 : 05 : 01 stinkbug CRON [ 7582 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu


ll && debian - sa1 1 1 )


Aug 23 15 : 08 : 48 stinkbug systemd [ 1 ] : Starting Cleanup of Temporary Directories . . .


Aug 23 15 : 08 : 48 stinkbug systemd - tmpfiles [ 7584 ] : [ / usr / lib / tmpfiles . d / var . conf : 1


4 ] Duplicate line for path "/var/log" , ignoring .


Aug 23 15 : 08 : 48 stinkbug systemd [ 1 ] : Started Cleanup of Temporary Directories .


Aug 23 15 : 13 : 41 stinkbug systemd [ 1 ] : Started Session 182 of user shs .


Aug 23 15 : 14 : 29 stinkbug systemd [ 1 ] : Started Session 183 of user shs .


Aug 23 15 : 15 : 01 stinkbug CRON [ 7828 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu


ll && debian - sa1 1 1 )



​tail -f /var/log/syslog​

look

​look​​ 可能会让我们以为它和 ​​watch​​ 做类似的事情,但其实是不同的。​​look​​命令用于搜索以某个特定字符串开头的单词。





$                 look         ecl

eclectic

eclectic 's

eclectically

eclecticism

eclecticism' s

eclectics

eclipse

eclipse 's

eclipsed

eclipses

eclipsing

ecliptic

ecliptic' s


​look​​ 命令通常有助于单词的拼写,它使用 ​​/usr/share/dict/words​





$                 look         esac                 .        bashrc


esac


esac


esac

 



​awk​​ 命令后面的 ​​grep​

man -k

​man -k​​ 命令列出包含指定单词的手册页。它的工作基本上和 ​​apropos​














$                 man                 -        k                 logrotate

dh_installlogrotate ( 1 ) - install logrotate config files

logrotate ( 8 ) - rotates , compresses , and mails system logs

logrotate . conf ( 5 ) - rotates , compresses , and mails system logs

help

当你完全绝望的时候,您可能会试图使用此命令,​​help​










$                 help


GNU bash , version 4.4.7 ( 1 ) - release ( i686 - pc - linux - gnu )


These shell commands are defined internally . Type ` help ' to see this list.


Type `help name' to find out more about the function ` name '.


Use `info bash' to find out more about the shell in general .


Use ` man - k ' or `info' to find out more about commands not in this list .





A star ( * ) next to a name means that the command is disabled .





job _spec [ & ] history [ - c ] [ - d offset ] [ n ] or hist >


( ( expression ) ) if COMMANDS ; then COMMANDS ; [ elif C >


. filename [ arguments ] jobs [ - lnprs ] [ jobspec . . . ] or jobs >


: kill [ - s sigspec | - n signum | - sigs >


[ arg . . . ] let arg [ arg . . . ]


[ [ expression ] ] local [ option ] name [ = value ] . . .


alias [ - p ] [ name [ = value ] . . . ] logout [ n ]


bg [ job _spec . . . ] mapfile [ - d delim ] [ - n count ] [ - O or >


bind [ - lpsvPSVX ] [ - m keymap ] [ - f file > popd [ - n ] [ + N | - N ]


break [ n ] printf [ - v var ] format [ arguments ]


builtin [ shell - builtin [ arg . . . ] ] pushd [ - n ] [ + N | - N | dir ]


caller [ expr ] pwd [ - LP ]


case WORD in [ PATTERN [ | PATTERN ] . . . ) > read [ - ers ] [ - a array ] [ - d delim ] [ ->


cd [ - L | [ - P [ - e ] ] [ - @ ] ] [ dir ] readarray [ - n count ] [ - O origin ] [ - s >


command [ - pVv ] command [ arg . . . ] readonly [ - aAf ] [ name [ = value ] . . . ] o >


compgen [ - abcdefgjksuv ] [ - o option ] [ > return [ n ]


complete [ - abcdefgjksuv ] [ - pr ] [ - DE ] > select NAME [ in WORDS . . . ; ] do COMM >


compopt [ - o | + o option ] [ - DE ] [ name . . > set [ - abefhkmnptuvxBCHP ] [ - o option ->


continue [ n ] shift [ n ]


coproc [ NAME ] command [ redirections ] shopt [ - pqsu ] [ - o ] [ optname . . . ]


declare [ - aAfFgilnrtux ] [ - p ] [ name [ = v > source filename [ arguments ]


dirs [ - clpv ] [ + N ] [ - N ] suspend [ - f ]


disown [ - h ] [ - ar ] [ jobspec . . . | pid > test [ expr ]


echo [ - neE ] [ arg . . . ] time [ - p ] pipeline


enable [ - a ] [ - dnps ] [ - f filename ] [ na > times


eval [ arg . . . ] trap [ - lp ] [ [ arg ] signal _spec . . . ]


exec [ - cl ] [ - a name ] [ command [ argume > true


exit [ n ] type [ - afptP ] name [ name . . . ]


export [ - fn ] [ name [ = value ] . . . ] or ex > typeset [ - aAfFgilnrtux ] [ - p ] name [ = v >


false ulimit [ - SHabcdefiklmnpqrstuvxPT ] [ l >


fc [ - e ename ] [ - lnr ] [ first ] [ last ] o > umask [ - p ] [ - S ] [ mode ]


fg [ job_spec ] unalias [ - a ] name [ name . . . ]


for NAME [ in WORDS . . . ] ; do COMMAND > unset [ - f ] [ - v ] [ - n ] [ name . . . ]


for ( ( exp1 ; exp2 ; exp3 ) ) ; do COMMAN > until COMMANDS ; do COMMANDS ; done


function name { COMMANDS ; } or name > variables - Names and meanings of so >


getopts optstring name [ arg ] wait [ - n ] [ id . . . ]


hash [ - lr ] [ - p pathname ] [ - dt ] [ name > while COMMANDS ; do COMMANDS ; done


help [ - dms ] [ pattern . . . ] { COMMANDS ; }


stat -c

​stat​​ 命令用于显示文件的大小、所有者、用户组、索引节点号、权限、修改和访问时间等重要的统计信息。这是一个非常有用的命令,可以显示比 ​​ls -l​














$                 stat                 .        bashrc

File : . bashrc

Size : 4048 Blocks : 8 IO Block : 4096 regular file

Device : 806h / 2054d Inode : 421481 Links : 1

Access : ( 0644 / - rw - r -- r -- ) Uid : ( 1000 / shs ) Gid : ( 1000 / shs )

Access : 2017 - 08 - 23 15 : 13 : 41.781809933 - 0400

Modify : 2017 - 06 - 21 17 : 37 : 11.875157790 - 0400

Change : 2017 - 06 - 21 17 : 37 : 11.899157791 - 0400

Birth : -

 


使用 ​​-c​









$                 stat                 -        c                 '%n %a'                 .        bashrc


. bashrc 644



​%n​​ 表示每个文件的名称,而 ​​%a​​ 表示访问权限。​​%u​​ 表示数字类型的 UID,而 ​​%U​​表示用户名。












$                 stat                 -        c                 '%n %a'                 bin        /        *

bin / loop 700

bin / move2nohup 700

bin / nohup . out 600

bin / show _release 700



$ stat - c '%n %a %U' bin / *

bin / loop 700 shs

bin / move2nohup 700 shs

bin / nohup . out 600 root

bin / show _release 700 shs


TAB

如果你没有使用过 tab 键来补全文件名,你真的错过了一个非常有用的命令行技巧。tab 键提供文件名补全功能(包括使用 ​​cd​​ 时的目录)。它在出现歧义之前尽可能多的填充文件名(多个文件以相同的字母开头。如果您有一个名为 ​​bigplans​​ 的文件,另一个名为 ​​bigplans2017​​的文件会发生歧义,你将听到一个声音,然后需要决定是按下回车键还是输入 ​​2​

一些非常有用的命令能让命令行的生活更满足

使用 Linux 命令工作可以获得许多乐趣,但是如果您使用一些命令,它们可以减少您的工作或以有趣的方式显示信息时,您将获得更多的乐趣。在今天的文章中,我们将介绍六个命令,它们可能会使你用在命令行上的时间更加值当。

watch

​watch​​您可能会在等待某人登录时使用它。在这种情况下,您可以使用 ​​watch who​​ 命令或者 ​​watch -n 15 who​











$                   watch                   -         n                   5                   who


Every 5.0s : who stinkbug : Wed Aug 23 14 : 52 : 15 2017





shs pts / 0 2017 - 08 - 23 14 : 45 ( 192.168.0.11 )


zoe pts / 1 2017 - 08 - 23 08 : 15 ( 192.168.0.19 )




您也可以使用它来查看日志文件。如果您显示的数据没有任何变化,则只有窗口角落里的日期和时间会发生变化。


















$                   watch          tail                   /         var         /         log         /         syslog

Every 2.0s : tail / var / log / syslog stinkbug : Wed Aug 23 15 : 16 : 37 2017



Aug 23 14 : 45 : 01 stinkbug CRON [ 7214 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu

ll && debian - sa1 1 1 )

Aug 23 14 : 45 : 17 stinkbug systemd [ 1 ] : Started Session 179 of user shs .

Aug 23 14 : 55 : 01 stinkbug CRON [ 7577 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu

ll && debian - sa1 1 1 )

Aug 23 15 : 05 : 01 stinkbug CRON [ 7582 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu

ll && debian - sa1 1 1 )

Aug 23 15 : 08 : 48 stinkbug systemd [ 1 ] : Starting Cleanup of Temporary Directories . . .

Aug 23 15 : 08 : 48 stinkbug systemd - tmpfiles [ 7584 ] : [ / usr / lib / tmpfiles . d / var . conf : 1

4 ] Duplicate line for path "/var/log" , ignoring .

Aug 23 15 : 08 : 48 stinkbug systemd [ 1 ] : Started Cleanup of Temporary Directories .

Aug 23 15 : 13 : 41 stinkbug systemd [ 1 ] : Started Session 182 of user shs .

Aug 23 15 : 14 : 29 stinkbug systemd [ 1 ] : Started Session 183 of user shs .

Aug 23 15 : 15 : 01 stinkbug CRON [ 7828 ] : ( root ) CMD ( command - v debian - sa1 > / dev / nu

ll && debian - sa1 1 1 )

这里的输出和使用命令 ​​tail -f /var/log/syslog​

look

这个命令的名字 ​​look​​ 可能会让我们以为它和 ​​watch​​ 做类似的事情,但其实是不同的。​​look​​命令用于搜索以某个特定字符串开头的单词。











$                   look          ecl


eclectic


eclectic 's


eclectically


eclecticism


eclecticism' s


eclectics


eclipse


eclipse 's


eclipsed


eclipses


eclipsing


ecliptic


ecliptic' s


​look​​ 命令通常有助于单词的拼写,它使用 ​​/usr/share/dict/words​














$                   look          esac                   .         bashrc

esac

esac

esac


在这种情况下,它的作用就像跟在一个 ​​awk​​ 命令后面的 ​​grep​

man -k

​man -k​​ 命令列出包含指定单词的手册页。它的工作基本上和 ​​apropos​










$                   man                   -         k                   logrotate


dh_installlogrotate ( 1 ) - install logrotate config files


logrotate ( 8 ) - rotates , compresses , and mails system logs


logrotate . conf ( 5 ) - rotates , compresses , and mails system logs

 




help

​help​

















$                   help

GNU bash , version 4.4.7 ( 1 ) - release ( i686 - pc - linux - gnu )

These shell commands are defined internally . Type ` help ' to see this list.

Type `help name' to find out more about the function ` name '.

Use `info bash' to find out more about the shell in general .

Use ` man - k ' or `info' to find out more about commands not in this list .



A star ( * ) next to a name means that the command is disabled .



job _spec [ & ] history [ - c ] [ - d offset ] [ n ] or hist >

( ( expression ) ) if COMMANDS ; then COMMANDS ; [ elif C >

. filename [ arguments ] jobs [ - lnprs ] [ jobspec . . . ] or jobs >

: kill [ - s sigspec | - n signum | - sigs >

[ arg . . . ] let arg [ arg . . . ]

[ [ expression ] ] local [ option ] name [ = value ] . . .

alias [ - p ] [ name [ = value ] . . . ] logout [ n ]

bg [ job _spec . . . ] mapfile [ - d delim ] [ - n count ] [ - O or >

bind [ - lpsvPSVX ] [ - m keymap ] [ - f file > popd [ - n ] [ + N | - N ]

break [ n ] printf [ - v var ] format [ arguments ]

builtin [ shell - builtin [ arg . . . ] ] pushd [ - n ] [ + N | - N | dir ]

caller [ expr ] pwd [ - LP ]

case WORD in [ PATTERN [ | PATTERN ] . . . ) > read [ - ers ] [ - a array ] [ - d delim ] [ ->

cd [ - L | [ - P [ - e ] ] [ - @ ] ] [ dir ] readarray [ - n count ] [ - O origin ] [ - s >

command [ - pVv ] command [ arg . . . ] readonly [ - aAf ] [ name [ = value ] . . . ] o >

compgen [ - abcdefgjksuv ] [ - o option ] [ > return [ n ]

complete [ - abcdefgjksuv ] [ - pr ] [ - DE ] > select NAME [ in WORDS . . . ; ] do COMM >

compopt [ - o | + o option ] [ - DE ] [ name . . > set [ - abefhkmnptuvxBCHP ] [ - o option ->

continue [ n ] shift [ n ]

coproc [ NAME ] command [ redirections ] shopt [ - pqsu ] [ - o ] [ optname . . . ]

declare [ - aAfFgilnrtux ] [ - p ] [ name [ = v > source filename [ arguments ]

dirs [ - clpv ] [ + N ] [ - N ] suspend [ - f ]

disown [ - h ] [ - ar ] [ jobspec . . . | pid > test [ expr ]

echo [ - neE ] [ arg . . . ] time [ - p ] pipeline

enable [ - a ] [ - dnps ] [ - f filename ] [ na > times

eval [ arg . . . ] trap [ - lp ] [ [ arg ] signal _spec . . . ]

exec [ - cl ] [ - a name ] [ command [ argume > true

exit [ n ] type [ - afptP ] name [ name . . . ]

export [ - fn ] [ name [ = value ] . . . ] or ex > typeset [ - aAfFgilnrtux ] [ - p ] name [ = v >

false ulimit [ - SHabcdefiklmnpqrstuvxPT ] [ l >

fc [ - e ename ] [ - lnr ] [ first ] [ last ] o > umask [ - p ] [ - S ] [ mode ]

fg [ job_spec ] unalias [ - a ] name [ name . . . ]

for NAME [ in WORDS . . . ] ; do COMMAND > unset [ - f ] [ - v ] [ - n ] [ name . . . ]

for ( ( exp1 ; exp2 ; exp3 ) ) ; do COMMAN > until COMMANDS ; do COMMANDS ; done

function name { COMMANDS ; } or name > variables - Names and meanings of so >

getopts optstring name [ arg ] wait [ - n ] [ id . . . ]

hash [ - lr ] [ - p pathname ] [ - dt ] [ name > while COMMANDS ; do COMMANDS ; done

help [ - dms ] [ pattern . . . ] { COMMANDS ; }

 



stat -c

​stat​​ 命令用于显示文件的大小、所有者、用户组、索引节点号、权限、修改和访问时间等重要的统计信息。这是一个非常有用的命令,可以显示比 ​​ls -l​











$                   stat                   .         bashrc


File : . bashrc


Size : 4048 Blocks : 8 IO Block : 4096 regular file


Device : 806h / 2054d Inode : 421481 Links : 1


Access : ( 0644 / - rw - r -- r -- ) Uid : ( 1000 / shs ) Gid : ( 1000 / shs )


Access : 2017 - 08 - 23 15 : 13 : 41.781809933 - 0400


Modify : 2017 - 06 - 21 17 : 37 : 11.875157790 - 0400


Change : 2017 - 06 - 21 17 : 37 : 11.899157791 - 0400


Birth : -


 



​-c​














$                   stat                   -         c                   '%n %a'                   .         bashrc

. bashrc 644


在此命令中, ​​%n​​ 表示每个文件的名称,而 ​​%a​​ 表示访问权限。​​%u​​ 表示数字类型的 UID,而 ​​%U​​表示用户名。











$                   stat                   -         c                   '%n %a'                   bin         /         *


bin / loop 700


bin / move2nohup 700


bin / nohup . out 600


bin / show _release 700





$ stat - c '%n %a %U' bin / *


bin / loop 700 shs


bin / move2nohup 700 shs


bin / nohup . out 600 root


bin / show _release 700 shs




TAB

​cd​​ 时的目录)。它在出现歧义之前尽可能多的填充文件名(多个文件以相同的字母开头。如果您有一个名为 ​​bigplans​​ 的文件,另一个名为 ​​bigplans2017​​的文件会发生歧义,你将听到一个声音,然后需要决定是按下回车键还是输入 ​​2​