一、文件和目录操作
# mkdir创建空目录mkdir demo# 同时创建2个目录mkdir demo1 demo2# 创建目录树mkdir -p /tmp/dir1/dir2# 删除空目录rmdir demo
# 创建一个文件touch aaaa.txt# 删除一个文件rm -f aaaa.txt# 移动文件件到其父目录下的其他文件夹下mv aaaa.txt ./testing# 重命名文件mv aaa.txt bbb.txt# 复制文件cp file1 file2# 复制某目录下的所有文件至当前目录cp dir/* .# 复制目录cp -a dir1 dir2
# 创建指向文件/目录的软连接ln -s 源文件 目标文件# 创建指向文件/目录的物理连接ln file1 lnk1# 从根目录开始搜索文件/目录find / -name file1# 搜索user1的文件/目录find / -user user1# 在目录/dir中搜带有.bin后缀的文件find /dir -name *.bin# 快速定位文件:搜索etc目录下所有my开头的文件locate /etc/my# 修改文件权限chmod 777 test.txt
二、文件查看和处理
# 查看文件内容cat file1# 查看文件内容并显示行数cat -n file1# 从最后一行开始反看文件内容tac file1# 查看一个长文件的内容more file1# 查看文件前两行head -2 file1# 查看文件后两行tail -2 file1# 实时查看添加到文件中的内容tail -f /long/msg# 在文件hello.txt中查找关键字codesheepgrep codesheep hello.txt# 在文件hello.txt中查找以sheep开头的内容grep ^sheep hello.txt# 统计行wc ./demo/22.txt
# 将hello.txt文件中的s1替换成s2sed 's/s1/s2/g' hello.txt# 从hello.txt文件中删除所有空白行sed '/^$/d' hello.txt# 从hello.txt文件中删除所有注释和空白行sed '/*#/d;/^$/d' hello.txt# 从文件hello.txt中排除第一行sed -e '1d' hello.txt# 查看只包含关键字s1的行sed -n '/s1/p' hello.txt# 删除每一行最后的空白字符sed -e 's/*$//' hello.txt# 从文档中只删除词汇s1并保留剩余部分sed -e 's/s1//g' hello.txt
三、网络和进程
# 查看网络接口属性ifconfig # 查看路由表route -n# 查看所有监听端口netstat -lntp# 查看已经建立的TCP连接netstat -antp# 查看TCP/UDP的状态信息netstat -lutp# 启用eth0网络设备ifup eth0# 禁用eth0网络设备ifdown eth0# 查看iptables规则iptables -L# 查看主机名hostname# 查看所有进程ps -ef# 结束进程kill -s namekill -s pid# 实时显示进程状态top
四、打包和解压
# 压缩至zip包zip xxx.zip file# 将多个文件+目录压成zip包zip -r xxx.zip file1 dir1# 解压zip包unzip xxx.zip# 创建非压缩tar包tar -cvf xxx.tar file# 将多个文件+目录打tar包tar -cvf xxx.tar file1 file2 dir1# 查看tar包的内容tar -tf xxx.tar# 解压tar包tar -xvf xxx.tar# 将tar包解压至指定目录tar -xvf xxx.tar -C /dir# 创建bz2压缩包tar -cvfj xxx.tar.bz2 dir# 解压bz2压缩包tar -jxvf xxx.tar.bz2# 创建gzip压缩包tar -cvfz xxx.tar.gz dir# 解压gzip压缩包tar -zxvf xxx.tar.gz# 解压bz2压缩包bunzip2 xxx.bz2# 压缩文件bzip2 filename# 解压gzip压缩包gunzip xxx.gz# 压缩文件gzip filename
五、常用系统服务命令
# 列出系统服务chkconfig --list# 查看某个服务service <服务名> status# 启动某个服务service <服务名> start# 终止某个服务service <服务名> stop# 重启某个服务service <服务名> restart# 查看某个服务systemctl status <服务名># 启动某个服务systemctl start <服务名># 终止某个服务systemctl stop <服务名># 重启某个服务systemctl restart <服务名># 开启自启动systemctl enable <服务名># 关闭自启动systemctl disable <服务名>