【Linux】常见指令 _基础入门【初阶 _查阅文档】

快速导航

1.ls

1.1  -l

1.2  -a

2.pwd

3.whoami

4.touch

5.cd

6.mkdir

7.rm

8.man

9.cp

10.mv

11.cat

12.tac

13.more

14. less

15.head

16.tail

17.date

18.cal

19.find

20.which

21.grep

22.uname

1.ls

ls: 显示当前路径下的文件,不同的目录下文件不同,ls执行之后所显示的内容也有所差异。

[zwx@VM-20-3-centos ~]$ ls
linux-code  Makefile

[zwx@VM-20-3-centos linux-code]$ ls
code  csdn  lesson  LICENSE  README.en.md  README.md  script  typora

[zwx@VM-20-3-centos lesson]$ ls
lesson1  lesson10  lesson11  lesson2  lesson3  lesson4  lesson5  lesson6  lesson7  lesson9

1.1  -l

 -l 选项: 以列表的形式显示文件的各种属性。

[zwx@VM-20-3-centos lesson]$ ls -l
total 40
drwxrwxr-x 4 zwx zwx 4096 Sep 23 23:03 lesson1
drwxrwxr-x 2 zwx zwx 4096 Oct  7 15:57 lesson2
drwxrwxr-x 2 zwx zwx 4096 Jul 22 19:39 lesson3

注意:以d开头说明是一个目录,还有以 - 开头的文件,是普通文件或者可执行程序。

[zwx@VM-20-3-centos lesson3]$ ls -l
total 48
-rwxrwxr-x 1 zwx zwx  8360 Jul 22 19:39 a.out
-rw-rw-r-- 1 zwx zwx    76 Jul 21 12:20 code.c

1.2  -a

-a 选项:显示当先路径下的所有文件(包括隐藏文件)。

[zwx@VM-20-3-centos lesson]$ ls -a
.  ..  lesson1  lesson10  lesson11  lesson2  lesson3  lesson4  lesson5  lesson6  lesson7  lesson9

和没有使用 -a 选项相比,多了一个 . 文件和 ..文件

很多人不理解显示的文件中...是什么,Linux下是有隐藏文件的,Linux中隐藏文件不容易理解,这里和windows下的隐藏文件来对比理解。windows下想要查看隐藏文件很简单,只需要把查看中隐藏的项目打开即可。

 Linux下的隐藏文件都是以 . 开头的,. 表示当先路径,..表示上一级路径。

隐藏文件在实际的开发中有其很大用途:它可以隐藏系统的一些配置文件,避免误删除等操作

2.pwd

pwd: 显示当前的工作目录。

[zwx@VM-20-3-centos lesson]$ pwd
/home/zwx/linux-code/lesson

[zwx@VM-20-3-centos linux-code]$ pwd
/home/zwx/linux-code

3.whoami

whoami: 查看当前用户(zwx登录的是我的账户)。

[zwx@VM-20-3-centos linux-code]$ whoami
zwx

4.touch

touch file:在当前路径下创建一个文件。

[zwx@VM-20-3-centos cmd]$ touch file.txt
[zwx@VM-20-3-centos cmd]$ ls
file.txt

5.cd

cd: 更改目录,切换工作路径,可以在工作路径下进行来回的切换(cd .. 返回上一级目录)。

[zwx@VM-20-3-centos linux-code]$ cd lesson
[zwx@VM-20-3-centos lesson]$ pwd
/home/zwx/linux-code/lesson
[zwx@VM-20-3-centos lesson]$ cd lesson1
[zwx@VM-20-3-centos lesson1]$ pwd
/home/zwx/linux-code/lesson/lesson1

6.mkdir

mkdir directory:创建一个目录。

[zwx@VM-20-3-centos lesson1]$ ls
myproc  project
[zwx@VM-20-3-centos lesson1]$ mkdir directory
[zwx@VM-20-3-centos lesson1]$ ls
directory  myproc  project

在当前目录下创建a/b/c/d/e/f,那么直接使用mkdir a/b/c/d/e/f是不行的,这时候就需要加上一个-p选项

[zwx@VM-20-3-centos lesson2]$ mkdir a/b/c/d/e/f
mkdir: cannot create directory ‘a/b/c/d/e/f’: No such file or directory
[zwx@VM-20-3-centos lesson2]$ mkdir -p a/b/c/d/e/f

注意:创建出来的a~f都是一个目录,当然也包括f

7.rm

rm: 不加任何选项可以删除普通文件,加上-r选项可以删除目录。

[zwx@VM-20-3-centos cmd]$ ls
directory  file.txt
[zwx@VM-20-3-centos cmd]$ rm file.txt
[zwx@VM-20-3-centos cmd]$ rm -r directory
[zwx@VM-20-3-centos cmd]$ ls
[zwx@VM-20-3-centos cmd]$ 

rm还有两个常用的选项,-f可以进行强制删除,即使文件是只读的也会直接删除;

-i选项在删除的时候会先提示用户是否删除,删除的话输入yes/y,不删除输入no/n。

[zwx@VM-20-3-centos lesson2]$ ls
mydir
[zwx@VM-20-3-centos lesson2]$ rm -rif mydir
[zwx@VM-20-3-centos lesson2]$ ls
[zwx@VM-20-3-centos lesson2]$ mkdir mydir
[zwx@VM-20-3-centos lesson2]$ rm -ri mydir
rm: remove directory ‘mydir’? y

注意:当 -f 和 -i 选项一起使用的时候,是不会提示是否进行删除的。 

8.man

man: 查询指令、函数等

查询的时候可以带数字项,shell中输入man对数字项是这样解释的:

1   Executable programs or shell commands  
2   System calls (functions provided by the kernel)
3   Library calls (functions within program libraries)
4   Special files (usually found in /dev)
5   File formats and conventions eg /etc/passwd
6   Games
7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8   System administration commands (usually only for root)
9   Kernel routines [Non standard]

在我们使用的时候,可以不带这些数字项,因为在shell中也会自动识别。

man ls

space建可以往下翻阅,q可以直接退出。

man指令在我们查询的时候还是非常推荐使用的,像一些函数的功能,以及需要什么头文件都是可以直接在shell中查询的。

9.cp

cp: 拷贝目录/文件。

cp file/directory  path

无论是创建、删除,还是拷贝重命名等命令,都需要注意文件的路径,因为文件的路径分为两种:绝对路径和相对路径。

绝对路径:从根目录开始,一步一步定位到工作目录,如cd  /home/zwx/code/c

相对路径:以当前目录为参考,逐步定位到工作目录,如果我们在/home/zwx下想要进入cd  /home/zwx/code/c可以直接cd ./code/c。

[zwx@VM-20-3-centos c]$ cp test.c ../
[zwx@VM-20-3-centos c]$ cd ..
[zwx@VM-20-3-centos code]$ pwd
/home/zwx/code
[zwx@VM-20-3-centos code]$ ls
c  cpp  test.c

如果我们想要复制目录到一个路径下,就需要加上 -r  选项。

[zwx@VM-20-3-centos code]$ cp c ../
cp: omitting directory ‘c’
[zwx@VM-20-3-centos code]$ cp -r c ../
[zwx@VM-20-3-centos code]$ cd ..
[zwx@VM-20-3-centos ~]$ ls
c  code  csdn  lesson

cp中同样存在-f选项,也是强制复制的意思,不管目标路径是否存在目标文件,cp中还存在一个-i选项,选项的作用是如果目标文件在拷贝的路径下有同名的文件,会提示一下是否进行覆盖。

[zwx@VM-20-3-centos code]$ cp -ir c ../
cp: overwrite ‘../c/test.c’? y

10.mv

mv: 移动文件到另一个路径下/重命名。

  • 文件的移动: mv 源文件 + 路径       
  • 文件的重命名:  mv 源文件名 + 文件名

 test1和test2在是在同一路径下的两个目录,可以使用mv命令将myproc移动到test1中。

[zwx@VM-20-3-centos test2]$ mv myproc ../test1/
[zwx@VM-20-3-centos test2]$ tree ../
../
|-- test1
|   `-- myproc
`-- test2

mv指令同样存在-i和-f选项,这两个选项和在rm中意义是相同的。

11.cat

cat filename:查看文件内容。

12.tac

tac filename:反向查看文件内容(从最后一行开始)。

13.more

more: 查看文件。

在测试more和less的时候,我们先使用脚本创建一个有一万行"hello world"的文件file.txt(每一行都有对应的行号)。

脚本指令:

count=0; while [ $count -le 10000 ]; do echo "hello world ${count}"; let count++; done > file.txt

more指令进行的时候可以使用回车键一行一行下翻,也可以使用空格键一屏一屏的下翻

 这里实际上是电脑一个屏幕的内容,上面的没有截取, 最下面是读取文件的进度条。

14. less

less: 查看文件

less其实就是为了弥补more查看文件时只能往下翻,不能往上翻的缺点,使用less查看的时候就可以使用上下键来进行翻阅查看

less查看文件的时候最下面的一行是没有进度条的。

注意:more和less命令查看文件的中退出可以使用q键。

15.head

head filename: 默认查看文件内容的前十行。

[zwx@VM-20-3-centos test2]$ head file.txt
hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9

如果想要查看的不是前十行,可以在执行指令的时候加上行数。

[zwx@VM-20-3-centos test2]$ head -3 file.txt
hello world 0
hello world 1
hello world 2

16.tail

tail filename: 默认查看文件内容的后十行。

[zwx@VM-20-3-centos test2]$ tail file.txt
hello world 9991
hello world 9992
hello world 9993
hello world 9994
hello world 9995
hello world 9996
hello world 9997
hello world 9998
hello world 9999
hello world 10000

和head指令相同的是,使用时可以指定行数。

[zwx@VM-20-3-centos test2]$ tail -3 file.txt
hello world 9998
hello world 9999
hello world 10000

17.date

[zwx@VM-20-3-centos test2]$ date
Sun Oct  9 21:30:11 CST 2022

直接输入date终端会显示输入时的年月日和时间以及星期,那如果我们只想看年份和月份,或者只想看年月日,就可以这样实现:

[zwx@VM-20-3-centos test2]$ date +%Y-%m-%d:%H:%M:%S
2022-10-09:21:32:46

以上的格式分别对应时间的年月日时分秒。

时间转化为时间戳(从1970年1月1日午夜开始,到现在的秒数),date加上%s可以直接查看。

[zwx@VM-20-3-centos test2]$ date +%s
1665322423

复制一下之前的时间戳,转化为具体时间:

[zwx@VM-20-3-centos test2]$ date -d@1665322423
Sun Oct  9 21:33:43 CST 2022

18.cal

cal: 显示公历(阳历)日历。

  • cal: 显示当前月份的日历
  • cal  year: 就会显示一年的日历
  • cal month year: 输入月份的日历

19.find

find  directory -name filename: 查找文件filename所在位置。

[zwx@VM-20-3-centos test2]$ find ~ -name lesson10
/home/zwx/linux-code/lesson/lesson10
[zwx@VM-20-3-centos test2]$ find ~ -name myproc
/home/zwx/linux-code/csdn/test1/myproc
/home/zwx/linux-code/lesson/lesson10/myproc
/home/zwx/linux-code/lesson/lesson1/myproc
/home/zwx/linux-code/lesson/lesson1/myproc/myproc
/home/zwx/linux-code/lesson/lesson9/myproc
/home/zwx/linux-code/lesson/lesson11/myproc

20.which

which: 查找指令所在的路径。

Linux下一切皆文件;指令就在文件中;

[zwx@VM-20-3-centos test2]$ which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[zwx@VM-20-3-centos test2]$ which mkdir
/usr/bin/mkdir

21.grep

grep  "string"   filename: 查找你想在filename找的内容。

 选项-v,这个指令是反向选择的意思;比如说我们所查找的内容在第1行,一共有十行内容,那么输出的就是2-10行。

 选项-i, 查找的时候忽略大小写。

[zwx@VM-20-3-centos lesson1]$ cat test.c
#include <stdio.h>

int main()
{
	printf("hello Linux");
	pRINtf("hello Linux");
	PRINTF("hello Linux");
	return 0;
}
[zwx@VM-20-3-centos lesson1]$ grep "printf" test.c
	printf("hello Linux");
[zwx@VM-20-3-centos lesson1]$ grep -i "printf" test.c
	printf("hello Linux");
	pRINtf("hello Linux");
	PRINTF("hello Linux");

22.uname 

[zwx@VM-20-3-centos lesson1]$ uname -r
3.10.0-1160.45.1.el7.x86_64
[zwx@VM-20-3-centos lesson1]$ uname -a
Linux VM-20-3-centos 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

-r 和 -a 选项都可以用来查看Linux的体系结构和内核版本

Linux的基础指令到这里就结束了,码字不易,希望阅读到这里的铁汁动动发财的小手,来个点赞+收藏+关注。

  • 23
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 37
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

new出新对象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值