美文网首页
部分简单 shell 命令合集

部分简单 shell 命令合集

作者: 有情怀的程序猿 | 来源:发表于2020-02-17 15:15 被阅读0次

遍历目录

  • cd 切换到指定目录
    cd 命令可接受单个参数 destination ,用以指定想切换到的目录名。如果没有为cd 命令指定目标路径,它将切换到用户主目录。

  • pwd 查看当前目录
    pwd 命令可以显示出shell会话的当前目录,这个目录被称为当前工作目录 。
    摘录来自: ePUBw.COM. “Linux命令行与shell脚本编程大全(第3版)。” Apple Books.

  • . 单点符(.),表示当前目录

  • .. 双点符(..),表示当前目录的父目录

christine@server01:~/Documents$ pwd
/home/christine/Documents
christine@server01:~/Documents$ cd ../Downloads
christine@server01:~/Downloads$ pwd
/home/christine/Downloads
christine@server01:~/Downloads$

文件和目录列表

  • ls 显示当前目录下的文件和目录
  • ls -a 可展示隐藏文件
  • ls -l 展示长列表信息, 会包含了目录中每个文件的更多相关信息
    • 文件类型,比如目录(d )、文件(- )、字符型文件(c )或块设备(b )
    • 文件的权限
    • 文件的硬链接总数
    • 文件属主的用户名
    • 文件属组的组名
    • 文件的大小(以字节为单位)
    • 文件的上次修改时间
    • 文件名或目录名
$ ls -l
total 48
drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Desktop
drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Documents
drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Downloads
-rw-r--r-- 1 christine christine 8980 Apr 22 13:36 examples.desktop
-rw-rw-r-- 1 christine christine    0 May 21 13:44 fall
-rw-rw-r-- 1 christine christine    0 May 21 13:44 fell
-rw-rw-r-- 1 christine christine    0 May 21 13:44 fill
-rw-rw-r-- 1 christine christine    0 May 21 13:44 full
drwxr-xr-x 2 christine christine 4096 May 21 11:39 Music
-rw-rw-r-- 1 christine christine    0 May 21 13:25 my_file
-rw-rw-r-- 1 christine christine    0 May 21 13:25 my_scrapt
  • ?问号(? )用于过滤器字符串中替代任意位置的单个字符
$ ls -l my_scr?pt
-rw-rw-r-- 1 christine christine  0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$
  • *星号(* )代表零个或多个字符
$ ls -l my_s*t
-rw-rw-r-- 1 christine christine  0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$

处理文件

  • touch xxx 创建文件
  • cp source destination 复制文件 source 为 destination
  • cp -i source destination 复制文件且问询是否覆盖已有同名文件
$ touch test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:17 test_one

$ cp  test_one test_two   // 复制 test_one  为 test_two
$ cp -i touch test_one test_two // 复制 test_one 为 test_two 且查询是否由同名文件
“cp: overwrite 'test_two'? n”

$ cp -i /etc/NetworkManager/NetworkManager.conf  . // 复制文件 /etc/NetworkManager/NetworkManager.conf  到当前文件目录
  • cp -R xxx xxxy 递归复制整个文件xxx 为xxxy
  • mv xx xxx 重命名或者移动文件
  • mv -i xx xxx 重命名或者移动文件且询问是否覆盖已有同名文件
$ ls  f?ll
296717 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fall // 原文件名称 fall
$
$ mv fall  fzll  // 重命名文件名 fzll
$ ls  f?ll
296717 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll // 重新查看文件,名称 fzll

// 移动目录
// fzll 原目录在 /home/christine/fzll 
现在执行
$ mv fzll  Pictures/
$
// 现在fzll 目录在  /home/christine/Pirctures中

  • rm -i xxx 删除某个文件且删除前提示是否确认删除
$ rm -i fall //执行删除命令,且提示是否真要删除
rm: remove regular empty file 'fall'? y
$
$ ls -l fall
ls: cannot access fall: No such file or directory
$
  • rm -f xxx 强制删除(谨慎使用)

处理目录

“有些命令(比如cp 命令)对文件和目录都有效,而有些只对目录有效。”

  • 创建目录 mkdir xxx
$ mkdir New_Dir
$ ls -ld New_Dir
drwxrwxr-x 2 christine christine 4096 May 22 09:48 New_Dir
$

系统创建了一个名为New_Dir的新目录。注意,新目录长列表是以d开头的。这表示New_Dir并不是文件,而是一个目录。

  • 同时创建多个目录和子目录 mkdir -p xxx/yyy/zzz
$ mkdir -p New_Dir/Sub_Dir/Under_Dir
  • 删除目录 rmdir xxx

rmdir 命令只删除空目录, 如果目录有文件则删除失败
rmdir 并没有-i 选项来询问是否要删除目录
如果目录内有文件需要先删除内部文件才可以删除目录

  • rmdir -r xxx

可以在整个非空目录上使用rm 命令。使用-r 选项使得命令可以向下进入目录,删除其中的文件,然后再删除目录本身
这种方法虽然可行,但很难用。注意,你依然要确认每个文件是否要被删除。如果该目录有很多个文件和子目录,这将非常琐碎。

$ ls -l My_Dir
total 0
-rw-rw-r-- 1 christine christine 0 May 22 10:02 another_file
$
$ rm -ri My_Dir
rm: descend into directory 'My_Dir'? y
rm: remove regular empty file 'My_Dir/another_file'? y
rm: remove directory 'My_Dir'? y
$
$ ls -l My_Dir
ls: cannot access My_Dir: No such file or directory
$
  • rm -rf xxx 一次性彻底删除目录和内部文件

查看文件内容

  • file 它能够探测文件的内部,并决定文件是什么类型的
$ file my_file
my_file: ASCII text
$
$ file New_Dir
New_Dir: directory

file 命令不仅能确定文件中包含的文本信息,还能确定该文本文件的字符编码,ASCII
而且还可以作为一种区分目录的方法

查看整个文件
  • cat cat 命令是显示文本文件中所有数据的得力工具。
$ cat test1
hello

This is a test file.


That we'll use to       test the cat command.
$
  • cat -n xxx 会给内容打上行号

cat 命令的主要缺陷是:一旦运行,你就无法控制后面的操作,
如果文件很大则一闪而过。
所以:
可以使用 more / less 命令,可以实现上下翻页

  • more /etc/bash.bashrc
  • less /etc/bash.bashrc

但是如果一个很大文件我们要的信息在文件结尾 / 中间部分,怎么搞,翻页不翻死了?

  • tail xx会默认现实文件的末尾十行
  • tail - n number xx -n后的数字可以指定默认展示末尾倒数的第 number

tail 功能一样的是 head

  • head xx会默认现实文件的开头的十行
  • head - n number xx -n后的数字可以指定默认展示开头正数的第 number

相关文章

  • 部分简单 shell 命令合集

    遍历目录 cd 切换到指定目录cd 命令可接受单个参数 destination ,用以指定想切换到的目录名。如果没...

  • 第八节Linux下的帮助命令

    一、内建命令与外部命令 1、内建命令: shell程序一部分,命令简单,系统加载时shell被加载并驻留在内存中...

  • Shell命令合集

    C cat zdd 浏览文件zdd的内容 cat zdd1 zdd2 浏览多个文件的内容 cat -n zdd浏览...

  • adb shell ls /system/bin

    adb shell ls /system/bin 可以查看可用adb shell 命令。 并且大部分命令 可以通过...

  • 常用shell命令合集

    安装HomeBrew 官网安装链接(一般需要翻墙) 国内镜像资源 gitLab与ssh key 查看本机的ssh ...

  • 运维高手一:工作累积的最常用 Shell 命令合集及使用技巧

    本文主要讲解 Shell 命令合集,以及对控制台的使用技巧。在这之前你需要掌握如下三部分的知识内容: 需要熟悉掌握...

  • Linux下的帮助命令

    一,内命建令与外部命令 内建命令:内建命令是 shell 程序的一部分,包含比较简单的 Linux 系统命令。通常...

  • Linux下的帮助命令

    内建命令与外部命令 内建命令实际上是 shell 程序的一部分,其中包含的是一些比较简单的 Linux 系统命令,...

  • iOS 终端 shell 操作,Mac 操作快捷键

    shell 操作命令 简单的shell 命令操作指令 在执行完ls -l 命令后,会列出该目录下的文件,此时要打开...

  • shell 语言相关

    shell学习记录 shell是什么 简单来说,shell是一个命令解释器,将用户输入的命令解释给操作系统内核。它...

网友评论

      本文标题:部分简单 shell 命令合集

      本文链接:https://www.haomeiwen.com/subject/bczvfhtx.html