不能不知道的常用Linux指令

目录

一、ls

 二、pwd

三、cd

四、touch、rm

 五、mkdir、rmdir

六、man

 七、cp

八、mv

九、cat

 十、more

十一、less

十二、head、tail

 十三、find

十四、grep

十五、zip/unzip

十六、tar


一、ls

功能:列出当前目录下所有的文件和目录。还可查看除文件的文件名之外的其他信息。

用法:ls 选项 目录或文件

常用选项:

-a   列出所有的文件,包括以 . 开头的隐藏文件。

-l    列出所有的文件的详细信息(创建时间、大小、权限等)。

-r   对目录进行反向排列

-t   对目录以时间排序

-s   输出文件名同时和文件大小

例如:

[yzj@localhost linux]$ ls -a
.  ..  6  lesson4  lesson5  lesson6
[yzj@localhost linux]$ ls -l
total 0
drwxrwxr-x. 2 yzj yzj   6 Jul  2 00:49 6
drwxrwxr-x. 2 yzj yzj  50 Jun  4 18:40 lesson4
drwxrwxr-x. 2 yzj yzj 178 Jun  8 23:54 lesson5
drwxrwxr-x. 3 yzj yzj  17 Jul  2 00:51 lesson6
[yzj@localhost linux]$ ls -r
lesson6  lesson5  lesson4  6
[yzj@localhost linux]$ ls -t
lesson6  6  lesson5  lesson4
[yzj@localhost linux]$ ls -s
total 0
0 6  0 lesson4  0 lesson5  0 lesson6

 二、pwd

功能:查看当前所在的目录 

[yzj@localhost python]$ pwd
/home/yzj/python

三、cd

功能:改变工作目录,将当前目录改换到指定目录下。

[yzj@localhost linux]$ mkdir file
[yzj@localhost linux]$ ls
6  file  lesson4  lesson5  lesson6
[yzj@localhost linux]$ ls -t
file  lesson6  6  lesson5  lesson4
[yzj@localhost linux]$ rmdir file
[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6

用法:cd 目录名

常用用法:

cd .. 返回上一级目录

cd ~ 返回家目录

cd - 返回最近访问的目录

cd 绝对路径:进入这个路径下

[yzj@localhost linux]$ cd ..                       //从linux返回上一层目录      
[yzj@localhost ~]$ cd ~                            //从linux回到家目录下
[yzj@localhost ~]$ cd python                       //从家目录进入/home/yzj/python目录下
[yzj@localhost python]$ cd ~
[yzj@localhost ~]$ cd -                            //返回最近访问的目录
/home/yzj/python                       
[yzj@localhost python]$ cd /home/yzj/linux        //使用绝对路径进入linux目录下
[yzj@localhost linux]$ 

四、touch、rm

功能:创建一个新文件、修改文件或目录的时间。rm删除文件

用法:touch 文件名 rm 文件名

常用选项:

rm -r 递归的删除目录及其下所有的文件

rm -i 删除前逐一确认

[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6
[yzj@localhost linux]$ touch lesson3        //创建一个文件lesson3
[yzj@localhost linux]$ ls
6  lesson3  lesson4  lesson5  lesson6
[yzj@localhost linux]$ ls -t                //将文件显示出来并按照创建时间排序
lesson3  lesson6  6  lesson5  lesson4
[yzj@localhost linux]$ rm lesson3
[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6

 五、mkdir、rmdir

功能:在当前目录下创建新的目录。

用法:mkdir 目录名       rmdir目录名

[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6
[yzj@localhost linux]$ mkdir file
[yzj@localhost linux]$ ls
6  file  lesson4  lesson5  lesson6
[yzj@localhost linux]$ ls -t
file  lesson6  6  lesson5  lesson4
[yzj@localhost linux]$ rmdir file
[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6

六、man

功能:查看手册获取帮助,点击q退出

用法:man 命令

[yzj@localhost linux]$ man man        //查看man的用法

显示

MAN(1)                                            Manual pager utils                                           MAN(1)

NAME
       man - an interface to the on-line reference manuals

等内容

 七、cp

功能:复制文件或目录

用法:cp 选项 源目录或文件 目标目录或文件

常用选项:

-f 强制复制不论目的文件或目录是否存在

-r 递归的将目录下的文件和子目录一起处理

[yzj@localhost linux]$ cp -r lesson5 lesson7
[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6  lesson7
[yzj@localhost linux]$ cd lesson7
[yzj@localhost lesson7]$ ls
cfile  cfile.c  exec  exec.c  hello  hello.c  Makefile  myshell  myshell.c  test.txt  wait  wait.c
[yzj@localhost lesson7]$ cp -f hello.c helloworld.c 
[yzj@localhost lesson7]$ ls
cfile  cfile.c  exec  exec.c  hello  hello.c  helloworld.c  Makefile  myshell  myshell.c  test.txt  wait  wait.c

八、mv

功能:移动文件或者重命名文件,第二个参数如果是目标目录则为移动

用法:mv 源文件或目录 目标文件或目录

[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6  lesson7
[yzj@localhost linux]$ mv lesson7 /home/yzj/python    //移动
[yzj@localhost linux]$ cd /home/yzj/python
[yzj@localhost python]$ ls
'  lesson7  test.py  test.txt
[yzj@localhost python]$ cd /home/yzj/linux
[yzj@localhost linux]$ ls
6  lesson4  lesson5  lesson6
[yzj@localhost python]$ mv lesson7 newlesson    //重命名
[yzj@localhost python]$ ls
'  newlesson  test.py  test.txt

九、cat

功能:查看文件内容

用法:cat 选项 文件名

常用选项:

-b 非空的行进行编号

-n  所有行进行编号

-s  不输出多行空行,多行空行只输出一行

[yzj@localhost python]$ cat -b test.txt
     1	aaa
     2	bbb
     3	ccc





     4	aaa
     5	bb
     6	cc
     7	aaa
[yzj@localhost python]$ cat -n test.txt
     1	aaa
     2	bbb
     3	ccc
     4	
     5	
     6	
     7	
     8	
     9	aaa
    10	bb
    11	cc
    12	aaa
[yzj@localhost python]$ cat -s test.txt
aaa
bbb
ccc

aaa
bb
cc
aaa

 十、more

功能:和cat类似,使用enter向下翻一行,space向下翻一屏;b(back)向上翻页。从前向后读取文件,启动时加载整个文件。q退出。

用法:more 选项 文件名

常用选项:

+n 从第n行开始显示(+3 从 第三行开始显示)

-n 一屏只显示n行(+10一屏只显示10行)

十一、less

功能:和more相似,在查看前不加载整个文件。

用法:less 选项 文件名

查看两个文件 less 文件1名 文件2名(:n向后一个文件:p向前一个文件)

查找文件中的某一个或某几个字符 /字符或?字符如果查找到的字符有多个则用n向下查看用N向上查看

常用选项:

-i 忽略搜索时大小写

-N 显示行号

十二、head、tail

功能:head用来显示开头某个数量的文字区块,tail显示结尾的某个数量的文字区块

用法:head 选项 文件名

常用选项:

-n 显示多少行(-10 显示十行)

[yzj@localhost python]$ tail -n10 test.py
#    print globals()


# print locals()
# print globals()
# 前面有_的表示一个私有变量无法被import
# def _Add(x, y):
#     return x + y
# print x
# 写注释的另一种方法
[yzj@localhost python]$ head -10
[yzj@localhost python]$ head -10 test.py
#!/usr/bin/python
# coding=utf-8
print "hellojane"
count = 0
miles = 1000.0
name = 'Bob'
kilomiters = 1.609*miles
# print type(count)
# print type(miles)
# print type(name)
[yzj@localhost python]$ tail -10 test.py
#    print globals()


# print locals()
# print globals()
# 前面有_的表示一个私有变量无法被import
# def _Add(x, y):
#     return x + y
# print x
# 写注释的另一种方法
[yzj@localhost python]$ head -2 test.py
#!/usr/bin/python
# coding=utf-8

 十三、find

功能:查找

用法:find 路径 选项(经常和通配符*联系使用)

常用选项:-name按照文件名查找

[yzj@localhost python]$ find /home/yzj/python -name "test.*"
/home/yzj/python/test.py
/home/yzj/python/test.txt
/home/yzj/python/newlesson/test.txt

十四、grep

功能:搜索字符串并找到其所在行打印出来

用法:grep 选项 搜索字符串 文件

常用选项:

-i :忽略大小写

-v:反向输出,显示没有指定字符串的那一行

[yzj@localhost python]$ grep -i PRINT test.py
print "hellojane"
# print type(count)
# print type(miles)
# print type(name)
# print type(kilomiters)
# print count
# print kilomiters
# print n
# print type(n)
[yzj@localhost python]$ grep -v print test.py
#!/usr/bin/python
# coding=utf-8
count = 0
miles = 1000.0
name = 'Bob'
kilomiters = 1.609*miles
# n = 1000 * 1000 * 1000 * 1000 * 1000
# n = 'hehe'
[yzj@localhost python]$ grep -v -n print test.py
1:#!/usr/bin/python
2:# coding=utf-8
4:count = 0
5:miles = 1000.0
6:name = 'Bob'
7:kilomiters = 1.609*miles
14:# n = 1000 * 1000 * 1000 * 1000 * 1000

十五、zip/unzip

功能:压缩目录或文件、解压目录或文件

用法:zip 选项 压缩后文件名 

unzip 选项 压缩文件的文件名 

常用选项:

-r 递归处理,将指定目录下所有的子目录一并处理

解压时

-n 解压缩时不覆盖原来的文件

-d 路径:在某个路径下解压

十六、tar

功能:打包、解包,即将一大串文件或目录变成一个总的文件

用法:tar 主选项+副选项 文件或目录

重要选项:

5个主选项中选取一个:

-c: --create 新建一个压缩文档,即打包

-x: --extract,--get解压文件

-t: --list,查看压缩文档里的所有内容

-r:--append 向压缩文档里追加文件

-u:--update 更新原压缩包中的文件

辅助选项中任意选择:

-z:是否需要用gzip压缩或解压?一般带有.tar.gz或.tgz

-j:是否需要用bzip2压缩或解压?一般格式为xx.tar.bz2

-v:显示压缩过程中显示文件

-f:使用文档名,后直接跟文档名

-C:解压到指定目录

仅打包不压缩tar -cvf 

打包后用gzip压缩 tar -zcvf

查看使用gzip压缩的文件中有哪些内用 tar -tf

将压缩的文件解压缩在某路径下 tar -xvf 路径

猜你喜欢

转载自blog.csdn.net/jane_yao/article/details/81125740