命令行學習筆記(文件相關命令)

  • ls -- 列出當前目錄下的文件(不包含隱藏文件)
  • ls -l -- 列出當前目錄下文件的詳細信息
  • ls -a -- 列出當前目錄下的文件(包含隱藏文件)

效果如下:

?  ~ ls
anyang         Documents         IdeaProjects  Public     wget-log
configuration  Downloads         Music         Templates
Desktop        examples.desktop  Pictures      Videos
?  ~ ls -l
total 56
drwxr-xr-x 3 anyang anyang 4096 12月  2 19:50 anyang
drwxr-xr-x 3 anyang anyang 4096 11月 29 22:27 configuration
drwxr-xr-x 2 anyang anyang 4096 11月 29 11:56 Desktop
drwxr-xr-x 2 anyang anyang 4096 11月 29 11:56 Documents
drwxr-xr-x 5 anyang anyang 4096 11月 29 20:45 Downloads
-rw-r--r-- 1 anyang anyang 8980 11月 29 03:47 examples.desktop
drwxr-xr-x 2 anyang anyang 4096 11月 29 22:26 IdeaProjects
drwxr-xr-x 2 anyang anyang 4096 11月 29 11:56 Music
drwxr-xr-x 2 anyang anyang 4096 12月  2 21:44 Pictures
drwxr-xr-x 2 anyang anyang 4096 11月 29 11:56 Public
drwxr-xr-x 2 anyang anyang 4096 11月 29 11:56 Templates
drwxr-xr-x 2 anyang anyang 4096 11月 29 11:56 Videos
-rw-r--r-- 1 anyang anyang    0 11月 28 22:03 wget-log
?  ~ ls -a
.                 .fonts.conf          Public
..                .gconf               .ssh
.adobe            .gitconfig           .sudo_as_admin_successful
anyang            .gnome               Templates
.bash_history     .gradle              Videos
.bash_logout      .ICEauthority        .viminfo
.bashrc           IdeaProjects         .WebStorm2016.3
.cache            .IntelliJIdea2016.3  wget-log
.compiz           .java                .Xauthority
.config           .local               .xinputrc
configuration     .macromedia          .xsession-errors
Desktop           .mozilla             .xsession-errors.old
.dmrc             Music                .zcompdump
Documents         .oh-my-zsh           .zcompdump-anyang-5.2
Downloads         Pictures             .zsh_history
examples.desktop  .pki                 .zshrc
.fonts            .profile             .zsh-update
  • cd directory -- 切換目錄
  • cd .. -- 切換到當前目錄的上一級目錄
  • cd - -- 切換到最近一次所在的目錄
  • cd ~cd -- 切換到當前用戶的家目錄

效果如下:

?  ~ cd anyang 
?  anyang ls
file1  file2  learngit
?  anyang cd ..
?  ~ cd -
~/anyang
?  anyang cd ~
?  ~ cd anyang 
?  anyang cd
?  ~ 
  • mkdir directory -- 創建目錄
  • rmdir directory -- 刪除空目錄

效果如下:

?  anyang ls   
file1  file2  learngit
?  anyang mkdir test 
?  anyang ls
file1  file2  learngit  test
?  anyang rmdir test
?  anyang ls
file1  file2  learngit
  • touch file -- 新建文件(輸入多個文件名實現多文件創建)
  • rm file -- 刪除文件(輸入多個文件名實現多文件刪除)

效果如下:

?  anyang touch newfile1 newfile2 newfile3
?  anyang ls
file1  file2  learngit  newfile1  newfile2  newfile3
?  anyang rm file1 file2
?  anyang ls
learngit  newfile1  newfile2  newfile3
  • rm -r directory -- 遞歸刪除非空目錄(刪除目錄和目錄下的內容)
  • rm -fr directory -- 強制遞歸刪除非空目錄(刪除目錄和目錄下的內容)

效果如下:

?  anyang mkdir test
?  anyang ls
learngit  newfile1  newfile2  newfile3  test
?  anyang cd test 
?  test touch file1 file2 file3
?  test ls
file1  file2  file3
?  test cd ..
?  anyang ls
learngit  newfile1  newfile2  newfile3  test
?  anyang rmdir test 
rmdir: failed to remove 'test': Directory not empty
?  anyang rm test                    // 此處如果不加 -r 參數,則會報錯,因為該目錄為非空目錄
rm: cannot remove 'test': Is a directory
?  anyang rm -r test
?  anyang ls
learngit  newfile1  newfile2  newfile3
  • mv file/directory path -- 移動文件位置(默認移動整個文件夾,包含文件夾下的內容)
  • mv file1 file2 -- 文件 file1 重命名為 file2

效果如下:

?  anyang ls
file1  learngit  test
?  anyang ls test 
?  anyang mv file1 test 
?  anyang ls test      
file1
?  anyang ls
learngit  test
?  anyang cd test 
?  test mv file1 test1
?  test ls
test1
  • cp file1 file2 -- 拷貝 file1 到 file2
  • cp -Rcp -r -- 遞歸拷貝目錄(默認不會移動目錄下的所有內容,除非加上參數 -R 或 -r)

效果如下:

?  anyang ls
learngit  test
?  anyang touch file1      
?  anyang ls
file1  learngit  test
?  anyang cp file1 test/test1
?  anyang cd test
?  test ls
test1
?  anyang mkdir newtest
?  anyang ls
file1  learngit  newtest  test
?  anyang cp test newtest        // 如不加 -r 或 -R 參數則會報錯,因為test目錄非空
cp: omitting directory 'test'
?  anyang cp -r test newtest
?  anyang cd newtest 
?  newtest ls
test
  • cat file -- 輸出全部文件內容
  • cat > file -- 將標準輸入重定向到 file,且只能創建新文件, 不能編輯已有文件。

效果如下:

?  anyang ls
file1  learngit  test
?  anyang cat file1 
Hello, anyang!
?  anyang cat > file2
Add some words!
^C
?  anyang cat file2  
Add some words!
  • more file -- 輸出文件內容,顯示滿一個屏幕時暫停,此時可按空格健繼續顯示下一頁,或按Q鍵停止顯示。
  • less file -- 輸出文件內容,顯示滿一個屏幕時暫停,此時除了可以按空格鍵向下顯示文件外,還可以利用上下鍵來顯示文件,當結束時只需在:后輸入Q即可。
  • head file -- 輸出文件前10行
  • tail file -- 輸出文件后10行
  • tail -f file -- 輸出文件內容,參數 -f 使 tail 不停地去讀文件最新的內容,可用于監控文件變化,Ctrl+c 終止監控。
  • ln -s file link -- 為文件 file 在另外一個位置建立一個同步的軟鏈接 link。

效果如下:

?  anyang ls
file1  learngit  test
?  anyang cat file1 
Hello, anyang!
?  anyang ls test 
?  anyang ln -s file1 test/file1
?  anyang ls test               
file1
?  anyang cd test 
?  test ls -l
total 0
lrwxrwxrwx 1 anyang anyang 5 12月  7 10:44 file1 -> file1
  • stat file -- 查看文件狀態
  • pwd -- 顯示當前工作目錄

效果如下:

?  anyang stat file1 
  File: 'file1'
  Size: 15          Blocks: 8          IO Block: 4096   regular file
Device: 809h/2057d  Inode: 402603      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  anyang)   Gid: ( 1000/  anyang)
Access: 2016-12-07 10:10:58.987863942 +0800
Modify: 2016-12-07 10:10:45.947557464 +0800
Change: 2016-12-07 10:10:45.975558132 +0800
 Birth: -
?  anyang pwd
/home/anyang/anyang

相關資料:

  1. 29個你必須知道的Linux命令: http://www.imooc.com/article/1285
  2. 常用命令行介紹: https://github.com/iamcoach/console/blob/master/COMMANDS.md
  3. 常用命令行cheet sheet: https://bbs.excellence-girls.org/topic/167
  4. 書籍《鳥哥的Linux私房菜》: https://book.douban.com/subject/4889838/
  5. Ubuntu各種技巧:http://wiki.ubuntu.org.cn/UbuntuSkills
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 初學Linux,記錄資料,以備留存,親手測試了一部分,有的正確,不正確的也改了,沒有全部測試,如有誤,望大神們不吝...
    世外大帝閱讀 5,577評論 1 32
  • centos7 常用命令管理centos服務器的時候常會對文件進行一些常規操作,除了ftp之外了解在ssh下必要的...
    小線亮亮閱讀 1,215評論 0 2
  • 系統信息 arch 顯示機器的處理器架構(1)uname -m 顯示機器的處理器架構(2)uname -r 顯示正...
    jsondream閱讀 559評論 4 13
  • 系統信息 arch 顯示機器的處理器架構(1) uname -m 顯示機器的處理器架構(2) uname -r 顯...
    黑夜的眸閱讀 384評論 0 0
  • 如果你現在在西湖邊,你會發現我留下的身影! 如果你恰好路過斷橋,你會發現我留下的腳步! 如果你愿意走走蘇堤,你會聽...
    Eason丿閱讀 285評論 0 0