Linux基础指令 二
Linux基础指令2
文章目录
echo与cat
echo是输出内容到显示器文件中,linux中显示器或者各种设备都是文件
可以使用>进行重定向,表示输出内容到>后面的文件里,这会将文件的原内容清空,也可以用>>重定向,不清空文件
如果重定向的文件不存在,则会创建该文件再进行输出
cat可以获取输入设备文件中的内容,同样可以通过<进行输入重定向
除了cat之外,还有tac,表示从最后一行开始倒序打印文件的内容
echo "hello"
hello
echo "hello" >test/test.cpp//>会清空test.cpp的内容,然后再输入hello
cat < test/test.cpp//获取test文件的内容
hello //返回的内容
echo " world" >>test/test.cpp//>>表示不清空文件,只将world输出到文件最后面
cat < test/test.cpp//获取test文件的内容
hello
world //返回的内容,会自动换行,cat -n < 表示不自动换行
tac < test/test.cpp//倒序获取
world
hello//返回的内容
echo >test/test.cpp //输出空内容到文件里,可以相当于清空该文件
cp与mv
cp是拷贝指令,会将文件及其内容共拷贝到目标文件,目录也是文件,语法为cp 选项 源 目的
可以加入多个源,也可以通过-r进行递归拷贝,将目录及其子目录全部拷贝
mv是移动指令,将源文件移动到目标文件处,语法与cp一样,支持多个源和-r,还可以指定移动后的文件名称
cp test/test.cpp .//将test.cpp拷贝到当前目录下
cp -r test/test.cpp test/test1 . //将test.cpp和test1目录全都拷贝出来
mv test/test.cpp test/test1//将test文件移动到test1目录中
mv test/test1/test.cpp test/test1/test1.cpp //也可以将文件原地拷贝,重命名
more与less
二者都用于查看大文件,点击q退出浏览模式
more打开文件后输出一屏内容后会暂停,点击回车下翻一行,空格下翻一屏,只能下翻,不能上翻
lsee打开文件后可以用上下控制翻动,用/向下查找关键字,?向上查找关键字,
head与tail
二者用于选择文件的前多少行和后多少行
head -num file 输出file的前num行
tail -num file 输出file的后num行
可以二者结合来固定截取某个段落
head -5 test.cpp > temp.txt//将test.cpp的前五行放到临时文件
tail -3 temp.txt//将临时文件的后三行输出,即text.cpp的三到五行
head -5 test.cpp | tail -3 //管道,可以通过|将前一条指令的执行结果直接传递给后一条指令,省去了中间创建temp.txt的过程
//管道相当于一个公用内存块
date
使用指定格式展示时间
date
Mon May 11 20:23:58 CST 2026//默认格式
date +%Y_%m-%d*%H:%M.%S//用各种符号链接的年月日时分秒
2026_05-11*20:26.38
date +%F-%X //使用默认的格式输出内容,其中F表示年月日,X表示时分秒
2026-05-11-20:29:11
date +%s //小写s,表示输出时间戳,是从1970.1.1-0:0:0开始计时的秒数
1778502581
find,which,whereis,grep
find用于查询文件,在目录树下查询指定名称的文件,*表示通配符,可以匹配任何选项
which用于专门搜索系统命令的目录,相比于find,它只搜索命令,更快
whereis在二者之间,先去usrbin下搜索命令,再去查找
grep是在大文件内检索关键词的命令,指定关键词,指定文件,会将该文件中含有该关键词的行都列举出来,并将关键词高亮,可以-n加上行号,-i不区分大小写,-v反向匹配,只输出不含关键词的行
find test//在当前目录下查询test
test
test/test.cpp
test/test1//返回三个结果
find *.cpp //进入test文件夹后,查询所有cpp文件
test.cpp
which find //which查找find命令
/usr/bin/find //返回地址
whereis find //whereis查找find命令
find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/info/find.info-1.gz /usr/share/info/find.info.gz /usr/share/info/find.info-2.gz
cat test/test.cpp//显示test.cpp的全部内容
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;v
return 0;
}
grep 'm' test/test.cpp//查找含有字母m的行
#include <iostream>
int main()
alias
alias是为一条指令起别名,比如将ls -al 这条指令起别名叫lsal,可以通过将lsal设置为空来取消别名
起的别名只在内存中,重启或者换一个终端,别名就会失效
ls -al//输入指令,下面是输出
total 32
drwxr-xr-x 3 root root 4096 May 11 20:42 .
drwxr-xr-x 3 root root 4096 May 11 20:42 ..
drwxr-xr-x 3 root root 4096 May 11 19:35 test
-rw-r--r-- 1 root root 1977 May 11 20:43 基础指令1.md
-rw-r--r-- 1 root root 4174 May 11 20:43 基础指令2.md
-rw-r--r-- 1 root root 2646 May 11 20:43 基础指令3.md
-rw-r--r-- 1 root root 434 May 11 20:43 基础指令4.md
alias lsal='ls -al'//起别名
lsal//输入该指令的别名,输出相同结果
total 32
drwxr-xr-x 3 root root 4096 May 11 20:42 .
drwxr-xr-x 3 root root 4096 May 11 20:42 ..
drwxr-xr-x 3 root root 4096 May 11 19:35 test
-rw-r--r-- 1 root root 1977 May 11 20:43 基础指令1.md
-rw-r--r-- 1 root root 4174 May 11 20:43 基础指令2.md
-rw-r--r-- 1 root root 2646 May 11 20:43 基础指令3.md
-rw-r--r-- 1 root root 434 May 11 20:43 基础指令4.md
zip,unzip,tar
zip是将文件压缩成.zip格式,unzip则用于.zip的解压
tar则是用于.tar.gz文件,简称为.tgz的压缩和解压,一般要配合选项使用,-z是使用gzip压缩,-c是压缩,-x解压,-f放在最后一个,后面紧跟文件名,-C(大写)是将文件解压到指定目录
zip test.zip test/ //压缩test目录
adding: test/ (stored 0%) //输出,文件夹压缩完成
unzip test.zip -d ./test
Archive: test.zip
creating: ./test/test/ // 输出
tree //tree一下,可以看到压缩包test.zip和test下的test目录
.
├── test
│ ├── test
│ ├── test.cpp
│ └── test1
├── test.zip
├── 基础指令1.md
├── 基础指令2.md
├── 基础指令3.md
└── 基础指令4.md
tar -zcvf 基础指令.tar.gz 基础指令1.md 基础指令2.md 基础指令3.md 基础指令4.md//压缩一个基础指令.tgz
基础指令1.md
基础指令2.md
基础指令3.md
基础指令4.md //输出的压缩过程
tar -zxvf 基础指令.tar.gz -C ./test //将基础指令.tgz解压到test文件夹里
基础指令1.md
基础指令2.md
基础指令3.md
基础指令4.md//输出解压过程
tree//tree一下
.
├── test
│ ├── test
│ ├── test.cpp
│ ├── test1
│ ├── 基础指令1.md
│ ├── 基础指令2.md
│ ├── 基础指令3.md
│ └── 基础指令4.md //压缩包被解压到这里
├── test.zip
├── 基础指令.tar.gz//生成了的压缩指令.tgz
├── 基础指令1.md
├── 基础指令2.md
├── 基础指令3.md
└── 基础指令4.md
tab,ctrl+c和ctrl+d
tab可以自动补全指令,比如
root@louis:/home/louis/code/study/Linux/基础# wh//按下tab,猜测可能要用的指令
whatis wheel.exe whereis whesvc_assets.dll which while who whoami.exe
whealogr.dll where.exe whesvc.dll whhelper.dll which.debianutils whiptail whoami whqlprov.mof
root@louis:/home/louis/code/study/Linux/基础# whi//加一个i,再tab,会缩小范围
which which.debianutils while whiptail
root@louis:/home/louis/code/study/Linux/基础# which //加一个c,按tab,会直接补全h,再按tab,再次缩小范围
which which.debianutils
root@louis:/home/louis/code/study/Linux/基础# which.debianutils //加个.,再按tab,补全后续
ctrl+c表示停止当前程序
ctrl+d表示键盘输入结束,EOF,也可以取代exit
shell命令的运行原理
shell指令是在linux内核外面的一层外壳,直接与内核交互很繁琐,程序员与外壳交互,外壳与内核交互,会更方便,也会过滤非法请求,保护内核
/user/bin/bash是命令行解释器,它就相当于外壳的实体
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)