1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。
目錄管理命令:mkdir, rmdir
注意:路徑名的基名方爲命令作用的對象。基名前面的路徑必須要真實存在,
mkdir命令:
NAME
mkdir - make directories
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask
-p, --parents
no error if existing, make parent directories as needed
-v, --verbose
print a message for each created directory
示例:
~]# mkdir dir1/dir2/dir3 mkdir: cannot create directory ‘dir1/dir2/dir3’: No such file or directory ~]# mkdir -p dir1/dir2/dir3 ~]# tree dir1 dir1 └── dir2 └── dir3 2 directories, 0 files
rmdir命令:
NAME
rmdir - remove empty directories
SYNOPSIS
rmdir [OPTION]... DIRECTORY...
DESCRIPTION
Remove the DIRECTORY(ies), if they are empty.
--ignore-fail-on-non-empty
ignore each failure that is solely because a directory is non-empty
-p, --parents
remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'
-v, --verbose
output a diagnostic for every directory processed
示例:
~]# rmdir dir1 rmdir: failed to remove ‘dir1’: Directory not empty #只能刪除空目錄 ~]# rmdir dir1/dir2/ rmdir: failed to remove ‘dir1/dir2/’: Directory not empty ~]# rmdir dir1/dir2/dir3/ ~]# rmdir dir1/dir2/ ~]# rmdir dir1/
文件查看類命令:cat, tac, head, tail, more, less
分屏查看命令more和less
more命令
more FILE
特點:翻屏至文件尾部後自動退出
less命令
less FILE
操作方法類似於man命令
head命令
head [OPTION]... [FILE]...
查看文件前n行內容,默認爲前10行
-n # 或 -n# 或 -# 或 -n+# 或-n +#:顯示文件的前number行
-n -# 或 -n-#:顯示文件第一行至文件倒數#+1行的內容
示例:test.txt文件共有21行
~]# head -n2 test.txt 1 2 ~]# head -2 test.txt 1 2
tail命令
tail [OPTION]... [FILE]...
查看文件的後n行,默認爲後10行
-n # 或 -n# 或 -n -# 或 -n-# 或 -#:顯示文件的後#行
-n +# 或 -n+#:顯示文件第#行至文件最後一行的內容
-f:查看文件尾部內容結束後不退出,跟隨顯示新增的行
動態顯示文件尾部的內容,直到按ctrl+c退出
示例:test.txt文件共有21行
~]# tail -n2 test.txt 20 21 [root@learn ~]# tail -2 test.txt 20 21 ~]# tail -n+18 test.txt 18 19 20 21
stat命令
NAME
stat - display file or file system status
SYNOPSIS
stat [OPTION]... FILE...
文件:文件存儲有兩類數據
元數據:metadata 用來描述屬性的數據(文件名、大小、時間戳、索引號等)
數據:data 文件的內容
示例:
~]# stat /etc/passwd File: ‘/etc/passwd’ Size: 798 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 17116887 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:passwd_file_t:s0 Access: 2018-07-27 12:32:25.704000038 +0800 Modify: 2018-07-24 23:05:04.257314967 +0800 Change: 2018-07-24 23:05:04.259314967 +0800 Birth: -
touch命令
NAME
touch - change file timestamps
SYNOPSIS
touch [OPTION]... FILE...
DESCRIPTION
Update the access and modification times of each FILE to the current time.
-a change only the access time
-m change only the modification time
-c, --no-create
do not create any files
-d, --date=STRING
parse STRING and use it instead of current time
用STRING替換當前時間
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
-h, --no-dereference
affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
-r, --reference=FILE
use this file's times instead of current time
示例:
~]# touch -d "2018-7-27 11:30:58" hello.txt ~]# ll hello.txt -rw-r--r--. 1 root root 0 Jul 27 11:30 hello.txt
文件管理工具:cp, mv, rm
cp命令:copy
源文件 目標文件
文件複製原理:假如源文件是一個常規文件,裏面有很多數據(我們把它稱爲流式數據),複製文件就是,首先在目標位置(某一個路徑)下先創建一個空文件,隨後從原來的文件中抽取數據流(流式數據:可流式化的數據),一行一行(或者一個字節一個字節或者一位一位)的抽出來,最終給它填充到這個目標文件中去,進而目標文件越來越大,越來越大,變得和原來文件內容一樣了,所以複製文件指的是複製文件的數據,而不是複製文件的元數據的,只不過,在有些特殊場景下我們可以保留他複製數據時的某些元數據,但複製通常只是針對於數據而言的。
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
單源複製:cp [OPTION]... [-T] SOURCE DEST
多源複製:cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE
單源複製:cp [OPTION]... [-T] SOURCE DEST
如果DEST不存在:
沒有給出DEST參數,則會報錯(cp: missing destination file operand after ‘SOURCE’)。
如果給出DEST是不存在的路徑(錯誤的路徑),則會報錯(cp: cannot create regular file ‘DEST’: No such file or directory)。
如果DEST存在:
如果DEST是非目錄文件,則覆蓋DEST文件。
如果DEST是目錄文件,則在DEST目錄下創建一個與SOURCE文件同名的文件,並複製其數據(元數據-如果有必要)至該文件。
多源複製:cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE
多源複製,如果沒有明確給出DIRECTORY,會把SOURCE參數最後的一個值作爲DIRECTORY的值。
如果DIRECTORY是一個非目錄文件或者是一個不存在的路徑(錯誤的路徑),會報錯(cp: target ‘DIRECTORY’ is not a directory)。
若果DIRECTORY是一個目錄文件,則會分別複製SOURCE參數中的每一個文件至DIRECTORY目錄中,並保持原名稱,如果DIRECTORY裏面存在與SOURCE參數中的值同名的文件,則會被覆蓋(-i選項會提示)。
cp命令如果遇到符號鏈接,默認是複製的符號鏈接指向的文件的數據,而非符號鏈接本身。
cp命令的常用選項:
-i, --interactive
prompt before overwrite (overrides a previous -n option)
交互式複製,即覆蓋前提請用戶確認。(若果前面有-n參數,則忽略-n參數)
-n, --no-clobber
do not overwrite an existing file (overrides a previous -i option)
不覆蓋已經存在的目標文件(如果前面有-i參數,則忽略-i參數)
-f, --force
if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
強行覆蓋目標文件(如果使用-n選項,則-f不起作用)
-R, -r, --recursive
copy directories recursively
遞歸複製目錄及目錄的內容至目標目錄。
-P, --no-dereference
never follow symbolic links in SOURCE
不跟蹤SOURCE中的符號鏈接
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
保留指定的屬性
參數值 : 保留原來的某種屬性
mode : 權限
ownership : 屬主
timestamps : 時間戳
context : 安全標籤
links : 鏈接屬性
xattr : 擴展屬性
all : 以上所有
-d same as --no-dereference --preserve=links
複製鏈接文件本身,而非其指向的源文件(拷貝時保留其鏈接屬性)
-a, --archive
same as -dR --preserve=all
-L, --dereference
always follow symbolic links in SOURCE
跟蹤符號鏈接指向的文件(拷貝符號鏈接指向的文件內容)
-p same as --preserve=mode,ownership,timestamps
保留源文件權限、屬主、時間戳屬性
--backup[=CONTROL]
make a backup of each existing destination file
爲每一個存在的目標文件先創建備份,再拷貝。
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VER‐
SION_CONTROL environment variable. Here are the values:
none, off
never make backups (even if --backup is given)
numbered, t
make numbered backups
existing, nil
numbered if numbered backups exist, simple otherwise
simple, never
always make simple backups
-S, --suffix=SUFFIX
override the usual backup suffix
覆蓋通常的備份後綴
-l, --link
hard link files instead of copying
用硬鏈接代替cp(對源文件建立硬鏈接,而非複製文件)
-s, --symbolic-link
make symbolic links instead of copying
用符號鏈接替代cp(對源文件建立符號鏈接,而非複製文件)
(只能在同一個目錄創建相對符號鏈接)
-u, --update
copy 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
示例:
~]# cd /tmp tmp]# cp /var/log ./log cp: omitting directory ‘/var/log’ tmp]# cp -r /var/log ./log
mv命令
mv命令原理:如果是一個文件,首先在目標位置建立一個文件,文件名就是DEST所指定的路徑或文件名,然後再把源文件複製過去填充進去,填充完畢,刪除源文件。
如果源有多個,目標只能是目錄
NAME
mv - move (rename) files
SYNOPSIS
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
[OPTION]
-i, --interactive
prompt before overwrite
-f, --force
do not prompt before overwriting
示例:
~]# ll test.txt -rw-r--r--. 1 root root 54 Jul 26 20:59 test.txt ~]# mv test.txt test2.txt ~]# ll test.txt ls: cannot access test.txt: No such file or directory ~]# ll test2.txt -rw-r--r--. 1 root root 54 Jul 26 20:59 test2.txt
rm命令
NAME
rm - remove files or directories
SYNOPSIS
rm [OPTION]... FILE...
[OPTION]
-i prompt before every removal
-f, --force
ignore nonexistent files and arguments, never prompt
-I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
對於多個文件僅提醒一次
-r, -R, --recursive
remove directories and their contents recursively
遞歸地刪除目錄及其內容
-d, --dir
remove empty directories
--preserve-root
do not remove '/' (default)
--no-preserve-root
do not treat '/' specially
-v, --verbose
explain what is being done
示例:
~]# rm -rI /tmp/log #-I僅提醒一次 rm: remove 1 argument recursively? y ~]# ll /tmp/log ls: cannot access /tmp/log: No such file or directory
2、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。
命令執行的狀態結果
bash通過狀態返回值來輸出此結果
成功:0
失敗:1-255
命令執行完成之後,其狀態返回值保存於bash的特殊變量$?中,其只保存最近一次命令的狀態返回值結果。
命令正常執行後,有的命令有返回值,其不同於狀態返回值,根據命令及其功能不同,結果各不相同。
示例:
~]# pwd /root ~]# echo $? 0 ~]# pwdl -bash: pwdl: command not found ~]# echo $? 127
引用命令的執行結果
$(COMMAND)
或
`COMMAND`
例如:
~]# mkdir $(date +%H-%m-%S)
~]# mkdir `date +%H-%m-%S`
示例:
~]# mkdir $(date +%H-%m-%S) ~]# ls 22-07-47 ~]# mkdir `date +%H-%m-%S` ~]# [root@learn xxx]# ls 22-07-02 22-07-47
命令行展開
~ :自動展開爲用戶的家目錄,或指定用戶的家目錄。
~, ~root
示例:
xxx]# pwd /root/testdir/xxx [root@learn xxx]# cd ~ [root@learn ~]# pwd /root ~]# cd - /root/testdir/xxx xxx]# cd ~root ~]# pwd /root
{}:大括號擴展(引號內的大括號將失去擴展功能),大括號包圍的,用逗號隔開(只有一個元素也需要逗號,會匹配一個元素和一個空元素)的參數會擴展爲獨立的多個參數。
花括號用來匹配一組用逗號分隔的字符串中的任一個。左花括號之前的所有字符稱爲前文(preamble),右花括號之後的所有字符稱爲後文(preamble)。前文和後文都是可選的。花括號中不能包含不加引號的空白符。
可承載一個以逗號分隔的路徑列表,並能夠將其展開爲多個路徑
/tmp/{a,b} 相當於 /tmp/a /tmp/b
示例:
~]# echo ab{1,2,3}c ab1c ab2c ab3c ~]# echo "ab{1,2,3}c" ab{1,2,3}c ~]# echo \"ab{1,2,3}c\" "ab1c" "ab2c" "ab3c" ~]# echo {a,b}{1} a{1} b{1} ~]# echo {a,b}{1,} a1 a b1 b
3、請使用命令行展開功能來完成以下練習:
(1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d
示例:
~]# cd /tmp tmp]# touch {a,b}_{c,d} tmp]# ls a_c a_d b_c b_d
(2)、創建/tmp/mylinux目錄下的:
mylinux/ ├── bin ├── boot │ └── grub ├── dev ├── etc │ ├── rc.d │ │ └── init.d │ └── sysconfig │ └── network-scripts ├── lib │ └── modules ├── lib64 ├── proc ├── sbin ├── sys ├── tmp ├── usr │ └── local │ ├── bin │ └── sbin └── var ├── lock ├── log └── run
~]# mkdir -p /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}} ~]# tree /tmp/mylinux/ /tmp/mylinux/ ├── bin ├── boot │ └── grub ├── dev ├── etc │ ├── rc.d │ │ └── init.d │ └── sysconfig │ └── network-scripts ├── lib │ └── modules ├── lib64 ├── proc ├── sbin ├── sys ├── tmp ├── usr │ └── local │ ├── bin │ └── sbin └── var ├── lock ├── log └── run 24 directories, 0 files