前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Linux基础指令与权限

Linux基础指令与权限

作者头像
发布2024-12-01 14:40:14
发布2024-12-01 14:40:14
710
举报
文章被收录于专栏:转自CSDN

登录指令

ssh root@111.11.11.11

ssh 名字@ip 地址可以登录到自己的云服务器

接受主机密匙 然后输入自己的密码即可登录到服务器内

ls指令

语法

ls [选项] [目录或者文件]

功能

用于查找指定目录或者当前目录下的文件

选项

-a 列出所有文件,无论是普通还是隐藏文件 -d 将目录像文件一样显示,而不是显示其下的文件 目录 dir -i 输出文件的 i 结点的索引信息 -k 以K字节的形式表示文件的大小,ls -alk指定文件 -l -l列出问及拿的详细信息 -n 用数字UID,GID代替名称 -F 在每个文件后附上⼀个字符以说明该⽂件的类型,“*”表⽰可执⾏的普通⽂件;“/”表⽰ ⽬录;“@”表⽰符号链接;“|”表⽰FIFOs;“=”表⽰套接字(sockets)。 -r 对目录进行反向排序 -R 对目录进行递归排列 -t 对目录以时间排序 -s 在l文件名后输出文件大小 -1 一行只输出一个文件

示例

root@hcss-ecs-bffb:/home/root_dir# ls -a . .. 1.c 2.c tree_dir root@hcss-ecs-bffb:/home/root_dir# ls -al total 12 drwxr-xr-x 3 root root 4096 Nov 25 20:08 . drwxr-xr-x 3 root root 4096 Nov 25 20:07 .. -rw-r--r-- 1 root root 0 Nov 25 20:08 1.c -rw-r--r-- 1 root root 0 Nov 25 20:08 2.c drwxr-xr-x 2 root root 4096 Nov 25 20:08 tree_dir root@hcss-ecs-bffb:/home/root_dir# ls -alF total 12 drwxr-xr-x 3 root root 4096 Nov 25 20:08 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:07 ../ -rw-r--r-- 1 root root 0 Nov 25 20:08 1.c -rw-r--r-- 1 root root 0 Nov 25 20:08 2.c drwxr-xr-x 2 root root 4096 Nov 25 20:08 tree_dir/ root@hcss-ecs-bffb:/home/root_dir# ls -alFn total 12 drwxr-xr-x 3 0 0 4096 Nov 25 20:08 ./ drwxr-xr-x 3 0 0 4096 Nov 25 20:07 ../ -rw-r--r-- 1 0 0 0 Nov 25 20:08 1.c -rw-r--r-- 1 0 0 0 Nov 25 20:08 2.c drwxr-xr-x 2 0 0 4096 Nov 25 20:08 tree_dir/

pwd指令

语法

pwd

功能

显示用户当前所在目录

示例

root@hcss-ecs-bffb:/home/root_dir# pwd /home/root_dir

cd指令

语法:

cd 目录名

功能

改变工作目录到指定目录名

示例

root@hcss-ecs-bffb:/home/root_dir# ls 1.c 2.c tree_dir root@hcss-ecs-bffb:/home/root_dir# cd tree_dir root@hcss-ecs-bffb:/home/root_dir/tree_dir# pwd /home/root_dir/tree_dir root@hcss-ecs-bffb:/home# ls root_dir root@hcss-ecs-bffb:/home# cd root_dir root@hcss-ecs-bffb:/home/root_dir# pwd /home/root_dir root@hcss-ecs-bffb:/home/root_dir# cd - /home root@hcss-ecs-bffb:/home# pwd /home root@hcss-ecs-bffb:/home# cd root_dir/tree_dir/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# pwd /home/root_dir/tree_dir

目录

• Linux系统中,磁盘上的⽂件和⽬录被组成⼀棵⽬录树,每个节点都是⽬录或⽂件 • 其中普通⽂件⼀定是⽬录树的叶⼦节点 • ⽬录可能是叶⼦(空⽬录),也可能是路上节点 • 理解路径存在的意义:树状组织⽅式,都是为了保证快速定位查找到指定的⽂件,⽽定位⽂件就需要具有唯⼀性的⽅案来进⾏定位⽂件。其中任何⼀个节点,都只有⼀个⽗节点,所以,从根⽬录开始,定位指定⽂件,路径具有唯⼀性 • 绝对路径:⼀般从/开始,不依赖其他⽬录的定位⽂件的⽅式 • 相对路径:相对于当前⽤⼾所处⽬录,定位⽂件的路径⽅式 • 绝对路径⼀般不会随着⽤⼾的路径变化⽽丧失唯⼀性,⼀般在特定服务的配置⽂件中经常被使⽤ • 相对路径因为它的便捷性,⼀般在命令⾏中使⽤较多

touch指令

语法

touch [选项] 文件

功能

touch命令参数可以更改文档或目录的日期时间,包括存取,更改的时间,或者新建一个不存在的文件

选项

-a 仅更改访问时间 -c 仅更改修改时间

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a . .. #创建文件 root@hcss-ecs-bffb:/home/root_dir/tree_dir# touch NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a . .. NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -al total 8 drwxr-xr-x 2 root root 4096 Nov 25 20:41 . drwxr-xr-x 3 root root 4096 Nov 25 20:08 .. -rw-r--r-- 1 root root 0 Nov 25 20:41 NewFile.txt #查看文件状态 root@hcss-ecs-bffb:/home/root_dir/tree_dir# stat NewFile.txt File: NewFile.txt Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fc01h/64513d Inode: 150873 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2024-11-25 20:41:04.691415079 +0800 Modify: 2024-11-25 20:41:04.691415079 +0800 Change: 2024-11-25 20:41:04.691415079 +0800 Birth: 2024-11-25 20:41:04.691415079 +0800 #更改仅访问时间 root@hcss-ecs-bffb:/home/root_dir/tree_dir# touch -a NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# stat NewFile.txt File: NewFile.txt Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fc01h/64513d Inode: 150873 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2024-11-25 20:41:52.654258243 +0800 Modify: 2024-11-25 20:41:04.691415079 +0800 Change: 2024-11-25 20:41:52.654258243 +0800 Birth: 2024-11-25 20:41:04.691415079 +0800 #更改Modify时间 root@hcss-ecs-bffb:/home/root_dir/tree_dir# touch -m NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# stat NewFile.txt File: NewFile.txt Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fc01h/64513d Inode: 150873 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2024-11-25 20:41:52.654258243 +0800 Modify: 2024-11-25 20:42:16.213690010 +0800 Change: 2024-11-25 20:42:16.213690010 +0800 Birth: 2024-11-25 20:41:04.691415079 +0800

mkdir指令

语法

mkdir [选项] 目录名

功能

创建一个指定名字的目录

常用选项

-p 一次创建多个目录

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a . .. NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# mkdir MyDir root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a . .. MyDir NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a MyDir/ NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a MyDir/ . .. root@hcss-ecs-bffb:/home/root_dir/tree_dir# mkdir -p ./MyDir1/MyDir2/MyDir3/MyDir4 root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -a . .. MyDir MyDir1 NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls -aR .: . .. MyDir MyDir1 NewFile.txt ./MyDir: . .. ./MyDir1: . .. MyDir2 ./MyDir1/MyDir2: . .. MyDir3 ./MyDir1/MyDir2/MyDir3: . .. MyDir4 ./MyDir1/MyDir2/MyDir3/MyDir4: . .. root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree ./ ./ ├── MyDir ├── MyDir1 │ └── MyDir2 │ └── MyDir3 │ └── MyDir4 └── NewFile.txt 5 directories, 1 file root@hcss-ecs-bffb:/home/root_dir/tree_dir#

rmdir 指令

语法

rmdir [选项] 目录

功能

删除目录

适用对象

目录有操作权限的使用者

常用选项

-p 递归删除整个目录

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 16 drwxr-xr-x 4 root root 4096 Nov 25 20:47 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 2 root root 4096 Nov 25 20:46 MyDir/ drwxr-xr-x 3 root root 4096 Nov 25 20:47 MyDir1/ -rw-r--r-- 1 root root 0 Nov 25 20:42 NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# rm MyDir/ MyDir1/ NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# rm NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 16 drwxr-xr-x 4 root root 4096 Nov 25 20:51 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 2 root root 4096 Nov 25 20:46 MyDir/ drwxr-xr-x 3 root root 4096 Nov 25 20:47 MyDir1/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# rmdir MyDir root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 12 drwxr-xr-x 3 root root 4096 Nov 25 20:51 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 3 root root 4096 Nov 25 20:47 MyDir1/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree MyDir1/ MyDir1/ └── MyDir2 └── MyDir3 └── MyDir4 root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 12 drwxr-xr-x 3 root root 4096 Nov 25 20:51 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 3 root root 4096 Nov 25 20:47 MyDir1/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# rmdir MyDir1/MyDir2/MyDir3/MyDir4/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 12 drwxr-xr-x 3 root root 4096 Nov 25 20:51 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 3 root root 4096 Nov 25 20:47 MyDir1/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree MyDir1/ MyDir1/ └── MyDir2 └── MyDir3 2 directories, 0 files root@hcss-ecs-bffb:/home/root_dir/tree_dir# rmdir -p MyDir1/ rmdir: failed to remove 'MyDir1/': Directory not empty root@hcss-ecs-bffb:/home/root_dir/tree_dir# rmdir -p MyDir1/MyDir2/MyDir3/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 8 drwxr-xr-x 2 root root 4096 Nov 25 20:52 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ root@hcss-ecs-bffb:/home/root_dir/tree_dir#

rm指令

语法

rm [选项] 文件/目录

功能

删除指定文件或者目录

选项

-f 强制删除文件 -i 删除前逐一询问 -r 递归删除目录下所有文件

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree . ├── A │ └── B │ └── C │ └── D └── NewFile.txt 4 directories, 1 file root@hcss-ecs-bffb:/home/root_dir/tree_dir# rm A/ NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# rm NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 12 drwxr-xr-x 3 root root 4096 Nov 25 20:56 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 3 root root 4096 Nov 25 20:55 A/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# rm -rf A/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree . 0 directories, 0 files root@hcss-ecs-bffb:/home/root_dir/tree_dir#

man指令

语法

man [选项]

功能

提供帮助

常用选项

-k 根据关键字搜索帮助 num 按章节查找 -a 将所有章节都显示 按q退出

章节

1 是普通的命令 2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数需 要加什么头文件) 3 是库函数,如printf,fread 4 是特殊文件,也就是/dev下的各种设备文件 5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义 6 是给游戏留的,由各个游戏自己定义 7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明 8 是系统管理用的命令,这些命令只能由root使用,如ifconfig

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# man man ## MAN(1) Manual pager utils MAN(1) NAME man - an interface to the system reference manuals SYNOPSIS man [man options] [[section] page ...] ... man -k [apropos options] regexp ... man -K [man options] [section] term ... man -f [whatis options] page ... man -l [man options] file ... man -w|-W [man options] page ... DESCRIPTION man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections following a pre-defined order (see DEFAULTS), and to show only the first page found, even if page exists in several sections. The table below shows the section numbers of the manual followed by the types of pages they contain. 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, e.g. /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7), man-pages(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] A manual page consists of several sections. Conventional section names include NAME, SYNOPSIS, CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, ERRORS, ENVIRONMENT, Manual page man(1) line 1 (press h for help or q to quit) ## root@hcss-ecs-bffb:/home/root_dir/tree_dir# man 1 man ## MAN(1) Manual pager utils MAN(1) NAME man - an interface to the system reference manuals SYNOPSIS man [man options] [[section] page ...] ... man -k [apropos options] regexp ... man -K [man options] [section] term ... man -f [whatis options] page ... man -l [man options] file ... man -w|-W [man options] page ... DESCRIPTION man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections following a pre-defined order (see DEFAULTS), and to show only the first page found, even if page exists in several sections. The table below shows the section numbers of the manual followed by the types of pages they contain. 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, e.g. /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7), man-pages(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] A manual page consists of several sections. Conventional section names include NAME, SYNOPSIS, CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, ERRORS, ENVIRONMENT, Manual page man(1) line 1 (press h for help or q to quit) ## root@hcss-ecs-bffb:/home/root_dir/tree_dir# man -a man ## MAN(1) Manual pager utils MAN(1) NAME man - an interface to the system reference manuals SYNOPSIS man [man options] [[section] page ...] ... man -k [apropos options] regexp ... man -K [man options] [section] term ... man -f [whatis options] page ... man -l [man options] file ... man -w|-W [man options] page ... DESCRIPTION man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections following a pre-defined order (see DEFAULTS), and to show only the first page found, even if page exists in several sections. The table below shows the section numbers of the manual followed by the types of pages they contain. 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, e.g. /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7), man-pages(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] A manual page consists of several sections. Conventional section names include NAME, SYNOPSIS, CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, ERRORS, ENVIRONMENT, Manual page man(1) line 1 (press h for help or q to quit) ##

cp指令

语法

cp [选项] 源文件 目标文件/目录

功能

复制文件或者目录,如果没有目标文件/目录,就会自己创建

选项

-f 强制复制文件或目录,不管目标文件是否存在 -i 覆盖文件前询问执行者 -r 递归操作,一并处理整个目录文件

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# echo "hello world">NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# cat NewFile.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 20 drwxr-xr-x 4 root root 4096 Nov 25 21:09 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 3 root root 4096 Nov 25 21:07 A/ drwxr-xr-x 2 root root 4096 Nov 25 21:07 E/ -rw-r--r-- 1 root root 0 Nov 25 21:09 MyFile.txt -rw-r--r-- 1 root root 12 Nov 25 21:09 NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# cp NewFile.txt MyFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# cat MyFile.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# ll total 24 drwxr-xr-x 4 root root 4096 Nov 25 21:09 ./ drwxr-xr-x 3 root root 4096 Nov 25 20:08 ../ drwxr-xr-x 3 root root 4096 Nov 25 21:07 A/ drwxr-xr-x 2 root root 4096 Nov 25 21:07 E/ -rw-r--r-- 1 root root 12 Nov 25 21:10 MyFile.txt -rw-r--r-- 1 root root 12 Nov 25 21:09 NewFile.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# cp NewFile.txt E/text.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree E E └── text.txt 0 directories, 1 file root@hcss-ecs-bffb:/home/root_dir/tree_dir# cat ./E/text.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# cp *.txt ./E/ root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree E E ├── MyFile.txt ├── NewFile.txt └── text.txt 0 directories, 3 files root@hcss-ecs-bffb:/home/root_dir/tree_dir# cp -r ./E ./A root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree A A ├── B │ └── C │ └── D └── E ├── MyFile.txt ├── NewFile.txt └── text.txt 4 directories, 3 files root@hcss-ecs-bffb:/home/root_dir/tree_dir#

mv指令

语法

mv [选项] 源文件/目录 目标文件/目录

功能

-按照 目标文件/目录 的名字 从源文件/目录 按照目标备份过去 -视mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或 将其移至一个新的目录中 -当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。 -当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。

常用选项

-f 强制覆盖/搬运 -i 每次搬运如果存在目标文件,询问是否覆盖

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree . ├── A │ ├── B │ │ └── C │ │ └── D │ └── E │ ├── MyFile.txt │ ├── NewFile.txt │ └── text.txt ├── E │ ├── MyFile.txt │ ├── NewFile.txt │ └── text.txt ├── MyFile.txt └── NewFile.txt 6 directories, 8 files root@hcss-ecs-bffb:/home/root_dir/tree_dir# mv MyFile.txt mv.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# cat mv.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# mv E A/B root@hcss-ecs-bffb:/home/root_dir/tree_dir# tree . ├── A │ ├── B │ │ ├── C │ │ │ └── D │ │ └── E │ │ ├── MyFile.txt │ │ ├── NewFile.txt │ │ └── text.txt │ └── E │ ├── MyFile.txt │ ├── NewFile.txt │ └── text.txt ├── mv.txt └── NewFile.txt 6 directories, 8 files root@hcss-ecs-bffb:/home/root_dir/tree_dir#

cat指令

语法

cat [选项] 文件

功能

查看目标文件内容

常用选项

-b 对非空输出行编号 -n 对输出的所有行编号 -s 不输出多行空行

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# cat -n temp.txt 1 hello worlde 2 hello worlde 3 hello worlde 4 hello worlde 5 hello worlde 6 hello worlde 7 hello worlde 8 hello worlde 9 hello worlde 10 hello worlde 11 hello worlde 12 hello worlde 13 hello worlde 14 hello worlde 15 hello worlde 16 hello worlde 17 hello worlde 18 hello worlde 19 hello worlde 20 hello worlde 21 hello worlde

more指令

语法

more [选项]

功能

more命令,类似于cat

选项

-n 指定输出行数 q 退出more

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# more -1 temp.txt hello worlde ...back 1 page hello worlde ...back 1 page hello worlde ...back 1 page hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde hello worlde --More--(76%)

less指令

语法

less [选项] 文件

功能

-与more cat类似 但是更为强大 -支持分页显示 -支持前后翻滚显示用上下键 -less支持更多功能

选项

-i 忽略搜索时的大小写 -N 显示每一行的行号 / 向下搜索'字符串''功能 ? 向上搜索''字符串''功能 n 重复前一个操作与(/ ?)有关 N 反向重复前一个操作 q 退出

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# less temp.txt ## hello 2 hello 3 hello 4 hello 5 hello 6 ... ##

head指令

语法

head [选项]...[文件]..

功能

查看文件的头部

选项

-n<行数>显示的行数

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# head 10 temp.txt head: cannot open '10' for reading: No such file or directory ==> temp.txt <== hello 0 hello 1 hello 2 hello 3 hello 4

tail指令

语法

tail [选项] [文件]

功能

用于显示文件末尾

选项

-f 循环读取 -n 显示行数

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# tail 10 temp.txt tail: cannot open '10' for reading: No such file or directory ==> temp.txt <== hello 1996 hello 1997 hello 1998 hello 1999 hello 2000

date指令

语法

date +%Y:%m:%d

用法

date [选项]

选项

%H 小时 %M 分钟 %S 秒 %d 日 %X 相当于%H:%M:%S %m 月份 %Y 完整年份 %F 相当于%Y-%m-%d

设定时间

date -s 设定当前时间 date -s 20241129 设置成时间为20241129 00:00:00 date -s 00:00:00 设置时间为00:00:00 date -s "00:00:00 (20241129 或者 2024-11-29)" 设置全部时间 日期和时间不分前后

时间戳

时间变时间戳 date+%s 时间戳变时间 date -d@时间戳

示例

root@hcss-ecs-bffb:~# date Fri Nov 29 07:15:29 PM CST 2024 root@hcss-ecs-bffb:~# date +%Y-%X 2024-07:15:56 PM root@hcss-ecs-bffb:~# date +%F-%X 2024-11-29-07:16:08 PM

cal指令

语法

cal 参数 [年份]

功能

查看日历等时间信息,如果只有一个参数,则表示年份1-9999,如果两个参数 月份和年份

选项

-3 显示系统前一个月,当前月,下一个月的日历 -j 显示在当年中的第几天 -y 显示当前按年份的日历

示例

root@hcss-ecs-bffb:~# cal November 2024 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 root@hcss-ecs-bffb:~# cal 2004 2004 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 4 5 6 7 8 9 10 8 9 10 11 12 13 14 7 8 9 10 11 12 13 11 12 13 14 15 16 17 15 16 17 18 19 20 21 14 15 16 17 18 19 20 18 19 20 21 22 23 24 22 23 24 25 26 27 28 21 22 23 24 25 26 27 25 26 27 28 29 30 31 29 28 29 30 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 1 2 3 4 5 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 30 31 July August September Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 2 3 4 5 6 7 1 2 3 4 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 1 2 3 4 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 31 root@hcss-ecs-bffb:~# cal -3 2024 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 1 2 1 2 3 4 5 6 7 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31

find指令

语法

find pathname -options

功能

用于在文件树中查找文件,并做出相应的处理(可能访问磁盘)

选项

-name 按照文件名查找文件

注意

Linux在find命令在目录结构中搜索文件,并执行指定的操作 Linux下find命令提供了相当多的查找条件,功能强大,find具有强大的功能,所以他的选项也多,其中大部分选项都值得我们花时间来了解一下 即使系统中含有网络文件系统,find命令在该文件系统中同样有效 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长时间

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# find -name mv.txt ./mv.txt

which指令

功能

搜索系统指定的命令

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# which ls /usr/bin/ls root@hcss-ecs-bffb:/home/root_dir/tree_dir# which pwd /usr/bin/pwd

whereis指令

功能

用于找到程序的源 二进制文件或者手册

举例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# whereis ls ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz root@hcss-ecs-bffb:/home/root_dir/tree_dir# whereis pwd pwd: /usr/bin/pwd /usr/share/man/man1/pwd.1.gz

alias指令

功能

设置命令的别名

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# alias lla='ls -l -a' root@hcss-ecs-bffb:/home/root_dir/tree_dir# which lla root@hcss-ecs-bffb:/home/root_dir/tree_dir# lla total 44 drwxr-xr-x 3 root root 4096 Nov 25 21:54 . drwxr-xr-x 3 root root 4096 Nov 25 20:08 .. drwxr-xr-x 4 root root 4096 Nov 25 21:12 A -rw-r--r-- 1 root root 12 Nov 25 21:10 mv.txt -rw-r--r-- 1 root root 12 Nov 25 21:09 NewFile.txt -rw-r--r-- 1 root root 20901 Nov 25 21:54 temp.txt

grep指令

语法

grep [选项] 搜索字符串 文件

功能

在文件中搜索字符串,将找到的打印出来

选项

-i 忽略大小写不同 -n 顺带输出行号 -v 反向选择

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# cat mv.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# grep "g" mv.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# grep -v "g" mv.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# grep "o" mv.txt hello world root@hcss-ecs-bffb:/home/root_dir/tree_dir# grep -n "o" mv.txt 1:hello world

zip/unzip指令

语法

zip 压缩文件.zip 目录或文件

功能

将目录或者文件压缩成zip格式

常用选项

-r 递归处理

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls A mv.txt NewFile.txt temp.txt root@hcss-ecs-bffb:/home/root_dir/tree_dir# zip tmp.zip -r ./* adding: A/ (stored 0%) adding: A/E/ (stored 0%) adding: A/E/text.txt (stored 0%) adding: A/E/NewFile.txt (stored 0%) adding: A/E/MyFile.txt (stored 0%) adding: A/B/ (stored 0%) adding: A/B/C/ (stored 0%) adding: A/B/C/D/ (stored 0%) adding: A/B/E/ (stored 0%) adding: A/B/E/text.txt (stored 0%) adding: A/B/E/NewFile.txt (stored 0%) adding: A/B/E/MyFile.txt (stored 0%) adding: mv.txt (stored 0%) adding: NewFile.txt (stored 0%) adding: temp.txt (deflated 78%) root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls A mv.txt NewFile.txt temp.txt tmp.zip

rzsz

一个工具 用于传输windows与linux机器通过XShell传输文件

下载方式

sudo yum/apt install -y lrzlz

示例

tar指令

打包/解包,不打开直接看内容

语法

tar [选项] 文件与目录

选项

-c: 建立一个压缩文件的参数指令 -x: 解开一个压缩文件的参数指令 -t: 查看tarfile里面的文件 -z: 是否同时具有gzip的属性?亦即是否需要用gzip压缩 -j: 是否同时具有bzip2的属性?亦即是否需要用bzip2压缩 -v: 压缩的过程中显示文件!这个常用,但不建议用在背景执行过程 -f: 使用档名,请留意,在f之后要立即接档名喔 不要再加参数 -C: 解压到指定目录

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# tar -jcvf myzip2.bz2 ./* ./A/ ./A/E/ ./A/E/text.txt ./A/E/NewFile.txt ./A/E/MyFile.txt ./A/B/ ./A/B/C/ ./A/B/C/D/ ./A/B/E/ ./A/B/E/text.txt ./A/B/E/NewFile.txt ./A/B/E/MyFile.txt ./mv.txt ./NewFile.txt ./temp.txt ./tmp.zip root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls A mv.txt myzip2.bz2 NewFile.txt temp.txt tmp.zip root@hcss-ecs-bffb:/home/root_dir/tree_dir# tar -cvf myzip1 ./* ./A/ ./A/E/ ./A/E/text.txt ./A/E/NewFile.txt ./A/E/MyFile.txt ./A/B/ ./A/B/C/ ./A/B/C/D/ ./A/B/E/ ./A/B/E/text.txt ./A/B/E/NewFile.txt ./A/B/E/MyFile.txt ./mv.txt ./myzip2.bz2 ./NewFile.txt ./temp.txt ./tmp.zip root@hcss-ecs-bffb:/home/root_dir/tree_dir# tar -zcvf myzip.gz ./* ./A/ ./A/E/ ./A/E/text.txt ./A/E/NewFile.txt ./A/E/MyFile.txt ./A/B/ ./A/B/C/ ./A/B/C/D/ ./A/B/E/ ./A/B/E/text.txt ./A/B/E/NewFile.txt ./A/B/E/MyFile.txt ./mv.txt ./myzip1 ./myzip2.bz2 ./NewFile.txt ./temp.txt ./tmp.zip root@hcss-ecs-bffb:/home/root_dir/tree_dir# ls A mv.txt myzip1 myzip2.bz2 myzip.gz NewFile.txt temp.txt tmp.zip

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir# tar -tf myzip.gz ./A/ ./A/E/ ./A/E/text.txt ./A/E/NewFile.txt ./A/E/MyFile.txt ./A/B/ ./A/B/C/ ./A/B/C/D/ ./A/B/E/ ./A/B/E/text.txt ./A/B/E/NewFile.txt ./A/B/E/MyFile.txt ./mv.txt ./myzip1 ./myzip2.bz2 ./NewFile.txt ./temp.txt ./tmp.zip

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir/A# tar -xvf ../myzip.gz ./A/ ./A/E/ ./A/E/text.txt ./A/E/NewFile.txt ./A/E/MyFile.txt ./A/B/ ./A/B/C/ ./A/B/C/D/ ./A/B/E/ ./A/B/E/text.txt ./A/B/E/NewFile.txt ./A/B/E/MyFile.txt ./mv.txt ./myzip1 ./myzip2.bz2 ./NewFile.txt ./temp.txt ./tmp.zip root@hcss-ecs-bffb:/home/root_dir/tree_dir/A# ls A B E mv.txt myzip1 myzip2.bz2 NewFile.txt temp.txt tmp.zip

示例

tar -zxvpf 保留属性备份

bc命令

语法

bc

功能

进行浮点运算

示例

root@hcss-ecs-bffb:/home/root_dir/tree_dir/A# bc bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 1+1 2 2+2 4 1.1+2.2 3.3 quit

uname -r指令

语法

uname [选项]

功能

uname用来获取电脑和操作系统的相关信息

常用选项

-a或-all详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称

不演示示例了

热键

用法

[Tab] 命令补全 与 档案补全 [Tab]+[Tab] 如果有多个补全 显示当前可选补齐的选项 [Ctrl]+[c] 终止当前程序 [Ctrl]+[d] 正常结束当前程序

shutdown指令

语法

shutdown [选项]

选项

-h 关机 -r 延迟关机 -t [time] 几(time)秒后关机

扩展命令

安装和登录命令:login、shutdown、halt、reboot、install、mount、umount、chsh、 exit. last; 文件处理命令:file、mkdir、grep、dd、find、mv、ls、diff、cat、ln; ·系统管理相关命令:df、top、free、quota、at、lp、adduser、groupadd、kill、crontab; ·网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、 mail. nslookup; 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、 who; 其它命令:tar、unzip、gunzip、unarj、mtools、man、unendcode、uudecode。

命令及其运行原理

用户使用linux系统的时候,是使用外壳程序访问linux(核心 kernel),用户无法直接访问linux的底层,需要"工具'访问,外壳程序就是工具,也就是命令行解释器

翻译使用者命令 把命令交给核心出来 把处理结果返回给使用者

Linux权限

不同用户

用户分为

超级用户 可以在系统下做任何事情 命令符为# 普通用户 可以在linux做权限允许的事情 命令符为$

命令 su

语法

su [用户名]

功能

切换用户

Linux权限管理

访问者

文件和文件目录的所有者 u-user 文件和文件目录的所有者所在的组 g-group 其他用户 o-other

被访问物

类型权限 d 目录 - 一般文件 l 软连接 b 块设备文件 p 管道文件 c 字符设备文件 s 套接口文件 使用权限 r 读 对文件对文件可读 对目录 可读目录内内容 w 写 对文件 可更改文件内容 对目录 可更改目录内文件 x 执行 对文件 文件是否可执行 对目录 是否可进入目录 - 对应权限无 权限表示方法 字符表示法 rwx 八进制表示法 111 222 333

文件访问权限设置

chmod命令

语法

chmod [参数] 权限 文件名

选项

R 递归更改目录文件的权限 只有root和所有者可更改 用户标识符 + 权限范围增加权限代号表示权限 - 权限范围取消权限代号表示权限 = 权限范围赋予权限代号表示权限 用户符号 u user g group o other a all

用法

chmod (u/g/o/a) (+/-/=) (r/w/x) 文件 chmod 8进制数 文件

chown命令

语法

chown [参数] 用户名 文件名

功能

修改文件的拥有者 (choice own)

用法

chown -R other-user my-file

chgrp命令

语法

chgrp [选项] 用户组名 文件名

功能

修改文件或目录的所有组 (change group)

用法

chgrp -R other-group group-file

umask指令

语法

umask [选项]/八进制

功能

查看或修改文件编码 文件的默认权限666 目录的默认权限777 创建出来的文件权限时 mask&(~umask)

用法

umask 查看 umask 000 设置为000

file指令

语法

file [选项] 文件或目录

常用选项

-c 详细显示指令执行过程,便于排错或分析程序执行的情形 -z 尝试取解读压缩文件的内容

sudo指令

语法

sudo 要以管理员权限执行的命令

目录权限

可执行权限:如果目录没有可执行权限,则无法cd到目录中 可读权限:如果目录没有可读权限,则无法用ls等命令查看目录中的文件内容. 可写权限:如果目录没有可写权限,则无法在目录中创建文件,也无法在目录中删除文件.

粘滞键

chomd +t 目录

当设置了粘滞键文件只能又它的创建者删除

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 登录指令
  • ls指令
  • pwd指令
  • cd指令
    • 目录
    • touch指令
    • mkdir指令
    • rmdir 指令
    • rm指令
    • man指令
    • cp指令
    • mv指令
    • cat指令
    • more指令
    • less指令
    • head指令
    • tail指令
    • date指令
    • cal指令
    • find指令
    • which指令
    • whereis指令
    • alias指令
    • grep指令
    • zip/unzip指令
    • rzsz
    • tar指令
    • bc命令
    • uname -r指令
    • 热键
    • shutdown指令
    • 扩展命令
    • 命令及其运行原理
    • Linux权限
      • 不同用户
        • 用户分为
        • 命令 su
      • Linux权限管理
        • 访问者
        • 被访问物
      • 文件访问权限设置
        • chmod命令
        • chown命令
        • chgrp命令
        • umask指令
        • file指令
        • sudo指令
        • 目录权限
        • 粘滞键
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档