Skip to content

Linux基础

ruifengwei edited this page Jun 11, 2020 · 4 revisions

常用命令

命令
简介 链接
pwd 显示当前目录 链接
cat / less 显示所有文本内容 / 查看文件 链接
cp 拷贝 链接
mv 移动文件路径 / 重命名 链接
touch 创建文件 / 修改文件访问时间 链接
mkdir 创建文件夹 链接
rm / rmdir 删除文件 / 文件夹 链接
head / tail 输出文件的开头/结尾 链接

以上命令都可使用man查看更详细的使用文档,如 man ls

LS(1)                     BSD General Commands Manual                    LS(1)
NAME
     ls -- list directory contents

SYNOPSIS
     ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays its name as well as any
     requested, associated information.  For each operand that names a file of type directory, ls displays
     the names of files contained within that directory, as well as any requested, associated information.

     If no operands are given, the contents of the current directory are displayed.  If more than one oper-
     and is given, non-directory operands are displayed first; directory and non-directory operands are
     sorted separately and in lexicographical order.

     The following options are available:

     -@      Display extended attribute keys and sizes in long (-l) output.

     -1      (The numeric digit ``one''.)  Force output to be one entry per line.  This is the default when
             output is not to a terminal.

     -A      List all entries except for . and ...  Always set for the super-user.

     -a      Include directory entries whose names begin with a dot (.).

     -B      Force printing of non-printable characters (as defined by ctype(3) and current locale settings)
             in file names as \xxx, where xxx is the numeric value of the character in octal.

     -b      As -B, but use C escape codes whenever possible.

     -C      Force multi-column output; this is the default when output is to a terminal.

     -c      Use time when file status was last changed for sorting (-t) or long printing (-l).

     -d      Directories are listed as plain files (not searched recursively).

大多数程序命令安装之后,都可以用 --help 查看参数和介绍,如nvm --help。 实时查看服务器的日志文件,用tail命令tail -f ~/logs/test.logtail -f会把 test.log 文件里的最尾部的内容显示在屏幕上,并且不断刷新,只要 test.log 更新就可以看到最新的文件内容。

权限

linux的文件权限可以通过 ls -l查看:

-rw-r--r--@ 1 caoxueying  staff  1882  6  3 16:10 linux.md

前十个字符表示文件属性,第一位表示文件类型,后面九位表示文件权限:

可以看到,linux.md文件是一个普通文件,其所有者有读写权限,所有者所在群组和其他人只有读权限。

使用chmod命令可修改文件权限,如下,给所有人加build.sh文件的读写执行权限:

  chmod 777 build.sh

chmod 命令的参数采用八进制代表文件权限,具体的参数对应权限如下:

八进制 二进制 文件模式
0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx

一般情况,直接编辑根目录下的某些文件,保存时会提示你只有读权限而无法保存。 此时,需要切换管理员身份执行命令(sudo + 命令):

  # 编辑/新建根目录下的文件
  sudo vim /data/webapps/test

关于权限的更多详细内容,可参考附件

进程

命令
简介
链接
ps 显示当前所有进程运行情况 链接
top 实时显示当前所有任务资源占用情况 链接
kill -9 结束进程 链接

程序启动失败,有时候是因为端口占用,使用 lsof 命令检查:

~ lsof -i:8080
COMMAND     PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Google      371 caoxueying   25u  IPv4 0xf533b4e176032429      0t0  TCP localhost:64961->localhost:http-alt (CLOSE_WAIT)
Google      371 caoxueying   27u  IPv4 0xf533b4e197ba4aa9      0t0  TCP localhost:64962->localhost:http-alt (CLOSE_WAIT)
Google      371 caoxueying   31u  IPv4 0xf533b4e182176da9      0t0  TCP localhost:64996->localhost:http-alt (CLOSE_WAIT)
Google      371 caoxueying   33u  IPv4 0xf533b4e176772129      0t0  TCP localhost:64997->localhost:http-alt (CLOSE_WAIT)
WeChat    25420 caoxueying   31u  IPv6 0xf533b4e19c3f06e9      0t0  TCP 192.168.1.100:52492->157.255.174.105:http-alt (ESTABLISHED)
node      89655 caoxueying   23u  IPv6 0xf533b4e1aacacd69      0t0  TCP *:http-alt (LISTEN)

~ lsof -i:8080 | grep node
node      89655 caoxueying   23u  IPv6 0xf533b4e1aacacd69      0t0  TCP *:http-alt (LISTEN)

如果如上所示,8080端口被占用,可以使用kill -9 PID强制结束占用端口的进程:

~ kill -9 89655

网络

命令
简介
链接
ssh 远程登录服务器 链接
netstat 查看网络系统的状态信息 链接
ping 测试主机之间网络的连通性。向目标主机发送数据包,网络设备收到数据包后会回应。 链接
curl 各种类型网络请求,文件上传下载 链接

压缩 | 归档

命令
简介
链接
gzip / gunzip 文件压缩解压缩命令 操作后原文件会被输出文件替代 链接
tar
以tar结尾的文件是用tar归档的
-cf 创建名为test-dir.tar的归档文件,该文件内包含test-dir文件夹所有内容tar -cf test-dir.tar test-dir
-tf 列出归档文件中所有文件tar -tf test-dir.tar
-xf 从归档文件中提取到当前路径tar -xf test-dir.tar
链接
zip / unzip zip压缩解压缩,用于和windows系统交换文件zip -r test-dir.zip test-dir unzip test-dir.zip 链接

命令行常用快捷键

快捷键
简介
Ctrl + A 移动光标到行首
Ctrl + E 移动光标到行尾
Ctrl + L / Command + K 清屏
tab 自动补齐 按两次展示所有可用补齐
Ctrl + R 搜索历史命令

命令行编辑器 vi的使用

更多详细请参考: