419-Linux基础(常用命令详解1)

1、ls 命令

ls是英文单词list的缩写.用来查看文件目录的属性。

例如直接输入ls按回车,查看根目录的文件以及目录。

ls /

输出如图:

bin dev initrd.img media proc sbin tmp vmlinuz
boot etc lib mnt root srv usr workspace
cdrom home lost+found opt run sys var

ls ‐l -l参数代表以列表的方式显示。

where@ubuntu:~$ ls ‐l
总用量 31224
‐rw‐‐‐‐‐‐‐ 1 where where 31916032 77 18:39 core
drwxr‐xr‐x 2 where where 4096 78 12:05 Desktop
drwxr‐xr‐x 2 where where 4096 625 16:53 Documents
drwxr‐xr‐x 2 where where 4096 625 16:53 Downloads
‐rw‐r‐‐r‐‐ 1 where where 8980 76 15:13 examples.desktop
‐rw‐‐‐‐‐‐‐ 1 where where 12 78 18:08 helloworld.txt
drwxr‐xr‐x 2 where where 4096 625 16:53 Music
drwxrwxr‐x 2 where where 4096 730 18:28 mydir
drwxrwxr‐x 2 where where 4096 78 18:18 nfs
drwxr‐xr‐x 2 where where 4096 625 16:53 Pictures
drwxr‐xr‐x 2 where where 4096 625 16:53 Public
drwxr‐xr‐x 2 where where 4096 625 16:53 Templates
drwxr‐xr‐x 2 where where 4096 625 16:53 Videos
drwxr‐xr‐x 2 where where 4096 625 16:53 Videos
#第一个字符d 代表这是一个目录文件。
	# ‐ 代表普通文件
	# c 字符设备文件
	# b 块设备文件
	# p 管道文件
	# l 链接文件
	# s socket文件
	
#后面的rwxr‐xr‐x字符,代表user、group、other对文件所拥有的权限,rwx代表该用户拥有读写执行的权限。r‐x代表同一组的用户拥有的读和执行权限,后一个r‐x代表其他用户拥有读和执行权限。

# 2代表文件硬链接的计数,表示该文件有两个硬链接。

# where 文件所属的用户名。
# where 文件所属的用户组。
# 4096 文件大小,单位字节。
# 625 16:53 文件最后被修改的日期。
# Videos 文件名

ls ‐a -a参数代表all的意思,表示把所有的文件都罗列出来,包括隐藏文件,点号开头的在Linux中都表示隐藏文件。

where@ubuntu:~$ ls ‐a
. Desktop .local .thunderbird
.. .dmrc .mozilla Videos
.bash_history Documents Music .viminfo
.bash_logout Downloads mydir .vimrc
.bashrc .emacs.d nfs .Xauthority
.cache examples.desktop .pam_environment .xinputrc
.compiz .gconf Pictures .xsession‐errors
.config .gvfs .profile .xsession‐errors.old
core helloworld.txt Public
.dbus .ICEauthority Templates

ls ‐ld [filename] 代表只列出目录文件的属性。

2、stat命令

查看文件的访问时间,修改时间等文件属性。

where@ubuntu:~$ stat hello
文件:"hello"
大小:7341 块:16 IO 块:4096 普通文件
设备:801h/2049d Inode:1878587 硬链接:1
权限:(0775/‐rwxrwxr‐x) Uid:( 1000/ where) Gid:( 1000/ where)
最近访问:2016‐08‐12 22:38:25.272181218 +0800
最近更改:2016‐08‐12 22:38:25.272181218 +0800
最近改动:2016‐08‐12 22:38:25.272181218 +0800
创建时间:‐
  • 访问时间,是指通过指令如cat、vi等来查看的文件的最近一次时间。
  • 更改时间,是指修改文件内容的最近一次时间。
  • 改动时间,是指修改文件属性的最近一次时间。

注意: 访问时间是内容更改后,第一次访问的时间,后面再次访问的时候访问时间不会改变。
在这里插入图片描述

Inode:

  • 相当于一个编号,每个文件都对应一个编号,就是Inode;
  • Inode也是存放在文件属性中的;
  • 同一个分区Inode唯一,不同分区Inode可能会一样!

3、cd命令

cd 是 change directory的缩写,表示改变当前所在路径。

在这里插入图片描述

4、pwd命令

pwd 是英文print working directory 显示当前所在路径。

where@ubuntu:~$ pwd
/home/where

5、which命令

寻找可执行文件 ,并在PATH环境变量里面寻找。

where@ubuntu:~$ which ls
/bin/ls
where@ubuntu:~$ which reboot
/sbin/reboot
where@ubuntu:~$

6、touch命令

touch [OPTION] [FILE]
  • 文件存在,将每个文件的访问及修改时间都更新为目前的时间。
    在这里插入图片描述
  • 如果文件不存在,则创建一个字节数为0的文件。
‐a #只更新访问时间,不改变修改时间
‐c #不创建不存在的文件
‐m #只更新修改时间,不改变访问时间
‐r file #使用文件file的时间更新文件的时间
‐t #将时间修改为参数指定的日期,如:07081556代表781556

例如:

touch ‐a file1 #更新file1文件的访问时间
touch ‐r file1 file2 #使用file1的时间来更新文件file2

7、mkdir 命令

mkdir 是make directory的英文缩写。

mkdir [OPTION] [DIRECTORY]

创建目录DIRECTORY,可以一次创建多个。OPTION如果是p,表示可以连同父目录一起创建。

where@ubuntu:~$ mkdir ‐p dir/dir/dir/dir/dir
where@ubuntu:~$

8、rm 命令

rm是remove的英文缩写。

rm [options] [file]
‐f ‐‐force #强制删除,不询问是否要删除。
‐r ‐‐recursive #递归删除,包括文件夹中的内容。

9、mv命令

mv是英文单词move的缩写。可以用来移动文件夹或者文件,也可以用来更改文件名。

mv [srcfile] [destfile]
mv file / #把文件file移动到根目录中。
mv file file_bak #把文件file重命名为file_bak

10、cp命令

cp是英文单词copy的缩写,表示拷贝文件。

cp [srcfile] [destfile]

可以用来拷贝普通文件:

cp file file_bak #拷贝一份file为file_bak

可以用来拷贝目录:

cp dir dir_bak ‐r #拷贝一个目录dir为dir_bak, ‐r参数代表递归拷贝,把dir目录中的文件也拷贝过去

11、cat命令

cat英文单词concatenate连锁的缩写,用来查看文件内容,以及将几个文件连成一个文件。

不填文件参数,默认的情况下是从标准输入中获取内容:

where@ubuntu:~$ cat #cat从标准输入中获取内容,并且打印一次。
helloworld
helloworld

查看文件mydir/test.cpp文件:

where@ubuntu:~$ cat mydir/test.cpp
#include <stdio.h>
int main()
{
printf("%d\n", 3.0 );
return 0;
}

将文件file1 file2连成file3文件:

cat file1 file2 > file3

12、more命令

more 是我们最常用的工具之一,最常用的就是显示输出的内容,然后根据窗口的大小进行分页显示,并且提示文件的百分比。

参数如下:

+num #从第num行开始显示;
‐num #定义每屏显示num行;

例如:

more +105 file #从第10行开始,每一页5

打开之后的动作:
在这里插入图片描述

13、less

less工具也是对文件或其它输出进行分页显示的工具

‐f #强制打开文件,二进制文件显示时,不提示警告;
‐N #在每行前输出行号;

打开之后的动作:
在这里插入图片描述


/Name:搜索内容
在这里插入图片描述

14、locate 命令

全盘寻找文件,文件名部分匹配,只要有包含该字符串的都罗列出来,这个指令查找速度很快,它需要一个数据库,这个数据库由每天的例行工作(crontab)程序来更新。当我们建立好这个数据库后,就可以方便地来搜寻所需文件了。

where@ubuntu:~$ locate sources.list
/etc/apt/sources.list
/etc/apt/sources.list.d
/etc/apt/sources.list~
/usr/share/doc/apt/examples/sources.list
/usr/share/man/de/man5/sources.list.5.gz
/usr/share/man/es/man5/sources.list.5.gz
/usr/share/man/fr/man5/sources.list.5.gz
/usr/share/man/it/man5/sources.list.5.gz
/usr/share/man/ja/man5/sources.list.5.gz
/usr/share/man/man5/sources.list.5.gz
/usr/share/man/pl/man5/sources.list.5.gz
/usr/share/man/pt/man5/sources.list.5.gz
/var/lib/dpkg/info/python‐pkg‐resources.list
/var/lib/dpkg/info/python3‐pkg‐resources.list

马上创建的文件没办法使用locate查找到:

where@ubuntu:~$ touch newfile
where@ubuntu:~$ locate newfile
where@ubuntu:~$

如果想马上更新可以使用一下指令:

updatedb

15、find 命令

直接在全文件系统上搜寻,功能强大,速度慢。

find [path] [‐option] [ ‐print ‐exec ‐ok command ] {} \;
path: #要执行查找的目录。
‐option: #查找的具体方法。
‐print: #find命令将匹配的文件输出到标准输出。
‐exec: #find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {} \;,注意{}和
\;之间的空格。
‐ok: #和‐exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令
之前,都会给出提示,让用户来确定是否执行。
find / ‐name filename #在根目录里面搜索文件名为filename的文件
find /etc ‐name *s* #在目录里面搜索带有s的文件
find /etc ‐name *S #在目录里面搜索以s结尾的文件
find /etc ‐name s* #在目录里面搜索以s开头的文件

find / ‐amin ‐10 #在系统中搜索最后10分钟访问的文件
find / ‐atime ‐2 #查找在系统中最后48小时访问的文件
find / ‐mmin ‐5 #查找在系统中最后5分钟修改过的文件
find / ‐mtime ‐1 #查找在系统中最后24小时修改过的文件
find / ‐ctime ‐1 #查找在系统中最后24小时被改变状态的文件

find / ‐user username #查找在系统中属于用户username的文件
find / ‐group groupname #groupname 查找在系统中属于groupname的文件

find / ‐empty #查找在系统中为空的文件或者是文件夹
find / ‐inum 3 #查找inode号为3的文件
find / ‐type d #查找为文件类型为文件夹的文件d为文件夹
					f #普通文件
					d #目录文件
					l #链接文件
					b #块设备文件
					c #字符设备文件
					p #管道文件
					s #socket文件

查找当前目录中所有c源文件的文件属性:

find ./ ‐name "*.c" ‐exec ls ‐l {} \;

16、grep 命令

搜索内容中是否包含指定的字符串,并打印出该行。

常用参数有:

‐i ‐‐ignore‐case #忽略字符大小写的差别。
‐v #输出没有指定字符串的文件
‐c #只输出匹配行的计数。
‐R #连同子目录中所有文件一起查找。

查找当前目录c源文件中,包含头文件stdio.h的所有文件:

find ./ ‐name "*.c" |xargs grep stdio.h

17、管道命令

用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为command 2的输入。

管道命令操作符是:”|”它只能处理经由前面一个指令传出的正确输出信息,对错误信息信息没有直接处理能力。

ls ‐l | grep hello
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

liufeng2023

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

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

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

打赏作者

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

抵扣说明:

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

余额充值