目录
文件和文件夹操作
一、文件操作
二、文件夹操作
文件和文件夹操作
一、文件操作
功能项 | 命令实例 | 作用 |
---|---|---|
文件创建 | vi /opt/learn/helo.txt | 在目录/opt/learn下创建文件hello.txt并进入vi编辑界面 |
touch /opt/learn/test.txt | 在目录/opt/learn下创建空白文件test | |
cat > /opt/learn/catfile | 创建文件catfile并在屏幕上输入内容,最后按Ctrl+D退出,但是这种方式每一次新的数据进去会覆盖掉原来的数据,要想给原来的文件后追加新的数据,应该用 >> | |
文件查看 | vi /etc/password | 在vi编辑器中输出文本内容 |
cat /etc/password | 在屏慕上输出文本内容 | |
more /etc/password | 分屏输出文本内容 | |
less /etc/password | 分屏输出文本内容并按需加载文件(适用于大文件的查看) | |
head -n 10 /etc/password | 只输出文件的头10行 | |
tail -n 20 /etc/password | 只输出文件末尾的20行 | |
string /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 | 强制删除文件,不会有提示信息 | |
rm -r /opt/learn | 删除目录 | |
du -sk hello.txt | 查看文件hello.txt的大小(以K为单位) | |
链接 | ln -s hello.txt shello | 为hello.txt文件创建一个名为shello的软链接(类似于快捷方式) |
ln -d hello.txt dhello | 为hello.txt文件创建一个名为dhello的硬链接 硬链接表示所有文件中更改任意一个其他文件的所有属性会跟着变化,如大小,更新时间,权限等 | |
二、文件夹操作
功能项 | 命令或格式 | 作用 |
---|---|---|
ls/tree | ls[option] [file/directory] | 显示指定目录下的所有文件或文件夹(同Windows->dir命令) |
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] [directory1] [directory2] ... | 创建目录 |
mkdir -p /目录名/目录名/目录名/... | 根据层次创建目录 | |
rmdir | rmdir dir1 | 删除一个空目录 |
其他操作 | cp -r /opt/learn /opt/learn2 | 拷贝文件夹 |
mv /opt/learn2 /opt/learn3 | 重命名文件夹 | |
rm -rf /opt/learn | 强制删除文件夹 | |
rm -rf / | 别搞诶 | |
rm -rf /* | 别搞哦 | |
xxx -h 或 xxx --help 或 man xxx | 查看命令的帮助,实在读不懂,百度 | |