Linux常用命令

  • cd命令 
cd        #进入用户主目录 
cd ~      #进入用户主目录 
cd -      #返回进入此目录之前所在目录 
cd ..     #返回上一级目录 
cd ../..  #返回上两级目录 
cd !$     #把上个命令的参数作为cd 参数使用 
cd /      #进入根目录
cd .      #当前目录
  • ls命令
ls -a #列出目录所有文件,包含以.开始的隐藏文件
​ls -A #列出除.及..的其它文件
​ls -r #反序排列
​ls -t #以文件修改时间排序
​ls -S #以文件大小排序
​ls -h #以易读大小显示
​ls -l #除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来
  • mkdir命令
mkdir /root/game     #在root用户的家目录下创建一个game目录
mkdir game           #相对路径的方式来创建一个目录
mkdir 目录1 目录2    #创建多个目录
mkdir -p star/moon   #在root目录下创建一个star目录,同时在star目录下在创建一个目录moon
  • rmdir命令
rmdir -p parent/child/child11  #当parent子目录被删除后使它也成为空目录的话,则顺便一并删除
  • touch命令
touch testfile                #修改文件的时间属性 
touch file                    #创建一个名为“file”的新的空白文件 
  •  cp命令
cp a.txt /work/           #复制文件a.txt到work目录
cp a.txt /work/b.txt      #复制文件a.txt到work目录,并重命名为b.txt
cp a.txt b.txt /work/     #复制文件a.txt和b.txt到work目录
cp -i a.txt /work/        #-i 若目标文件已存在,则会询问是否覆盖
cp -r job/ /work/         #-r  递归处理,将指定目录下的文件与子目录一并处理。
  • mv命令
mv test.log test1.txt                    #将文件test.log重命名为test1.txt
mv llog1.txt log2.txt log3.txt /test3    #将文件log1.txt,log2.txt..移动到根的test3目录中
mv -i log1.txt log2.txt                  #将文件file1改为file2,如果file2存在,询问是否覆盖
mv * ../                                 #移动当前文件夹下的所有文件到上一级目录
  •  rm命令
rm -i *.log   #删除任何.log文件;删除前逐一询问确认
rm -rf test   #删除test子目录及子目录中所有档案删除,并且不用一一确认
rm -- -f*     #删除以-f开头的文件
rm  -vi a.txt #删除前确认

rm -f, –force             #强制删除。忽略不存在的文件,不提示确认
rm -i,-interactive       #进行交互式删除,在删除前需要确认
rm -r, -R, --recursive    #递归删除目录及其内容
rm -v, –verbose           #详细显示进行的步骤
  • pwd命令
pwd #pwd是Print Working Directory的缩写,其功能是显示当前所在工作目录的全路径。
  • ln命令
ln -s log2013.log link2013 
#为log2013.log文件创建软链接link2013,log2013.log丢失,link2013将失效。

ln log2013.log ln2013
给文件创建硬链接,为log2013.log创建硬链接ln2013,log2013.log与ln2013的各项属性相同。

#软链接:不可以删除源文件,删除源文件导致链接文件找不到,出现文件红色闪烁
#硬链接:可以删除源文件,链接文件可以正常打开

#软链接:
#1.软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
#2.软链接可以 跨文件系统 ,硬链接不可以
#3.软链接可以对一个不存在的文件名进行链接
#4.软链接可以对目录进行链接

#硬链接:
#1.硬链接,以文件副本的形式存在。但不占用实际空间。
#2.不允许给目录创建硬链接
#3.硬链接只有在同一个文件系统中才能创建
  • find命令
find . -name "*.c"     #将目前目录及其子目录下所有延伸档名是 c 的文件列出来。
find . -ctime -20      #将目前目录及其子目录下所有最近 20 天内更新过的文件列出。
find -size +1000c      #查找大于1K的文件
find . -type f         #将目前目录其其下子目录中所有一般文件列出

                       #-type 
                       #c : 文件类型是 c 的文件。
                       #d: 目录
                       #c: 字型装置文件
                       #b: 区块装置文件
                       #p: 具名贮列
                       #f: 一般文件
                       #l: 符号连结
                       #s: socket
                       #-pid n : process id 是 n 的文件
  • type/file/stat命令及区别
type cd           #用来查看命令类型,以区别是内部命令还是外部命令。
file 123.txt      #用来探测给定文件的类型。
stat README.md    #主要用于显示文件或文件系统的详细信息
  • cat命令
cat my.txt                          #一次显示整个文件
cat my.txt readme.txt >bind.txt     #用cat合并文件,将几个文件合并为一个文件
cat >my.txt                         #我们在键盘上输入的内容,就到my.txt去了,按ctrl+d,结束
tac log.txt                         #反向列示
cat -n f1.txt                       #查看f1.txt文件的内容,并且由1开始对所有输出行进行编号。
cat -e f1.txt                       #在输出内容的每一行后面加一个$符号。
  •  less命令
less info.log                #查看info.log的日志。
ps -aux | less -N            #ps查看进程信息并通过less分页显示。
history | less               #查看命令历史使用记录并通过less分页显示。
less info.log  error.log     #查看info.log的文件和error.log的文件,这是查看两个文件
#(1)less info.log   在打开一个文件的情况下,可以通过 :e error.log 打开error.log 第二个文件
#(2):n浏览下一个文件 :p浏览上一个文件


(1)/name    在日志中匹配name的下一个,    ?name  在日志中匹配name的上一个
(2)n 查看下一个      N 查看上一个
(3) ctrl + f 或者 f 下一页      ctrl + b 或者 b 上一页
(4) ctrl + d 或者 d   下半页      ctrl  + u  或者  u 上半页
(5)j  下一行   k 上一行    这里   上下键也可以
(6)g 第一行开始位置       G最后一行(末尾)
(7)q   退出
(8)h   帮助


less(选项)(参数) 
选项如下:
    -e:文件内容显示完毕后,自动退出;
  -f:强制显示文件;
  -g:不加亮显示搜索到的所有关键词,仅显示当前显示的关键字,以提高显示速度;
  -l:搜索时忽略大小写的差异;
  -N:每一行行首显示行号;
  -s:将连续多个空行压缩成一行显示;
  -S:在单行显示较长的内容,而不换行显示;
  -x<数字>:将TAB字符显示为指定个数的空格字符。
  • tail命令
tail -n 5 day1.txt     #显示文件最后5行内容
tail -n +5 day1.txt    #从第5行开始显示文件
tail -f test.log       #循环查看文件内容,用Ctrl+c来终止

-f 循环读取(常用于查看递增的日志文件)
​-n<行数> 显示行数(从后向前)
  • head命令
head -n 5 day1.txt     #显示day1.txt前5行内容
head -n -5 day1.txt    #输出文件除了最后5行的全部内容
head -c 20 test.log    #显示文件file的前20个字节

-q 隐藏文件名
-v 显示文件名
-c<字节> 显示字节数
-n<行数> 显示的行数
  •  echo命令
echo hello world             #显示普通字符串
echo "\"It is a test\""      #显示转义字符,结果 "It is a test"
echo $HOSTNAME               #输出变量名对应的值
echo "hello world" > 1.txt   #输出字符串到指定文件
echo `date`                  #这里使用的是反引号 `, 而不是单引号 ',结果将显示当前日期,显示命令执行结果
  • 管道命令 |
ls /tmp|grep '.txt'  #找到/tmp目录下所有txt文件
#用"|"符号来连接两个命令,以前面命令的标准输出作为后面命令的标准输入
  •  特殊字符 重定向相关 命令
echo "Java,zhen de hen xihuan ni" > qingshu.txt     #把左边的输出放到右边的文件里去
echo 'just for success'>> README.md                 #把文字追加到文件的结尾

ls > index.txt           #把ls的结果打印到index.txt文件中
ls bb >aa                #>不会把错误的指令执行的结果保存到aa文件中(bb文件不存在,报error)
ls bb 2>aa               #改成2>才能保存错误的结果这个2表示错误的执行结果
ls file >>aa 2>>bb       #把正确信息输入到aa,错误信息输入到bb(工作中较为常用),追加方式
ls &>aa                  #把命令的执行结果(无论是错误的还是正确的)都保存到aa文件中,是覆盖
ls cc >>aa  2>&1         #把错误和正确信息都输入aa,这里是追加方式(企业较为常用)

1.>>    追加重定向,把文字追加到文件的结尾
2.>     重定向符号,清空原文件所有内容,然后把文字覆盖到文件末尾
3.<     输入重定向
4.<<    将输入结果输入重定向
  • grep命令
grep 'nux' day1.txt                 #从文件中查找关键词
grep 'nux' day1.txt readme.md       #从多个文件中查找关键词
cat day1.txt | grep ^n              #找出已u开头的行内容
cat day1.txt | grep ^[^l]           #输出非u开头的行内容
cat day1.txt |grep 行$              #输出以'行'结尾的行内容
cat day1.txt |grep -E "行|数"       #显示包含”行“或者”数“字符的内容行
cat day1.txt |grep -w 'tail'        #完全匹配关键词 -w ,-i忽略大小写,--color 高亮显示
cat day1.txt |grep -A 10 'tail'    #-A, 显示匹配后N行,-B, 显示匹配前N行,-C, 显示匹配前后N行



# grep(Global Regular Expression Print)全局正则表达式搜索
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the file name for each match
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

'egrep' means 'grep -E'.  'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input.  With no FILE, read . if a command-line
-r is given, - otherwise.  If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>

 

  • &和&&,|和||区别
ping 192.168.120.204 > test.log &  # &  表示任务在后台执行
redis-server &                     # 在后台运行redis-server
echo 'hello‘ && echo 'nihao'       # '&&' 表示前一条命令执行成功时,才执行后一条命令   
echo 'hello' | wc -l               # '|' 表示管道,上一条命令的输出,作为下一条命令参数
cat nofile || echo "failed"        # '||' 表示上一条命令执行失败后,才执行下一条命令
  • vi命令
基本上vi可以分为三种状态,分别是命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下,
1) 命令行模式command mode)
  控制屏幕光标的移动,字符、字或行的删除,移动复制某区段及进入Insert mode下,或者到 last line mode。
2) 插入模式(Insert mode)
  只有在Insert mode下,才可以做文字输入,按「ESC」键可回到命令行模式。
3) 底行模式(last line mode)
  将文件保存或退出vi,也可以设置编辑环境,如寻找字符串、列出行号……等。

注意:
1.在「命令行模式(command mode)」下按一下字母「i」就可以进入「插入模式(Insert mode)」,这时候你就可以开始输入文字了。
2.按「i」切换进入插入模式「insert mode」,按"i"进入插入模式后是从光标当前位置开始输入文件;
3.在「命令行模式(command mode)」下,按一下「:」冒号键进入「Last line mode」

: w filename (输入 「w filename」将文章以指定的文件名filename保存)
: wq (输入「wq」,存盘并退出vi)
: q! (输入q!, 不存盘强制退出vi)

 

  •  图鉴介绍(图转)

 

参考:

Linux命令之find的用法

Linux中常用操作命令总结

linux常用命令详解 

cat命令和tac命令

 每天一个linux命令(13):less 命令

Linux echo 命令 

Linux 重定向 2>&1 , 1>&2 

每天一个linux命令(39):grep 命令 

Linux——vi命令详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值