Linux中权限和指令

发布于:2024-09-19 ⋅ 阅读:(12) ⋅ 点赞:(0)

💥1、Linux基本指令

1.1 mv 指令

mv指令是move的缩写,用来移动或重命名文件、目录,经常用来备份文件或目录。

  • mv old_name new_name: 重命名文件或目录
  • mv file /path/to/directory: 移动文件到指定目录
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 16 20:13 dir
-rw-r--r-- 1 root root   78 Sep 16 20:04 test.c
root@hcss-ecs-8f13:~/112# mv test.c name.c
root@hcss-ecs-8f13:~/112# mv dir mydir
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
-rw-r--r-- 1 root root   78 Sep 16 20:04 name.c
root@hcss-ecs-8f13:~/112# mv name.c ../
root@hcss-ecs-8f13:~/112# ls -l ../
total 12
drwxr-xr-x 3 root root 4096 Sep 16 20:16 112
-rw-r--r-- 1 root root   78 Sep 16 20:04 name.c
drwx------ 3 root root 4096 Sep 16 11:32 snap
root@hcss-ecs-8f13:~/112# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
root@hcss-ecs-8f13:~/112# mv mydir ../
root@hcss-ecs-8f13:~/112# ls -l ../
total 16
drwxr-xr-x 2 root root 4096 Sep 16 20:16 112
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
-rw-r--r-- 1 root root   78 Sep 16 20:04 name.c
drwx------ 3 root root 4096 Sep 16 11:32 snap
root@hcss-ecs-8f13:~/112# ls -l
total 0
root@hcss-ecs-8f13:~/112# 

1.2 cat 指令

语法: cat [选项] [文件]
功能: 读取文件内容并将其输出到标准输出设备(通常是终端或屏幕)
常用选项:

  1. -b 对非空行输出行编号
  2. -n 对输出的所有行编号
  3. -s 不输出多行空行
root@hcss-ecs-8f13:~/112# pwd
/root/112
root@hcss-ecs-8f13:~/112# touch test.c
root@hcss-ecs-8f13:~/112# ls
test.c
root@hcss-ecs-8f13:~/112# nano test.c
root@hcss-ecs-8f13:~/112# cat test.c
#include <stdio.h>

int main()
{
    printf("hello world\n");
    return 0;
}
root@hcss-ecs-8f13:~/112# cat -b test.c
     1	#include <stdio.h>

     2	int main()
     3	{
     4	    printf("hello world\n");
     5	    return 0;
     6	}
root@hcss-ecs-8f13:~/112# cat -n test.c
     1	#include <stdio.h>
     2	
     3	int main()
     4	{
     5	    printf("hello world\n");
     6	    return 0;
     7	}
root@hcss-ecs-8f13:~/112# cat -s test.c
#include <stdio.h>

int main()
{
    printf("hello world\n");
    return 0;
}
root@hcss-ecs-8f13:~/112# 

cat会把文件中的所有内容显示出来,因此cat不适合看大文本,适合看小文本。

如果cat后什么都不跟,则通常情况下我们从键盘输入什么,屏幕上就输出什么。

root@hcss-ecs-8f13:~/112# cat
hello world
hello world
Are you ok?
Are you ok?

tac指令: 功能和cat相反,逆序打印文件内容。

root@hcss-ecs-8f13:~/112# ls 
mydir  name.c
root@hcss-ecs-8f13:~/112# tac name.c
}
    return 0;
    printf("hello world\n");
{
int main()

#include <stdio.h>
root@hcss-ecs-8f13:~/112# 

1.3 echo 指令

功能:

  • echo 文本: 在终端(命令行界面)上输出文本
  • echo 文本>文件(如果文件不存在则新建): 将文本写入到文件中
root@hcss-ecs-8f13:~/112# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
root@hcss-ecs-8f13:~/112# echo "hello world"
hello world
root@hcss-ecs-8f13:~/112# echo "hello world">test.txt
root@hcss-ecs-8f13:~/112# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 16 20:41 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir/
-rw-r--r-- 1 root root   12 Sep 16 20:41 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
root@hcss-ecs-8f13:~/112# 

1.4 重定向

Linux下一切皆文件,Linux下,显示器、键盘、网卡、普通文件等都可以看作文件,只不过显示器只有写方法,向显示器打印,其实就是向显示器文件写入,无法从显示器读取数据。键盘只有读方法。而普通文件具有读写两个功能。
echo指令默认把后面跟的文本写入显示器文件中。cat指令后面如果没有跟任何文件,则默认从键盘文件中读取数据,然后写入到显示器文件中。

上面我们用echo 文本>(输出重定向)文件将文本写入到文件中,如果文件不存在则新建

root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:47 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
root@hcss-ecs-8f13:~/112# echo "hello world">test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# echo "aa" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
aa
root@hcss-ecs-8f13:~/112# 

通过测试我们还可以得出,如果指定的文件里面有内容,则会先清空原内容,然后再写入(并不是覆盖)。

  • 当文件不存在时,>文件名可以新建空文件
  • 当文件存在时,>文件名可以清空文件内容
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
root@hcss-ecs-8f13:~/112# >test.txt
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root    0 Sep 16 22:58 test.txt
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root   12 Sep 16 22:59 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# >test.txt
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root    0 Sep 16 22:59 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
root@hcss-ecs-8f13:~/112# 

>指定文件写入内容会把原内容删除掉,如果我们就不想删除原始内容,将新内容追加到原始内容后,可以用>>(追加重定向)实现。

root@hcss-ecs-8f13:~/112# cat test.txt
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# echo "Hello" >> test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# 

除了>>>进行输出,还有输入重定向符号<。前面我们说了如果cat后面什么都不跟,则默认从键盘上取数据。所以如果我们用输入重定向符号<指定文件,则会输出文件中的内容。

root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# cat < test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# 


网站公告

今日签到

点亮在社区的每一天
去签到