歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux常用命令總結——文件管理

Linux常用命令總結——文件管理

日期:2017/3/1 15:43:30   编辑:關於Linux
Linux常用命令總結——文件管理 Linux中的目錄 路徑:也就是linux中的目錄(文件夾)有絕對路徑和相對路徑 根目錄:/ 用戶主目錄(home directory):位於/home目錄下,用戶登錄時 工作目錄(working directory):當前目錄 當前目錄查看命令:pwd (print working directory) 當前目錄:./ 當前目錄的上一級目錄:../或.. 返回到上一級目錄:cd .. 進入當前目錄下的dirfile目錄:cd dirfile cd ~ :進入用戶主目錄(賬號所在目錄) 或者直接cd回車 cd - :(回到先前的目錄) 創建、刪除查看和顯示目錄 1.創建目錄 格式:mkdir [選項] 目錄 功能:創建目錄 常用選項說明: -m 創建目錄的同時設置訪問權限 -p 一次性創建多級目錄 【例】:在rootfile文件夾下創建test文件夾,並在test文件夾下創建file文件夾。 [cpp] [root@localhost rootfile]# mkdir -p test/file [root@localhost rootfile]# ls test [root@localhost rootfile]# cd test [root@localhost test]# ls file [root@localhost test]# 【例】:在rootfile文件夾下創建test2文件夾,並設置test2的權限為766 [cpp] [root@localhost rootfile]# mkdir -m 766 test2 [root@localhost rootfile]# ls test test2 [root@localhost rootfile]# ls -l total 16 drwxr-xr-x 3 root root 4096 Jul 21 21:27 test drwxrw-rw- 2 root root 4096 Jul 21 21:30 test2 注釋:rwxrw-rw-分別對應三種不同用戶的權限,分別有三們二進制表示,766對應111 110 110 2.刪除目錄 格式:rmdir [選項] 目錄 功能:刪除目錄 常用選項說明: -p 遞歸刪除目錄,當子目錄刪除後其父目錄為空時,也一同刪除 【例】:刪除test下的file目錄(文件夾),同時test也一並刪除 [cpp] [root@localhost rootfile]# ls test test2 [root@localhost rootfile]# rmdir -p test/file [root@localhost rootfile]# ls test2 3.查看當前目錄 格式:pwd 功能:pwd (print working directory),查看當前目錄. 常用選項說明: 【例】:查看當前目錄 [cpp] [root@localhost rootfile]# pwd /home/rootfile 5.顯示目錄內容 格式:ls [選項] [文件目錄] 功能:顯示指定目錄中的文件和了目錄信息,當不指定目錄時,顯示當前目錄下的文件和子目錄信息 常用選項說明: -a 顯示所有文件和子目錄,包括隱藏文件和主目錄 -l 顯示文件和子目錄的詳細信息,包括文件類型、權限、所有者和所屬群組、文件大小、最後修改時間、文件名 -d 如果參數是目錄,則只顯示目錄信息,而不顯示其中所包含的文件信息 -t 按時間順序顯示 -R 不僅顯示指定目錄下的文件和子目錄信息,而且還遞歸地顯示子目錄下的文件和子目錄信息 創建和查看文件 創建文件 格式:touch filename 功能:創建文件 常用選項說明: 【例】:在rootfile下創建文件file.txt和test2/file2.txt [cpp] [root@localhost rootfile]# touch file.txt [root@localhost rootfile]# touch test2/file2.txt [root@localhost rootfile]# ls file.txt test2 [root@localhost rootfile]# cd tes* [root@localhost test2]# ls file2.txt cat命令 格式:cat [選項] filename 功能:依次讀取filename中的內容 常用選項說明: 【例】:讀取rootfile下Test.java和file中的文件內容 [cpp] [root@localhost rootfile]# ls file.txt test2 Test.class Test.java [root@localhost rootfile]# vi test2 [root@localhost rootfile]# vi file* [root@localhost rootfile]# cat Test.java public class Test { public static void main(String args[]) { System.out.println("Hello Linux!"); } } [root@localhost rootfile]# cat Test.java file.txt public class Test { public static void main(String args[]) { System.out.println("Hello Linux!"); } } this is a file test. 【例】:把Test.java和file.txt文件合並到combine.txt文件中 [cpp] [root@localhost rootfile]# cat Test.java file.txt > combine.txt [root@localhost rootfile]# cat comb* public class Test { public static void main(String args[]) { System.out.println("Hello Linux!"); } } this is a file test. more命令 格式:more [選項] filename 功能:依次讀取filename中的內容,該命令與cat的不同是可以逐屏往下翻頁顯示,按q退出。 常用選項說明: -p 顯示下一屏之前先清屏 -s 文件中連續的空白行壓縮成一個空白行顯示 【例】:顯示file.txt的內容 [cpp] [root@localhost rootfile]# more file.txt this is a file test. 【例】:顯示Test.java和file.txt的內容 [cpp] [root@localhost rootfile]# more Test.java file.txt :::::::::::::: Test.java :::::::::::::: public class Test { public static void main(String args[]) { System.out.println("Hello Linux!"); } } :::::::::::::: file.txt :::::::::::::: this is a file test. less命令 格式:less [選項] filename 功能:依次讀取filename中的內容,該命令與more的不同是不僅可以向下翻頁,還可以向上翻頁,使用上下鍵、Enter、空格、pageDown、pageUp可以實現前後翻頁,按q退出。 常用選項說明: 【例】:顯示Test.java的內容 [cpp] [root@localhost rootfile]# less Test.java public class Test { public static void main(String args[]) { System.out.println("Hello Linux!"); } } head命令 格式:head [選項] filename 功能:顯示文件的頭幾行 常用選項說明: -n 顯示文件的前n行,如果沒有n值,默認為10行 【例】:顯示Test.java的前3行 [cpp] [root@localhost rootfile]# head -3 Test.java public class Test { public static void main(String args[]) { System.out.println("Hello Linux!"); tail命令 格式:tail [選項] filename 功能:顯示文件的末尾幾行 常用選項說明: +n 從第n行開始顯示 -n 顯示文件的最後n行,如果沒有n值,默認為最後10行 【例】:顯示Test.java的最後3行 [cpp] [root@localhost rootfile]# tail -3 Test.java System.out.println("Hello Linux!"); } } 文件查找 格式:find [選項] filename 功能:從指定的目錄開始,遞歸地搜索其子目錄,查找滿足條件的文件並對之采取相關的操作 常用選項說明: -name ‘字串’ 要查找的文件名,可以用通配符*、?、[] -group ‘字串’ 文件所屬的用戶組名 -user 文件所屬的用戶名 find命令提供的查詢條件可以是一個用邏輯符and、or、not組成的復合條件 -a 邏輯與 -o 邏輯或 -! 邏輯非 【例】:查找當前目錄下文件名含有Test的文件 [root@localhost rootfile]# find -name 'Test*' ./Test.class ./Test.java 【例】:在根目錄下查找文件名為’temp’或是匹配’install*’的所有文件 [java] [root@localhost rootfile]# find / -name 'temp' -o -name 'instal*' /etc/rhgb/temp /etc/yum/pluginconf.d/installonlyn.conf /etc/vmware-tools/installer.sh /software/tomcat5/webapps/docs/appdev/installation.html /software/tomcat5/temp /sbin/install-info /sbin/installkernel /usr/share/aclocal-1.9/install-sh.m4 /usr/share/icons/Bluecurve/96x96/mimetypes/install.png /usr/share/icons/Bluecurve/24x24/mimetypes/install.png /usr/share/icons/Bluecurve/16x16/mimetypes/install.png /usr/share/icons/Bluecurve/48x48/mimetypes/install.png /usr/share/aclocal-1.7/install-sh.m4 /usr/share/doc/cyrus-sasl-lib-2.1.22/install.html /usr/share/doc/sgml-common-0.6.3/html/install-catalog.html /usr/share/doc/m2crypto-0.16/demo/Zope27/install_dir /usr/share/doc/m2crypto-0.16/demo/ZopeX3/install_dir /usr/share/doc/libstdc++-devel-4.1.1/html/install.html …… 【例】:在rootfile下查找不含Test*的文件 [java] [root@localhost rootfile]# find ! -name 'Test*' . ./.Test2.swp ./1q ./.Test.java.swp ./test2 ./test2/file2.txt ./combine.txt ./file.txt 文字統計命令 格式:wc [選項] filename 功能:統計文件的字節數、字數、行數 常用選項說明: -c 統計字節數 -l 統計行數 -w 統計字數 【例】:統計Test.java的字節數、行數、字數 [java] [root@localhost rootfile]# wc Test.java 5 14 105 Test.java [root@localhost rootfile]# wc -wcl Test.java 5 14 105 Test.java 復制、移動和刪除文件或文件夾 cp 命令 格式:cp [選項] 源目錄或文件 目標目錄或文件 功能:將給出的文件或目錄復制到另一個文件或目錄中 常用選項說明: -b 若存在同名文件,則覆蓋前備份原來的文件 -f 強制覆蓋同名文件 -r或R 按遞歸方式,保留原目錄結構復制文件 【例】:復制file.txt文件到file2,若file2已經存在,則備份file2. [java] [root@localhost rootfile]# ls 1q combine.txt file.txt test2 Test.class Test.java [root@localhost rootfile]# cp -b file.txt file2 [root@localhost rootfile]# ls 1q combine.txt file2 file.txt test2 Test.class Test.java [root@localhost rootfile]# cp -b file.txt file2 cp: overwrite `file2'? n [root@localhost rootfile]# ls 1q combine.txt file2 file.txt test2 Test.class Test.java [root@localhost rootfile]# cp -b file.txt file2 cp: overwrite `file2'? y [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt test2 Test.class Test.java 【例】:把test2文件復制到test3文件夾 [java] [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt test2 Test.class Test.java [root@localhost rootfile]# [root@localhost rootfile]# cp -r test2 test3 [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt test2 test3 Test.class Test.java mv命令 格式:mv [選項] 源目錄或文件 目標目錄或文件 功能:移動或重命名文件或目錄 常用選項說明: -b 若存在同名文件,則覆蓋前備份原來的文件 -f 強制覆蓋同名文件 【例】:將/home/rootfile下的Test.java移動到/home/rootfile /test2下 [java] [root@localhost rootfile]# mv Test.java test2/Test [root@localhost rootfile]# ls -R .: 1q combine.txt file2 file2~ file.txt test2 test3 Test.class ./test2: file2.txt Test ./test3: file2.txt rm 命令 格式:rm [選項] 文件夾或目錄 功能:刪除文件夾或目錄 常用選項說明: -f 強制刪除文件,不出現確認提示 -r或R 按遞歸方式刪除目錄,默認只刪除文件 【例】:刪除當前目錄下的test3文件夾 [java] [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt test2 test3 Test.class [root@localhost rootfile]# ls test3 file2.txt [root@localhost rootfile]# rm -r test3 rm: descend into directory `test3'? y rm: remove regular empty file `test3/file2.txt'? y rm: remove directory `test3'? y [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt test2 Test.class 【例】: 強制刪除當前目錄下的test2文件夾 [java] [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt test2 Test.class [root@localhost rootfile]# rm -rf test2 [root@localhost rootfile]# ls 1q combine.txt file2 file2~ file.txt Test.class
Copyright © Linux教程網 All Rights Reserved