Linux 常用目录及文件操作(压缩、解压缩)等命令

本文主要介绍 Linux 操作系统里的目录及文件操作命令

浏览目录:

1. ls
英文原意:list directory contents(列出目录内容)
描述:List  information  about  the FILEs (the current directory by default). 列出有关文件的信息(默认情况下为当前目录)
语法:ls [OPTION]... [FILE]...
常用参数:
-a, --all:do not ignore entries starting with . (显示所有文件,包括隐藏文件)
-l:use a long listing format(详细地显示文件信息)
-d, --directory:list directories themselves, not their contents(显示目录属性信息)

2. pwd
英文原意:print name of current/working directory(显示当前的工作目录)
描述:Print the full filename of the current working directory.(打印当前工作目录的完整文件名)
语法:pwd

目录操作:

1. cd
英文原意:change directory(改变目录)
描述:Change  the  current directory to dir.(将当前目录更改为dir)
语法:cd [dir]
其他用法:cd ..(回到上一级目录)

2. mkdir
英文原意:make directories(创建目录)
描述:Create the DIRECTORY(ies), if they do not already exist.(如果该目录不存在,则创建)
语法:mkdir DIRECTORY...

3. rmdir
英文原意:remove empty directories(删除空目录)
描述:Remove the DIRECTORY(ies), if they are empty.(当目录为空,则删除)
语法:rmdir DIRECTORY...

浏览文件命令:

1. cat
英文原意:concatenate files and print on the standard output(连接文件并在标准输出上打印)
描述:Concatenate FILE(s), or standard input, to standard output.(将文件或标准输入连接到标准输出)
语法:cat [OPTION]... [FILE]...
常用参数:
-E, --show-ends:display $ at end of each line(在每行的末尾显示 $ )
-n, --number:number all output lines(为每行的显示内容编号)

2. more
英文原意:file perusal filter for crt viewing(用于crt查看的文件读取过滤器)
描述:
more is a filter for paging through text one screenful at a time.  This
version is especially primitive.  Users  should  realize  that  less(1)
provides more(1) emulation plus extensive enhancements.
就是分屏显示文本,用于查看文件内容过大,一屏幕显示不完全
语法:more file [...]
其他操作:空格显示下一页,enter 显示下一行,q(Q) 退出

3. less
英文原意:opposite of more(与 more 操作命令相反)
描述:原文意思是说:less 是一个类似于 more 的分屏查看程序,但是可以通过上下键进行文件内容的前后移动,
它读入文件内容不是直接全部读入,所以它比其他文本阅读器启动速度更快
语法:less file [...]
其他操作:空格显示下一页,enter 显示下一行,上下键前后内容移动,q(Q) 退出

4. head
英文原意:output the first part of files(输出文件的前部分)
描述:用于显示文件的前几行内容(默认显示前 10 行)
语法:head [OPTION]... [FILE]...
常用参数:
-n, --lines:显示前 n 行内容

5. tail
英文原意:output the last part of files(输出文件的后部分)
描述:用于现实文件的后几行内容(默认显示后 10 行)
语法:tail [OPTION]... [FILE]...
常用参数:
-n, --lines:显示后 n 行内容
-F, --follow:用于跟踪显示不断赠长的文件结尾内容(通常用于显示日志文件,非常有用)

文件操作命令:

1. cp
英文原意:copy files and directories(复制文件和目录)
描述:Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.(将源复制到DEST,或将多个源复制到目录。将源复制到DEST,或将多个源复制到目录。)
语法:cp [OPTION]... SOURCE... DIRECTORY
常用参数:
-p, --preserve:连同文件的属性一起复制,而非使用默认方式,常用于备份
-i, --interactive:prompt before overwrite(若目标文件已经存在时,在覆盖前提示)
-r, --recursive:copy directories recursively(递归复制目录,这个很常用)
-u, --update:copy only when the SOURCE file is  newer  than  the  destination file or when the destination file is missing
(当目标文件与源文件有差异时或者目标文件丢失时才会复制)
例子:cp -r test1(目录) test2(目录)

2. rm
英文原意:remove files or directories(删除文件或目录)
描述:
This  manual  page  documents  the  GNU version of rm.  rm removes each
specified file.  By default, it does not remove directories.
删除指定文件,默认不删除目录
语法:rm [OPTION]... FILE...
常用参数:
-f, --force:ignore nonexistent files and arguments, never prompt(忽略不存在的文件和参数,不要提示)
-i, --interactive:prompt before every removal(互动模式,删除带提示)
-r, --recursive:remove directories and their contents recursively(递归删除目录及其内容)
例子:rm -rf /*(通常程序员都会运行这个命令,一般人我不告诉他)

3. find
英文原意:search for files in a directory hierarchy(在目录层次结构中搜索文件)
语法:find [path...] [OPTION]
常用参数:
-name filename:找出文件名为 filename 的文件(find / -name a.txt)
-size [+-]SIZE:找出比 SIZE 还要大 (+)  或小 (-) 的文件
-type TYPE:查找文件的类型为 TYPE 的文件
TYPE:(f) 一般文件、(b、c) 设备文件、(d) 目录、(l) 链接文件、(s) socket、(p) FIFO 管道文件
-perm mode:查找文件权限刚好等于 mode 的文件,mode 用数字表示,例如: 0755

4. grep
英文原意:print lines matching a pattern(打印与模式匹配的行)
描述:在文件中搜索匹配的字符并进行输出
语法:grep [OPTIONS] PATTERN [FILE...]
常用参数:
-a, --text:将 binary 文件以 text 文本的方式查找数据
-c, --count:计算找到 “查找字符串” 的次数
 -i, --ignore-case:忽略大小写的区别
-v, --invert-match:反向匹配,显示不包含 “查找字符串” 内容的那一行

5. tar
英文原意:tape archive(磁带归档)
描述:能够将用户所指定的文件或目录打包成一个文件,也可以通过指定参数开启压缩、解压缩功能
语法:tar [OPTION...] [FILE]...
常用参数:
-c, --create:create a new archive(新建打包文件)
-t, --list:list the contents of an archive(查看打包文件内容含有哪些文件名)
-x, --extract, --get:extract files from an archive(解打包或解压缩的功能)
-j, --bzip2:filter the archive through bzip2(通过 bzip2 的支持进行压缩/解压缩)
-z, --gzip:filter the archive through gzip(通过 gzip 的支持进行压缩/解压缩)
-v, --verbose:verbosely list files processed(在压缩/解压缩过程中,将正在处理的文件名显示出来)
-f, --file=ARCHIVE:use archive file or device ARCHIVE(指定要处理的文件)
 -C, --directory=DIR:change to directory DIR(指定压缩/解压缩的目录)
重点来了:
tar -cvf test.tar *(将当前目录的所有文件打包成 test.tar)
tar -czvf test.tar.gz *(将当前目录的所有文件打包成 test.tar.gz)
tar -tf test.tar(查看 test.tar 文件中包括了哪些文件)
tar -xvf test.tar(将 test.tar 解打包)
tar -xzvf test.tar.gz(将 test.tar.gz 解压缩并解打包)
tar -czvf test.tar.gz a.txt b.txt -C /.(将 a.txt、b.txt 打包到当前目录下的 test.tar.gz )

授人以鱼不如授人以渔!
你们可以用 man + 命令 去查询各种命令的详解!

 

发布了92 篇原创文章 · 获赞 23 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/assiduous_me/article/details/103926826