Linux 基础常见命令

本文介绍了Linux文件系统的树状结构,包括绝对路径和相对路径的概念。详细讲解了如何使用`pwd`、`cd`命令切换和查看工作目录,以及`ls`命令的不同选项来列举目录内容。此外,还涵盖了文件的创建、删除、移动和复制,以及`cat`、`head`、`tail`、`grep`等命令的使用。文章还提到了输入/输出重定向和进程管理,以及`man`命令获取帮助信息。
摘要由CSDN通过智能技术生成

目录

我是谁

我在哪:工作目录(文件夹)位于何处

Linux 上的文件系统树介绍

绝对路径(absolute path)vs 相对路径(relative path) 

打印工作目录

修改工作目录(切换主机的所在位置)

1. 指定路径,跑到这个路径下

//绝对路径

//相对路径 

 2. 一键回家

3. 左右横跳

我周围有什么: 查看某目录下的文件列表

ls中的一些选项

查看所有文件

删除文件/目录

创建目录 make directory # 可以传入多个路径

创建一个空白的文本文件 #可以传入多个路径

小Tips

Tree命令 #可以让文件按照树的格式显示,但是使用前需要先安装

当遇到希望终止当前进程的时候,此时可以按下Ctrl + C (Control + C)给当前执行程序发送中断信号

查看文本文件内容相关的命令

PS: 对比Ctrl + C 和 Ctrl + D的区别

head 和 tail 

管道(pipe)

匹配输出(grep)

less || more 只显示一页

查找文件 find 从某个目录开始,查找符合一定条件的所有文件

复制和移动文件 :用树的视角去理解

移动(move 写做 mv)

复制(cp)

diff命令(比较)

echo 命令(类似于print,但是普通的echo不存在转译)

PS:echo中单引号双引号区别对比 

​编辑

输入/输出重定向(redirect)

标准输入重定向:进程的标准输入不再是键盘,而是从一个路径对应的文件中读取内容

标准输出重定向:进程的标准输出不再是屏幕,而是一个文件路径,所有的输出都会写到该文件

man manual(说明手册) 的缩写,比 --help 更全的文档,按q退出

看时间date

查看进程列表ps

查看内存free

杀死指定进程

Ubuntu 的包管理工具 apt


清理屏幕(滚屏)

$ clear

或者使用快捷键 Ctrl + L


我是谁

$ whoami

$ who


我在哪:工作目录(文件夹)位于何处

Linux 上的文件系统树介绍

文件系统主要使用树形结构组织,比如 Windows 上用森林组织

Linux 的文件系统树只有一个根结点 /

绝对路径(absolute path)vs 相对路径(relative path) 

路径(Path): 从根到当前结点的路径。比如 aba /a/AB/aba

Windows 路径的分隔符使用 \ "\\",Linux 路径使用 /

绝对路径:从根开始的路径。即开头是 /。 /a/AB/aba

相对路径:相对于当前工作目录所在的路径。

pwd 是在 CA 目录下,则 aba 的相对路径

".." 代表父结点 ../../a/AB/aba

pwd 是在 c 目录下,则 aba 的相对路径 ../a/AB/aba

"." 代表在本结点不动

pwd: CA 结点下 ./././././ 还在 CA 结点


打印工作目录

$ pwd #print working directory 打印工作目录

 


修改工作目录(切换主机的所在位置)

cd: change directory

1. 指定路径,跑到这个路径下

修改工作目录(切换主机的所在位置)

//绝对路径
$ cd /home #cd: change directory 修改工作目录 cd后面接绝对地址

//相对路径 
$ cd ../ #cd后面接了个相对路径 修改工作目录到当前工作目录的父目录


 2. 一键回家

$ cd #单cd 一键回家


3. 左右横跳

$ cd - #返回上一次的地址


我周围有什么: 查看某目录下的文件列表

$ ls #显示当前工作目录下的文件列表

$ ls ..# 查看当前工作目录的父目录下的文件列表

 


ls中的一些选项

$ ls --help #ls中的一些选项
ubuntu@VM-0-4-ubuntu:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory totals
  -l                         use a long listing format
  -L, --dereference          when showing file information for a symbolic
                               link, show information for the file the link
                               references rather than for the link itself
  -m                         fill width with a comma separated list of entries
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print entry names without quoting
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
                               (overrides QUOTING_STYLE environment variable)
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -s, --size                 print the allocated size of each file, in blocks
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            change the default of using modification times;
                               access time (-u): atime, access, use;
                               change time (-c): ctime, status;
                               birth time: birth, creation;
                             with -l, WORD determines which time to show;
                             with --sort=time, sort by WORD (newest first)
      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below
  -t                         sort by time, newest first; see --time
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid '\n' with -q or -b
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1).  If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.

Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'

查看所有文件

$ -ls -a

$ -ls --all #查看所有文件

Linux 上 . 开头的文件名认为是默认隐藏的 

$ ls --almost-all  #此时相比较$ ls -a 少了一个自己,也少了一个父目录

$ ls -A

$ ls -l                    # 以长格式显示当前目录中的文件和目录 列出详细信息

$ ls -l --all

$ ls -l -A 

$ ls -t  # 按照修改时间排序显示当前目录中的文件和目录

$ ls -d  #只列出目录(不递归列出目录内的文件)

$ ls -lt

$ ls -ld

ubuntu@VM-0-4-ubuntu:~$ pwd
/home/ubuntu
ubuntu@VM-0-4-ubuntu:~$ ls -l
total 0
ubuntu@VM-0-4-ubuntu:~$ ls -d
.
ubuntu@VM-0-4-ubuntu:~$ ls -t
ubuntu@VM-0-4-ubuntu:~$ ls -t /home
lighthouse  ubuntu
ubuntu@VM-0-4-ubuntu:~$ ls -alt
total 40
-rw------- 1 ubuntu ubuntu 2924 Jul 24 16:38 .bash_history
-rw-rw-r-- 1 ubuntu ubuntu   73 Jul 24 14:29 .pydistutils.cfg
drwx------ 2 ubuntu ubuntu 4096 Jul 24 14:29 .ssh
drwxr-xr-x 4 root   root   4096 Jul 24 14:29 ..
drwxr-x--- 5 ubuntu ubuntu 4096 May 26 16:01 .
drwxrwxr-x 2 ubuntu ubuntu 4096 May 18  2022 .pip
drwx------ 2 ubuntu ubuntu 4096 May 18  2022 .cache
-rw-r--r-- 1 ubuntu ubuntu  220 Jan  7  2022 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Jan  7  2022 .bashrc
-rw-r--r-- 1 ubuntu ubuntu  807 Jan  7  2022 .profile
ubuntu@VM-0-4-ubuntu:~$ ls -al
total 40
drwxr-x--- 5 ubuntu ubuntu 4096 May 26 16:01 .
drwxr-xr-x 4 root   root   4096 Jul 24 14:29 ..
-rw------- 1 ubuntu ubuntu 2964 Jul 24 16:38 .bash_history
-rw-r--r-- 1 ubuntu ubuntu  220 Jan  7  2022 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Jan  7  2022 .bashrc
drwx------ 2 ubuntu ubuntu 4096 May 18  2022 .cache
drwxrwxr-x 2 ubuntu ubuntu 4096 May 18  2022 .pip
-rw-r--r-- 1 ubuntu ubuntu  807 Jan  7  2022 .profile
-rw-rw-r-- 1 ubuntu ubuntu   73 Jul 24 14:29 .pydistutils.cfg
drwx------ 2 ubuntu ubuntu 4096 Jul 24 14:29 .ssh
ubuntu@VM-0-4-ubuntu:~$ ls -ald
drwxr-x--- 5 ubuntu ubuntu 4096 May 26 16:01 .

删除文件/目录

rm 路径
其中 $ rm -i 路径      --interactive   交互式
        S rm -f 路径      --force         强制的(不问了)
     
通常情况下,路径只能是普通文件

如果想要删除目录必须是
$ rm -rf 路径   # -r 可以删除目录   -f 不询问直接删除 

                # 遍历方式是深度优先
                # Windows 的删除只是移动到“回收站”目录下了
                # Linux 的删除是真删除

                # 现在可以用,但以后生产环境千万别用


创建目录 make directory # 可以传入多个路径

mkdir                    # 创建目录
mkdir -p  路径       # 更常见的用法

mkdir a/b/c/d       如果 a/b/c 存在,则创建成功。
                           如果 a/b/c 不存在,则创建失败。
                    
mkdir -p a/b/c/d    如果 a/b/c 存在,则创建成功。
                            如果 a/b/c 不存在,则连同不存在的目录都创建成功

$ mkdir a/b/c/d       #如果 a/b/c 存在 则创建成功
                      #如果 a/b/c 不存在 则创建失败
                    
$ mkdir -p a/b/c/d    #如果 a/b/c 存在 则创建成功
                      #如果 a/b/c 不存在 则连同不存在的目录都创建成功

ubuntu@VM-0-4-ubuntu:~$ pwd
/home/ubuntu
ubuntu@VM-0-4-ubuntu:~$ ls
ubuntu@VM-0-4-ubuntu:~$ mkdir /home/ubuntu/1
ubuntu@VM-0-4-ubuntu:~$ ls
1
ubuntu@VM-0-4-ubuntu:~$ mkdir /home/ubuntu/2/3 #此时/home/ubuntu/2不存在,则创建失败
mkdir: cannot create directory ‘/home/ubuntu/2/3’: No such file or directory
ubuntu@VM-0-4-ubuntu:~$ ls
1
ubuntu@VM-0-4-ubuntu:~$ mkdir -p /home/ubuntu/2/3
ubuntu@VM-0-4-ubuntu:~$ ls
1  2
ubuntu@VM-0-4-ubuntu:~$ ls ./2
3

创建一个空白的文本文件 #可以传入多个路径

!!! Linux 上没有文件名后缀的概念 !!!

touch   路径(某个普通文件)  触摸

1)如果路径对应的文件不存在,则创建一个空白文件
2)如果路径对应的文件存在,则修改文件的最后修改时间

如果真的需要在 Linux 上创建有内容的文件,直接在 Windows 上创建好,然后使用文件管理器将文件发送到 Linux 上即可。

ubuntu@VM-0-4-ubuntu:~$ ls
ubuntu@VM-0-4-ubuntu:~$ touch /home/ubuntu/hello.txt #此时文件不存在 创建空白文件
ubuntu@VM-0-4-ubuntu:~$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jul 25 09:59 hello.txt
ubuntu@VM-0-4-ubuntu:~$ touch /home/ubuntu/hello.txt #此时文件存在 修改该文件的最后修改时间
ubuntu@VM-0-4-ubuntu:~$ ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jul 25 10:00 hello.txt


小Tips

1.mkdir 还是 touch,都可以一次传入多个路径

2.输入命令,可以使用键盘上的 Tab 键,要求 shell 自动补全(auto completion),可以补全命令 或者 路径

3.通过键盘的 上 / 下 键翻阅历史输入

4.通过键盘的 左 / 右 键改变光标(cursor)位置,进而修改历史命令


Tree命令 #可以让文件按照树的格式显示,但是使用前需要先安装

此时是没法直接用tree命令的,按照他的提示输入sudo apt install tree即可安装 

此时则代表安装成功


当遇到希望终止当前进程的时候,此时可以按下Ctrl + C (Control + C)给当前执行程序发送中断信号

Ctrl + C 为当前正在执行程序发送一个中断信号(Interrupt Signal sigint)

意思是让这个进程赶紧中断。


查看文本文件内容相关的命令

$ cat 路径  #(必须一个有读权限的普通文件,最好是文本文件)

$ cat /home/ubuntu/1.txt

当读取文件过程中出现了乱码,可以使用reset命令恢复,如果这样仍然无法恢复,则只能断开重新连接  

$ cat -n #在显示内容的同时带一个行号出来

$ tac -n  #倒着显示

$ cat 回车 # 什么路径都不跟着 实际上是标准输入中读取内容 然后输出标准输出
          # Ctrl + C (Control + C) || Ctrl + D(Control + D)

 

PS: 对比Ctrl + C 和 Ctrl + D的区别

例:

public class Cat {
    public static void main(String[] args) {
        if (args.length == 0) {
            // System.in 系统提供的标准输入(stdin)
            Scanner sca = new Scanner(System.in);
            while (sca.hasNextLine()) {    // Ctrl + D
                String line = sca.nextLine();
                // System.out 系统提供的标准输出(stdout)
                System.out.println(line);
            }
        }
}

// Ctrl + C 不让代码执行结束了,执行到哪行算哪行
// Ctrl + D 说明输入结束了,正常退出【优先】

Ctrl + C 不让代码执行结束了,执行到哪行算哪行
Ctrl + D 说明输入结束了,正常退出【优先】


head 和 tail 

head 和 tail   后面跟着的文件要求 :1)普通文件  2)文本文件  3)有读权限
头          尾

head:只显示头几行(默认是 10 行)
tail:只显示尾几行(默认是 10 行)

$ head [路径] #显示该文件的头十行

$ tail [路径] #显示该文件的尾十行

head -n 100 路径    显示文件的前 100 行
tail -n 20  路径    显示文件的后 20 行

$ head -n 100 [路径]    # 显示文件的前 100 行
$ tail -n 20  [路径]    # 显示文件的后 20 行

 head  不带路径,读取的是标准输入
tail  不带路径,读取的是标准输入

head -n 不带路径

tail -n 不带路径

PS:tail要等标准输入完以后再输出

$ head 回车 #不带路径,读取的是标准输入
$ tail 回车 #不带路径,读取的是标准输入
$ head -n 100 回车 #不带路径,读取的是标准输入
$ tail -n 20  回车 #不带路径,读取的是标准输入

想要看文件有多少行

wc -l [路径]  

$ wc -l [路径] 


管道(pipe)

程序(program)vs 进程(process)

程序:提前编写好的,为完成一定目的而组成的一些指令+相关的数据。通常是一个或者一组文件的形式存在。—— 菜谱

进程:程序的一次执行过程。—— 利用菜谱的一次做菜的过程。

标准输入(stdin) System.in

标准输出(stdout) System.out

标准错误输出(stderr) System.err

OS 在启动一个进程时,就会打开 stdin、stdout、stderr。默认情况下,这 3 个对应的都是终端(terminal),可以简单理解:stdin 就是键盘、stdout 和 stderr 就是屏幕。

不同进程之间,默认是无法直接通信的,但是 OS 提供了一些机制,供进程间通信,其中有一种机制叫做管道(pipe)

 

Shell 中使用 "|" 连接两个命令

$ head -n 30 Hello.java    |    cat -n

# 启动了两个进程:前一个进程是 head 进程
               $ head -n 30 Hello.java  # 取前 30 行,输出到标准输出 -> 屏幕
#              后一个进程是 cat 进程
               $ cat -n                 # 读取标准输入的内容,输出到标准输出上,带上行号

# head -n 30 Hello.java | cat -n  最终的效果:输出前 30 行,并且带上行号

!!!上述两种之所以没区别是因为两者先后顺序并不影响最终显示,但是并不代表先后顺序不存在影响

如下情况可以说明,顺序的影响,本质是 | 是建立了一个通道,将上一个的标准输出连接到下一个进程的标准输入中,先后顺序就一定会存在影响


匹配输出(grep)

grep       gnu(开源组织) rep 进行匹配输出的

$ grep 'hello world'  Hello.java
# 读取 Hello.java 的内容,只把包含 "hello world" 的行输出到标准输出上
# 默认大小写敏感

$ grep Linux   #默认大小写敏感
$ grep -i Linux       Hello.java
# -i --in case sensitive
# case 大小写
# sensitive 敏感
# case-sensitive 大小不敏感
# in-case-sensitive 大小写不敏感

$ grep -v Linux  Hello.java   # 输出不含 "Linux" 的内容
#      -v, --invert-match        select non-matching lines

$ grep 回车  # 如果不跟路径,从标准输入读取

less || more 只显示一页

less 路径       只显示一页,按 q 退出   less 可以往前/往后, 通过上下键控制,通过空格
more 路径     只显示一页,按 q 退出   more可以往前/往后,通过上下键控制,通过空格

less/more      不带路径,就是标准输入

当类似匹配输出结果输出过多,此时可以加上less命令,使其只显示一页

$ grep -i 's' add.java | less #在add.java这个文件大小写不敏感找含有“s”的行,并且只显示一页

查找文件 find 从某个目录开始,查找符合一定条件的所有文件

ubuntu@VM-0-4-ubuntu:~$ find /home/ -name "*.java"
/home/ubuntu/Strings.java
/home/ubuntu/add.java
find: ‘/home/lighthouse’: Permission denied

复制和移动文件 :用树的视角去理解

移动(move 写做 mv)

把树上一个结点,移动另一个位置

!!!!无论怎么移动 首先需要保证的是该路径有效

/home/ubuntu/hello.java 移动 /home/nothing/
/home/ubuntu/hello.java 结点没有了
/home/nothing/hello.java 结点出现
先剪切 hello.java,去 /home/nothing/ 目录下,粘贴


/home/ubuntu/hello.java 移动 /home/nothing/hello.java
/home/ubuntu/hello.java 结点没有了
/home/nothing/hello.java 结点出现
先剪切 hello.java,去 /home/nothing/ 目录下,粘贴

实际上上述两者一致

/home/ubuntu/hello.java 移动 /home/nothing/world.java
/home/ubuntu/hello.java 结点没有了
/home/nothing/world.java 结点出现
先剪切 hello.java,去 /home/nothing/ 目录下,粘贴
再把 /home/nothing/hello.java 改名成 world.java

/home/ubuntu/hello.java 移动 /home/ubuntu/world.java
/home/ubuntu/hello.java 结点没有了
/home/ubuntu/world.java 结点出现
把 /home/ubuntu/hello.java 改名成 world.java

$ mv 移动文件路径 移动后文件路径 #无论怎么移动 首先需要保证的是该路径有效

复制(cp)

复制语义,默认情况下,只能复制结点,不能复制子树

普通文件就是结点,目录是子树

$ cp 某个文件  dest/
$ cp 某个文件  另一个文件
$ cp -r 某个目录 目标目录  #-r 的本质依旧是递归,通过递归来处理目录
ubuntu@VM-0-4-ubuntu:~$ mkdir /home/ubuntu/test/
ubuntu@VM-0-4-ubuntu:~$ tree
.
├── 1.jpg
├── add.java
├── Strings.java
└── test

1 directory, 3 files
ubuntu@VM-0-4-ubuntu:~$ cp add.java ./test/
ubuntu@VM-0-4-ubuntu:~$ tree
.
├── 1.jpg
├── add.java
├── Strings.java
└── test
    └── add.java

1 directory, 4 files
ubuntu@VM-0-4-ubuntu:~$ cp add.java ./test/newadd.java
ubuntu@VM-0-4-ubuntu:~$ tree
.
├── 1.jpg
├── add.java
├── Strings.java
└── test
    ├── add.java
    └── newadd.java

1 directory, 5 files
ubuntu@VM-0-4-ubuntu:~$ cp -r ./test ../
cp: cannot create directory '../test': Permission denied
ubuntu@VM-0-4-ubuntu:~$ mkdir /home/ubuntu/1
ubuntu@VM-0-4-ubuntu:~$ cp -r ./test/ ./1/
ubuntu@VM-0-4-ubuntu:~$ tree
.
├── 1
│   └── test
│       ├── add.java
│       └── newadd.java
├── 1.jpg
├── add.java
├── Strings.java
└── test
    ├── add.java
    └── newadd.java

3 directories, 7 files

diff命令(比较)

$ diff add.java add1.java #对比两个文件,一样则什么都不打印
                          #两个文件不一样会显示两个文件的区别


echo 命令(类似于print,但是普通的echo不存在转译)

$ echo "hello world"      #类似这类写法,此时打印 hello world
$ echo "hello world\n"    #由于其并不存在转译,此时打印 hello world\n
$ echo -e "hello world\n" #倘使如果希望转移,则需要写作 echo -n

PS:echo中单引号双引号区别对比 

ubuntu@VM-0-4-ubuntu:~$ msg="hello world"
ubuntu@VM-0-4-ubuntu:~$ echo "${msg}" #此时我通过echo将其提解析出来
hello world
ubuntu@VM-0-4-ubuntu:~$ echo '${msg}' #如果此时使用的是单引号则无法正常解析变量
${msg}


输入/输出重定向(redirect)

标准输入重定向:进程的标准输入不再是键盘,而是从一个路径对应的文件中读取内容

$ cat -n   <  test/Hello.java

                        # cat -n 还是从标准输入读取

                        # < 文件路径    完成标准输入的重定向

                        # cat -n 的标准输入变成了文件中的内容了

$ cat -n < add.java
                    # cat -n 还是从标准输入读取

                    # < 文件路径    完成标准输入的重定向

                    # cat -n 的标准输入变成了文件中的内容了

标准输出重定向:进程的标准输出不再是屏幕,而是一个文件路径,所有的输出都会写到该文件

$ echo "hello world" > 1.txt

                   # echo 还是从标准输出输出

                   # > 文件路径    完成标准输出的重定向,如果该文本不存在,则默认创建该文本

                   # echo 的标准输出输出到1.txt文本中了

  1. 文件如果不存在,则会创建该文件并且写入(要求是有权限)

  2. 如果文件存在

    1. '> 路径': 覆盖原来的内容(删除文件中的原有内容,把新的内容写入)

    2. '>> 路径': 追加模式,在原有的内容后边接着跟新内容 

echo "hello world" > 1.txt


man manual(说明手册) 的缩写,比 --help 更全的文档,按q退出

$ man man
$ man ls

看时间date

$ date
ubuntu@VM-0-4-ubuntu:~$ date
Wed Jul 26 09:19:55 AM CST 2023

查看进程列表ps

$ ps            #查看自己的进程
$ ps aux | less #查看所有人的进程,并只显示一页
$ top           #实时查看,按大些的P键,默认按cpu来排序


查看内存free

$ free
$ free -h


杀死指定进程

$ kill [端口号] 

Ubuntu 的包管理工具 apt

apt list                #列出所有可用的包

sudo apt install <包名> #安装所使用的包 通过sudo给一个权限

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值