一、文件和目录操作

  1. # mkdir创建空目录
  2. mkdir demo
  3. # 同时创建2个目录
  4. mkdir demo1 demo2
  5. # 创建目录树
  6. mkdir -p /tmp/dir1/dir2
  7. # 删除空目录
  8. rmdir demo
  1. # 创建一个文件
  2. touch aaaa.txt
  3. # 删除一个文件
  4. rm -f aaaa.txt
  5. # 移动文件件到其父目录下的其他文件夹下
  6. mv aaaa.txt ./testing
  7. # 重命名文件
  8. mv aaa.txt bbb.txt
  9. # 复制文件
  10. cp file1 file2
  11. # 复制某目录下的所有文件至当前目录
  12. cp dir/* .
  13. # 复制目录
  14. cp -a dir1 dir2
  1. # 创建指向文件/目录的软连接
  2. ln -s 源文件 目标文件
  3. # 创建指向文件/目录的物理连接
  4. ln file1 lnk1
  5. # 从根目录开始搜索文件/目录
  6. find / -name file1
  7. # 搜索user1的文件/目录
  8. find / -user user1
  9. # 在目录/dir中搜带有.bin后缀的文件
  10. find /dir -name *.bin
  11. # 快速定位文件:搜索etc目录下所有my开头的文件
  12. locate /etc/my
  13. # 修改文件权限
  14. chmod 777 test.txt

二、文件查看和处理

  1. # 查看文件内容
  2. cat file1
  3. # 查看文件内容并显示行数
  4. cat -n file1
  5. # 从最后一行开始反看文件内容
  6. tac file1
  7. # 查看一个长文件的内容
  8. more file1
  9. # 查看文件前两行
  10. head -2 file1
  11. # 查看文件后两行
  12. tail -2 file1
  13. # 实时查看添加到文件中的内容
  14. tail -f /long/msg
  15. # 在文件hello.txt中查找关键字codesheep
  16. grep codesheep hello.txt
  17. # 在文件hello.txt中查找以sheep开头的内容
  18. grep ^sheep hello.txt
  19. # 统计行
  20. wc ./demo/22.txt
  1. # 将hello.txt文件中的s1替换成s2
  2. sed 's/s1/s2/g' hello.txt
  3. # 从hello.txt文件中删除所有空白行
  4. sed '/^$/d' hello.txt
  5. # 从hello.txt文件中删除所有注释和空白行
  6. sed '/*#/d;/^$/d' hello.txt
  7. # 从文件hello.txt中排除第一行
  8. sed -e '1d' hello.txt
  9. # 查看只包含关键字s1的行
  10. sed -n '/s1/p' hello.txt
  11. # 删除每一行最后的空白字符
  12. sed -e 's/*$//' hello.txt
  13. # 从文档中只删除词汇s1并保留剩余部分
  14. sed -e 's/s1//g' hello.txt

三、网络和进程

  1. # 查看网络接口属性
  2. ifconfig
  3. # 查看路由表
  4. route -n
  5. # 查看所有监听端口
  6. netstat -lntp
  7. # 查看已经建立的TCP连接
  8. netstat -antp
  9. # 查看TCP/UDP的状态信息
  10. netstat -lutp
  11. # 启用eth0网络设备
  12. ifup eth0
  13. # 禁用eth0网络设备
  14. ifdown eth0
  15. # 查看iptables规则
  16. iptables -L
  17. # 查看主机名
  18. hostname
  19. # 查看所有进程
  20. ps -ef
  21. # 结束进程
  22. kill -s name
  23. kill -s pid
  24. # 实时显示进程状态
  25. top

四、打包和解压

  1. # 压缩至zip包
  2. zip xxx.zip file
  3. # 将多个文件+目录压成zip包
  4. zip -r xxx.zip file1 dir1
  5. # 解压zip包
  6. unzip xxx.zip
  7. # 创建非压缩tar包
  8. tar -cvf xxx.tar file
  9. # 将多个文件+目录打tar包
  10. tar -cvf xxx.tar file1 file2 dir1
  11. # 查看tar包的内容
  12. tar -tf xxx.tar
  13. # 解压tar包
  14. tar -xvf xxx.tar
  15. # 将tar包解压至指定目录
  16. tar -xvf xxx.tar -C /dir
  17. # 创建bz2压缩包
  18. tar -cvfj xxx.tar.bz2 dir
  19. # 解压bz2压缩包
  20. tar -jxvf xxx.tar.bz2
  21. # 创建gzip压缩包
  22. tar -cvfz xxx.tar.gz dir
  23. # 解压gzip压缩包
  24. tar -zxvf xxx.tar.gz
  25. # 解压bz2压缩包
  26. bunzip2 xxx.bz2
  27. # 压缩文件
  28. bzip2 filename
  29. # 解压gzip压缩包
  30. gunzip xxx.gz
  31. # 压缩文件
  32. gzip filename

五、常用系统服务命令

  1. # 列出系统服务
  2. chkconfig --list
  3. # 查看某个服务
  4. service <服务名> status
  5. # 启动某个服务
  6. service <服务名> start
  7. # 终止某个服务
  8. service <服务名> stop
  9. # 重启某个服务
  10. service <服务名> restart
  11. # 查看某个服务
  12. systemctl status <服务名>
  13. # 启动某个服务
  14. systemctl start <服务名>
  15. # 终止某个服务
  16. systemctl stop <服务名>
  17. # 重启某个服务
  18. systemctl restart <服务名>
  19. # 开启自启动
  20. systemctl enable <服务名>
  21. # 关闭自启动
  22. systemctl disable <服务名>