Linux命令(搜索查看查找类) find 从指定目录查找文件 -name <查询方式> 按照指定的文件名查找模式查找文件 -mtime n 查找n天以前被修改过的所有文件。 -exec<执行指令>:假设find指令的回传值为True,就执行该指令; -size <文件大小> 按照指定的文件大小查找文件
1 2 3 find /root -name "*.pdf" find /root -name "*.pdf" -a -size +1M find /root -mtime +7 find /root -name "*.sql" -exec ls -l {} \; find /root -name "*.pdf" -exec rm -f {} \;
head 与 tail 查看行 头+ 尾- head -n +行数 文件名 (不加-n默认开头十行) tail -n -行数 文件名 (不加-n默认结尾十行)
1 2 3 4 head a.txt tail a.txt head -n +3 a.txt tail -n -3 a.txt
cat 查看内容 cat(英文全拼:concatenate)命令用于连接文件并打印到标准输出设备上。 -n 显示行号包括空行 -b 跳过空白行编号 -s 将所有的连续的多个空行替换为一个空行(压缩成一个空行)
1 2 cat 1.log 2.log cat -sb test.log
more查看大内容 more 分屏查看文件(敲空格查看下一页)
grep 过滤查找 以行为单位进行查找,显示结果为满足的行 -c 统计满足的行数 -v 反转不包含
1 2 3 4 5 6 grep "p" 1.txt grep "P" 1.txt b.txt grep -v "p" 1.txt grep -c "p" 1.txt grep "^n" 1.txt grep "n$" 1.txt
history 查看已经执行过的历史命令
wc 统计文件 1 2 wc -c name.txt wc -l name.txt
du 查看空间 1 2 3 du -h name.txt 人性化方式(带单位) du -s name.txt 只统计每个参数所占用空间总的大小 du -sh /root
管道符号 | (配合命令使用) 管道符 | 表示将前一个命令的处理结果输出传递给后面的命令处理
> 和 >> 指令 > 输出重定向(覆盖写), >> 追加(追加写)
1 2 3 4 5 6 ls -l | grep -c "^d" ls -l | grep "^d" | wc -l history | grep -c "ls" history | grep "ls" | grep "s$" > ttt.txt cat a.txt b.txt >> ttt.txt cat a.txt b.txt | grep "s" >> ttt.txt
解压安装类 zip压缩 unzip解压 常用选项: -r 递归压缩,即压缩目录 -d<目录> 指定解压后文件的存放目录 压缩用相对路径,解压可以用绝对路径
1 2 3 4 5 6 zip 文件名.zip 将要压缩的内容 zip test.zip -r a/b unzip 文件名.zip unzip linux.x64_11gR2_database_1of2.zip -d /opt/app/database/
tar 压缩解压 -z 调用 gzip 程序进行压缩或解压 -c 创建(Create).tar 格式的包文件 -x 解开.tar 格式的包文件 -c </解压时指定释放的目标文件夹 指定目录 -v 输出详细信息(Verbose) -f 表示使用归档文件(一般都要带上表示使用tar,放在最后)
1 2 3 4 5 压缩:tar [选项] ... 归档文件名(压缩包名字) 源文件或目录 解压:tar [选项] ... 归档文件名 [-C 目标目录] tar -zcvf abc123.tar.gz abc.txt 123.txt tar -zcvf abc123.tar.gz m tar -zxvf abc123.tar.gz -C /home/test/
Yum包管理 Yum是一个Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载 RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包。
1 2 3 4 5 6 yum list | grep xxx yum info xxx yum install xxx yum remove xxx yum list installed yum install ntpdate
用户权限类 登录时尽量少用root帐号登录,因为它是系统管理员,最大的权限,避免操作失误。可 以利用普通用户登录,登录后再用 su - 用户名 命令来切换
用户及用户组 类似于角色,系统可以对有共性的多个用户进行统一的管理
1 2 3 4 5 6 7 8 9 10 新增用户组 groupadd xiaoshou 添加用户 useradd xiaomei 添加用户时加上组 useradd -g xiaoshou xiaomei 指定/修改密码 passwd xiaomei 查询用户信息 id xiaomei 切换用户 su - xiaomei 查看当前用户 whoami 修改用户的组 usermod -g 用户组 用户名 删除用户 userdel xiaomei (exit 退出后再删除) 删除组 groupdel xiaoshou
用户和组的相关文件 /etc/passwd 文件 用户(user)的配置文件,记录用户的各种信息 每行的含义:用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell
/etc/group 文件 组(group)的配置文件,记录Linux包含的组的信息 每行含义:组名:口令:组标识号:组内用户列表
权限详解 [ r ]代表可读(read) [ w ]代表可写(write) [ x ]代表可执行(execute)
1 2 drwxr-xr-x. 2 root root 6 5月 16 14:26 a -rw-rw-rw-. 1 wukong shenxian 0 5月 19 16:32 1.txt
10个字符. 第一个字符代表文件类型:文件(-) 目录(d) 链接(l) 接下来每三个字符为一组,共三组,分别代表 文件拥有者、同组的其他用户、不同组的其他用户
可用数字表示为:r=4, w=2, x=1 因此 rwx=4+2+1=7
chmod 修改权限 u 所有者 g 所有组 o 其他人 a 所有人(u、g、o的总和)
1 2 3 4 5 6 7 8 9 chmod u=rwx,g=rx,o=x name.txtchmod o+w name.txt chmod a-x name.txtchmod +x name.txt chmod u=rwx,g=rx,o=x filechmod 751 file chmod -R u+r directory
chown 修改文件所有者 1 2 3 4 chown [-R] 所有者 文件或目录 chown test02 /root/test.txt chown -R 用户:组 /usr/local/mysql chown meimei:students 1.txt
练习 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 cp /etc/passwd /root/ptesthead -n +8 ptestgrep "root" ptest du -sh /hometouch /root/test.logvim test.log i "@directory-client @gnome-apps @gnome-desktop " cat test.log ptest > /root/test1.log cat -n /root/ptest > test2.logfind / -name "*.png" -a -size +1M history | grep -c "ls" grep "root" /root/ptest > test.txt find /root -name "*.txt" find / -size +20M find /root/xxx/yyy/zzz -name "*.txt" -exec rm -f {} \; wc -c ptestcat ptest | wc -c grep -c "^s" ptest tar -zcvf a.tar.gz /root/xxx mkdir ceshitar -zxvf a.tar.gz -C /root/ceshi/ groupadd students useradd -g students kunkun useradd -g students meimei passwd kunkun kunkun123 su - kunkun cd touch student.txtid kunkun > student.txtexit touch 1.txt 2.txtchmod 777 /root/1.txtchmod 754 /root/2.txtchmod g+w /root/2.txtchown kunkun:students /root/1.txtmkdir opentouch /root/open/open{1..100}.log chown -R meimei:students openchmod -R 755 opencat /etc/group /etc/passwd > /root/open.txtuserdel kunkun userdel meimei tail -n -3 /etc/passwd >> /root/open.txtgroupdel students tail -n -3 /etc/group >> /root/open.txtrm -rf /home/kunkun /home/meimeigroupadd shenxian groupadd yaoguai useradd -g yaoguai wukong useradd -g yaoguai bajie useradd -g shenxian shaseng passwd wukong wukong123 passwd bajie bajie123 passwd shaseng shaseng123 su - wukong touch monkey.txtvim monkey.txt i esc :wq su - root chmod -R g=rwx /home/wukongsu - bajie vim /home/wukong/monkey.txt i esc :wq su - shaseng vim /home/wukong/monkey.txt :q su - root usermod -g yaoguai shaseng su - shaseng vim /home/wukong/monkey.txt i esc :wq