NOTE-文件 目录

NOTE-文件 & 目录

Linux 命令格式

command [-options][parameter]

-options : 可选

parameter : 可选

[ ]内为可选内容

Linux 目录结构

树型结构,没有盘符概念,只有一个根目录/

层级关系使用/表示

Linux 目录操作

command - 显示目录内容 - ls

ls [-a -l -h] [Linux path]

无参数:平铺列出可见文件与文件夹(隐藏文件或文件夹不显示)

-a:展示隐藏文件(linux中以.开头的文件或文件夹默认不展示)

-l:以列表形式展示内容

-h:以易于阅读的形式文件大小,必须与-l搭配使用

[Linux path]显示该目录下的文件

command - 转到目录 - cd

cd [-L|[-P [-e]] [-@]] [DIR]

-L : force symbolic links to be followed: resolve symbolic links in DIR after processing instances of `..'
-P : use the physical directory structure without following symbolic links: resolve symbolic links in DIR before processing instances of `..'
-e : if the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status
-@ : on systems that support it, present a file with extended attributes as a directory containing the file attributes

一般只管 [DIR]

带参数 [DIR] : 进入指定目录

不带参数: 回到用户的HOME目录

command - 打印工作目录 - pwd

pwd [-LP]

-L : print the value of $PWD if it names the current workingdirectory
-P : print the physical directory, without any symbolic links

一般不管参数

查看当前工作目录

特殊路径符

. 表示当前目录, cd ./Desktop == cd Desktop

.. 表示上一级目录, cd ..可转至上一级目录, cd ../..可转至上二级目录
表示HOME目录, cd ~可转至HOME目录

command - 创建目录 - mkdir

mkdir [-p] DIR1 DIR2...DIRN

named from Make Directory

-p : 自动创建不存在的父目录,可用于创建多层目录

DIR : 绝对路径、相对路径均可

注意:在HOME目录之外操作需要提权

Linux文件操作

command - 创建文件 - touch

touch [option] FILE

FILE : 文件路径,绝对相对都可以

[option] : 选项太多不列出

command - 查看文件 - cat & more

cat [option] FILE

FILE : 文件路径

option :

-A, --show-all : equivalent to -vET
-b, --number-nonblank : number nonempty output lines, overrides -n
-e : equivalent to -vE
-E, --show-ends : display $ at end of each line
-n, --number : number all output lines
-s, --squeeze-blank : suppress repeated empty output lines
-t : equivalent to -vT
-T, --show-tabs : display TAB characters as ^I
-u : (ignored)
-v, --show-nonprinting : use ^ and M- notation, except for LFD and TAB

more [option] FILE

option :

-d, --silent : display help instead of ringing bell
-f, --logical : count logical rather than screen lines
-l, --no-pause : suppress pause after form feed
-c, --print-over : do not scroll, display text and clean line ends
-p, --clean-print : do not scroll, clean screen and display text
-s, --squeeze : squeeze multiple blank lines into one
-u, --plain : suppress underlining and bold
-n, --lines <number> : the number of lines per screenful
-<number> : same as --lines
+<number> : display file beginning from line number
+/<pattern> : display file beginning from pattern match

区别:cat一次性显示文件全部内容,more可以分页查看

more打开后操作:往后翻页:空格 退出查看 : q

command - 复制文件、文件夹 - cp

cp [option] DIR1 DIR2

named from copy

[option] : 太多,只列举 -r,表示递归地复制文件夹,在复制文件夹时必须使用

DIR1 : 被复制的文件或文件夹

DIR2 : 复制到的地方

command - 移动文件、文件夹 - mv

mv [option] DIR1 DIR2

[option] :

--backup[=CONTROL] : make a backup of each existing destination file

-b : like --backup but does not accept an argument
-f, --force : do not prompt before overwriting
-i, --interactive : prompt before overwrite
-n, --no-clobber : do not overwrite an existing file If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes : remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX : override the usual backup suffix
-t, --target-directory=DIRECTORY : move all SOURCE arguments into DIRECTORY
-T, --no-target-directory : treat DEST as a normal file
-u, --update : move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose : explain what is being done
-Z, --context : set SELinux security context of destination file to default type

DIR1 : 被移动的文件、文件夹

DIR2 : 移动到的地方

若将文件移动到指定文件名的另一文件中且另一文件未被创建,则修改原文件名为指定的文件名再移动
img

command - 删除文件、文件夹 - rm

rm [option] DIR1 DIR2...DIRn

[option] :

-f, --force : ignore nonexistent files and arguments, never prompt(不弹出提示信息,强行删除)
-i : prompt before every removal
-I : prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
--interactive[=WHEN] : prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
--one-file-system : when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument
--no-preserve-root : do not treat '/' specially
--preserve-root[=all] : do not remove '/' (default); with 'all', reject any command line argument on a separate device from its parent
-r, -R, --recursive : remove directories and their contents recursively(递归地删除,用于删除文件夹)
-d, --dir : remove empty directories
-v, --verbose : explain what is being done

删除某些文件或文件夹可能需要提权

DIR1 DIR2 DIRn : 表示要删除的一个或多个文件或文件夹路径,用空格隔开

支持通配符

通配符

test* : 任何以test开头的内容
*test : 任何以test结尾的内容
*test* : 任何包含test的内容

command - 查看文件尾部内容 - tail

tail [option] DIR
[option] : (主要)
-f : output appended data as the file grows
持续追踪文件变化:文件更新时输出最后一行新增内容或新增行的内容;当文件内容缩短时输出整个文件;当非最后一行的内容有改变或新增时(该行字符量不变或增加)不更新
-nNUM : output the last NUM lines, instead of the last 10
改变默认输出的最后NUM行数;-f生效时,-n只对第一次输出有效。默认输出最后10行

Linux查找

command - 查找命令的程序文件路径 - which

which [-a] filename1 filename2...filenameN

-a : print all matching pathnames of each argument输出每一个参数所匹配的全部路径名

command - 按文件名查找文件 - find

find 搜索起始路径 -name "被查找文件名" (按文件名)
find 搜索起始路径 -size +|-n[KMG] (按文件大小)

+ 表示大于 ; - 表示小于
n 表示大小数字
KMG 表示大小单位,k==kb M==MB G==GB

搜索起始路径表示从该路径开始搜索本目录及所有子目录中匹配项

支持通配符

command - 查找文件中的指定内容 - grep

grep [-n] keyword file1...fileN (简化版)

-n : 输出匹配内容的行号

keyword建议使用" " 包围起来

file参数可以作为内容输入端口

img

command - 数量统计 - wc

wc [option] file1 ... fileN

[option] :
-c, --bytes : print the byte counts 统计字节数量
-m, --chars : print the character counts 统计字符数量
-l, --lines : print the newline counts 统计行数
--files0-from=F : read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input
-L, --max-line-length : print the maximum display width
-w, --words : print the word counts 统计单词数量

file可作为内容输入端口

[option]为空则依次输出行数、单词数、字节数、文件名

posted @ 2023-01-16 14:18  CINKUK  阅读(28)  评论(0)    收藏  举报