Linux(三) - 文件操作相关命令

Copy
Ctl-A 光标移动到行首 Ctl-C 终止命令 Ctl-D 注销登录 Ctl-E 光标移动到行尾 Ctl-U 删除光标到行首的所有字符,在某些设置下,删除全行 Ctl-W 删除当前光标到前边的最近一个空格之间的字符 Ctl-H 删除光标前边的字符 Ctl-R 匹配最相近的一个文件,然后输出 Ctl-Z 将目前任务转为后台运行

Path 相关(cd、ls)#

Copy
pwd #print working dir // 目录查看 ls == ls -h #list files under current directory ls -l #list with block and property ls -A #list including hidden files ls -Alh #list in a friendly view ls /home #list a upper dir ls *.json #list by wildcards ls -lrt #按时间排序,以列表的方式显示目录项 // 目录跳转 cd - #switch to last path cd .. #switch to upper path cd / #switch to root path cd ~/hexo #switch by absolute path cd ./user/xx #switch by relative path

帮助命令(help、man、info、type)#

Copy
help #仅适用于内置命令,效果不佳 man ls #查看命令手册 info ls #GNU的更详细的命令手册 type xxx $ xxx is a shell builtin #说明是内建命令,这些命令都是在 bash 源码中的 builtins 的 .def 中 $ xxx is /usr/bin/xxx #说明是外部命令 $ xxx is an alias for xx #说明该指令为命令别名所设定的名称

文件相关#

文件查看(cat、nl、head、tail、more、less、file)#

Copy
//文件内容 cat file #preview file cat -n fileA #preview file with linenum nl fileA #preview trim file with linenum more/less fileA #分页查看,more会直接结束,less 需要q退出 tail file #只看后几行 tail -n 2 file #只看2行 cut -c -5 file #每行前五个字节(包含第五个) cut -c 5- file #每行前五个之后的字节(包含第五个) cut -c 5 file #每行第五个字节 cut -c 2-5 file #每行第2到5之间的字节(包含第五个) //文件类型 file file #查看文件类型

文件创建、复制、删除、重命名(touch、mkdir、rm、cp、mv)#

Copy
// 创建 touch filename #create empty file mkdir xxx/xx -p #create path(-p: create xxx) //复制 cp -r A B #duplicate A's files to B(B unexist & only one floor) cp -r A B #copy A to B(if B exist) //删除 rm filename #delete filename rm -r dir #delete dir //重命名 mv xxx/oldfile xxx/newfile #rename a file mv oldfile xxx/newfile #rename & move a file mv dir xxx/dir #move dir to xxx/dir which is created

文件压缩和解压#

Copy
// 压缩 zip zip -rqo file.zip ./filename #普通压缩 zip -{1…9} -rqo zip.zip ./filename #不同级别压缩、加密压缩 zip -dv cp.zip file #从cp.zip中删除文件 # -d 从压缩文件内删除指定的文件 # -o 将压缩文件的更改时间设成和文件的最新更改时间一致 # -q 不显示指令执行过程 # -r 递归处理,将指定目录下的所有文件和子目录一并处理 # -v 显示指令执行过程或显示版本信息 //查看 unzip -l file.zip #显示压缩文件内所包含的文件 //解压 unzip unzip file.zip #直接解压 unzip -q file.zip -d ./dir #解压到 dir 中 # -d <目录> 指定文件解压缩后所要存储的目录 # -q 执行时不显示任何信息 # -v 执行时显示详细的信息 //tar //压缩 tar -cf file.tar xxx.xx #把xxx.xx 压缩为 file.tar tar -czvf file.tar.gz xxx.xx #把xxx.xx 打包为 tar.gz 文件 # -c 建立新的备份文件 # -f 指定备份文件 # -v(--verbose) 显示指令执行过程 # -z(--gzip) 通过gzip指令处理备份文件 tar -cphf etc.tar /dir #一般用于系统备份 # -p 保留文件属性和跟随链接(符号链接或软链接) # -h(--dereference) 不建立符号连接,直接复制该连接所指向的原始文件 # 使用 tar 备份文件当你在其他主机还原时希望保留文件的属性和备份链接指向的源文件而不是链接本身 //查看压缩内容 tar -tf file.tar #查看 file.tar tar -tzvf test.tar.gz #查看压缩文件内容 # -C<目录>(--directory) 指定文件解压缩后所要存储的目录 # -f 指定备份文件 //解压 tar -xf file.tar -C ./dir #解压 file.tar tar -xzf file.tar.gz #解压 tar.gz 打包文件 # -x (--extract) 从备份文件中还原文件 # -z(--gzip) 通过gzip指令处理备份文件

文件软链接(ln)#

软链接:

  1. 软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
  2. 软链接可以 跨文件系统 ,硬链接不可以
  3. 软链接可以对一个不存在的文件名进行链接
  4. 软链接可以对目录进行链接
Copy
ln -s log.log link #为log.log 建立名为 link 的软链接 # -s 软链接(符号链接)

文件查找(whereis、which、find)#

Copy
locate \*.jpg #locate 会维护一个数据库,查找时不会真的去找 which who #查找安装的命令、程序可以用 //find 命令 find [path] [option] [action] find . -name \*.jpg #查找 jpg 文件 find . -type f #查找一般文件 #d: 目录 #c: 字型装置文件 #b: 区块装置文件 #p: 具名贮列 #f: 一般文件 #l: 符号连结 #s: socket find . -ctime n #在过去n天内被变更过的文件 find . -atime n #在过去n天内被访问过的文件 find . -mtime n #在过去n天内容最后被修改的时间的文件 # -mtime n:n 为数字,表示为在 n 天之前的一天之内修改过的文件 # -mtime +n:列出在 n 天之前(不包含 n 天本身)被修改过的文件 # -mtime -n:列出在 n 天之内(包含 n 天本身)被修改过的文件 find . -newer /home/shiyanlou/Code # -newer file:file为一个已存在的文件,列出比file还要新的文件 find . -type f -perm 644 -exec ls -l {} \; #查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件

File Permission(chown、chmod)#

  • 3 个权限组,每组 3 位
  • 一个目录同时具有读权限和执行权限才可以打开并查看内部文件,而一个目录要有写权限才允许在其中创建其它文件
  • 权限
Copy
chmod 600 file #为 file 设置600权限 //g、o 还有 u 分别表示 group、others和user //+ 和 - 分别表示增加和去掉相应的权限 chmod go-rw iphone6 #表示去掉group、others 的读写权限 chown username file #grant file ownership to user chown :group file #grant file ownership to group chown xx:xx file #grant file ownership to xx & xx chown -R user:group dir #grant dir(-r) ownership to xx & xx

文件合并(join、paste)#

Copy
join file1 file2 #逐行合并,相同的部分仅保留一份 #「1 hello」和「1 world」合并为「1 hello world」 paste f1 f2 [文件...] #逐行合并 # -d 自定义合并后的分割符( '\n' 以回车为每行的分割符) paste -s file #将一个文件中的多行数据合并为一行显示,但不修改原文件
posted @   质子  阅读(221)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言
点击右上角即可分享
微信分享提示
CONTENTS