命令行学习笔记(文件相关命令)

  • 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
    原文作者:TW安洋
    原文地址: https://www.jianshu.com/p/df494e45d9d6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞