Linux文件和文件夹操作

发布于:2024-03-27 ⋅ 阅读:(63) ⋅ 点赞:(0)

一、文件操作

功能项 命令实例 作用
文件创建 vi /opt/learn/hello.txt

在目录/opt/learn下创建文件hello.txt并进入vi编辑界面

touch /opt/learn/test 在目录/opt/learn下创建空白文件test
cat > /opt/catfile 创建文件catfile并在屏幕上输入内容,最后按 Ctrl + D 退出
文件查看 vi /etc/passwd 在vi编辑器中输出文本内容
cat /etc/passwd 在屏幕输出文本内容
more /etc/passwd 分屏输出文本内容
less /etc/passwd 分屏输出文本内容并按需加载文件(适用于大文件的查看)
head -n 10 /etc/passwd 只输出文件的头10行
tail -n 20 /etc/passwd 只输出文件末尾的20行
strings /bin/ls 查看二进制文件中的可打印字符
文件操作 cp hello.txt /opt/test 把文件hello.txt复制到文件夹/opt/test下
cp hello.txt /opt/test/hello.cp 把文件hello.txt复制到文件夹/opt/test下并重命名为hello.cp
mv hello.txt /opt/test 把文件hello.txt剪切到文件夹/opt/test下
mv hello.txt /opt/test/hello.mv 将文件hello.txt剪切到文件夹/opt/test下并重命名为hello.mv
mv hello.txt hello2.txt 重命名
rm /opt/test/hello.cp 删除文件
rm -f /opt/test/hello.mv 强制删除文件,不会有提示信息
du -sk hello.txt 查看文件hello.txt的大小(以k为单位)
链接 In -s hello.txt shello 为hello.txt文件创建一个名为shello的软链接(类似于快捷方式)
In -d hello.txt dhello 为hello.txt文件创建一个名为dhello的硬链接(硬链接表示所有文件中更改任意一个,其他文件的所有属性会跟着变化,如大小,更新时间,权限等)

二、文件夹操作

功能项 命令或格式 作用
ls /tree ls [option] [file/directory] 显示指定目录下的所有文件或文件夹
ls 显示当前目录的内容
ls -l 显示当前目录的详细内容
ls -a 显示当前目录下的所有文件,包括隐藏文件
ls *.txt 显示当前目录下所有以.txt为后缀名的文件
ls /opt/training 显示目录/opt/training下的内容
ls -R /opt/ 列出所有/opt目录及其目录的内容
tree /opt 用树状结构显示目录及文件
pwd pwd 显示当前所在的目录
cd cd directory 切换到指定目录
cd 切换到当前用户所有的主目录

cd ..

回退到当前目录的上一级目录
cd /opt/learn 用绝对路径切换到/opt/training目录下
cd ../../ 使用相对路径切换到当前目录的上一级的上一级目录下
cd . 切换到当前用户,相当于什么也没做
mkdir mkdir [option] [director1] [directory2] 创建目录
mkdir /opt/other 在目录/opt/learn/下创建目录other
mkdir dir2 dir3 dir4 同时创建dir2 dir3 dir4三个目录
mkdir -p /dir1/dir2/dir3 同时创建三层目录
rmdir rmdir dir 1 删除一个空目录
其他操作 cp -r /opt/learn /opt/learn2 拷贝文件夹
mv /opt/learn2 /opt/learn3 重命名文件夹
rm -rf /opt/learn3 强制删除文件夹
⚠️ rm -rf / 从根目录开始,递归并强制删除所有文件和目录
⚠️ rm -rf /* 强制删除根目录下所有文件和子目录
x -h 或 x --help 或 man x 查看命令的帮助

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

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