使用vscode远程连接服务器的教程在另一篇已经写了,这里主要列举一些常见的linux操作命令
1.文件与目录操作命令
-
ls
列出当前目录下的内容
$ ls
Desktop Documents Downloads myfile.txt
-
ls -l
展示目录下文件或目录的详细信息,表格内为每一项具体含义
$ ls -l
-rw-r--r-- 1 user user 4096 Apr 24 10:00 myfile.txt
drwxr-xr-x 2 user user 4096 Apr 24 09:45 Documents
序号 | 含义 | 示例 |
1 | 权限(包括文件类型) | -rw-r--r-- |
2 | 硬链接数(文件被引用的次数) | 1 |
3 | 所有者用户名 | user |
4 | 所属用户组 | user |
5 | 文件大小(字节) | 4096 |
6 | 最后修改时间 | Apr 24 12:00 |
7 | 文件名 | example.txt |
额外用法:
- 查看当前目录中所有文件(包括隐藏文件)的详细信息:
ls -la
- 按时间排序(最近修改的在前)
ls -lt
- 查看文件大小(建议配合
-h
)
ls -lh
- pwd:显示当前路径
$ pwd
/home/user
- cd:切换目录
$ cd Documents
$ pwd
/home/user/Documents
- mkdir:创建目录
$ mkdir new_folder
$ ls
Desktop Documents Downloads myfile.txt new_folder
- rmdir:删除空目录
$ rmdir new_folder
$ ls
Desktop Documents Downloads myfile.txt
- touch:创建空文件或更新文件时间
$ touch note.txt
$ ls
Desktop Documents Downloads myfile.txt note.txt
- cp:拷贝文件或目录
基本语法
cp [选项] 源文件/目录 目标文件/目录
拷贝文件
$ cp myfile.txt copy.txt
$ ls
myfile.txt copy.txt
拷贝目录(必须加-r参数)
$ cp -r docs/ docs_backup/
还有其他扩展功能,后续有时间单出一章
- mv:移动或重命名
$ mv myfile.txt archive.txt
$ ls
archive.txt copy.txt
- rm:删除文件或目录
$ rm copy.txt
$ ls
archive.txt
递归删除目录:删除指定目录及目录下的所有内容
$ rm -r Documents_backup
递归删除一定要慎重使用,一不小心是有可能删库跑路的!!!!(也可能跑不掉(´・ω・`))
2.查看文件内容
- cat:显示整个文件内容
$ cat note.txt
This is a note file.
Line two of the note.
- more:分页向下看(按回车/空格)
$ more long.txt
Line 1
Line 2
...
--More--
- less:上下翻页(q退出)
$ less long.txt
- head:查看前几行
$ head note.txt
This is a note file.
Line two of the note.
- tail:查看末尾命令
查看最后十行(默认)
$ tail logfile.txt
查看最后 n 行(如 20 行)
$ tail -n 20 logfile.txt
实时查看文件更新(非常适合日志监控),终止查看按 Ctrl + C
$ tail -f /var/log/syslog
配合多个文件同时监控
$ tail -f file1.log file2.log
3.查看与搜索命令
- grep:文本匹配搜索
$ grep "note" note.txt
This is a note file.
- find:查找文件
$ find . -name "*.txt"
./note.txt
./Documents/todo.txt
which:命令所在路径
$ which python
/usr/bin/python
后面还有,先缓一缓