Linux基础自学记录四2
2012-02-29 21:31
477 查看
第3讲.Linux常用命令
Linux文件命名规则、文件处理命令、权限管理命令、文件搜索命令、帮助命令、压缩解压命令、网络通信命令、关机重启命令、Shell应用技巧
三、权限管理命令
1[/b]、[/b]chmod[/b]([/b]change the permissions mode of a file,/bin/chmod[/b]),[/b]
chmod [{ugo}{+-=}{rwx}] [[/b]文件或目录[/b]][/b]或[/b]
chmod [mode=421] [[/b]文件或目录[/b]][/b]
用root创建目录premission及文件filetest,改变filetest为777时,blank不能删除filetest;当目录premission改为777,filetest改回644时,blank(普通用户)是可以删除有w权限目录下的所有文件:
[root@localhost blank]# chmod 777 permission/filetest
[root@localhost blank]# ll -d permission permission/filetest
drwxr-xr-x 2 root root 4096 02-28 20:31 permission //目录对其他人只有rx权限
-rwxrwxrwx 1 root root 0 02-28 20:31 permission/filetest //文件已改为全权限
[root@localhost blank]# su blank
[blank@localhost ~]$ rm permission/file
[root@localhost blank]# su blank
[blank@localhost ~]$ export LANG= //切换为英文显示(防乱码)
[blank@localhost ~]$ rm permission/filetest
rm: cannot remove `permission/filetest': Permission denied //因目录无w权限,不能删除其中文件
[root@localhost blank]# chmod 777 permission
[root@localhost blank]# chmod 644 permission/filetest
[root@localhost blank]# ll -d permission permission/filetest
drwxrwxrwx 2 root root 4096 02-28 20:31 permission //改变目录为777全权限
-rw-r--r-- 1 root root 0 02-28 20:31 permission/filetest //改变文件回644,其他人只有读权限
[root@localhost blank]# su blank
[blank@localhost ~]$ export LANG=
[blank@localhost ~]$ rm permission/filetest
rm: remove write-protected regular empty file `permission/filetest'? y
[blank@localhost ~]$ ll -d permission permission/filetest
ls: permission/filetest: No such file or directory //文件已删除
drwxrwxrwx 2 root root 4096 Feb 28 20:57 permission
文件目录权限总结
2[/b]、[/b]chown[/b]([/b]change file ownership,/bin/chown[/b]),[/b]chown [[/b]用户[/b]] [[/b]文件或目录[/b]][/b],[/b]
[/b]chown nobody file1改变文件file1的所有者为nobody(类似Windows下的Guest)
3[/b]、[/b]chgrp[/b]([/b]change file group ownership,/bin/chgrp[/b]),[/b]chgrp [[/b]用户[/b]] [[/b]文件或目录[/b]]
[/b]chgrp adm file1 (改变file1的所属组为adm)
4[/b]、[/b]umask[/b]([/b]the user file-creation mask[/b]),[/b]umask [-S][/b]或[/b]umask 0027[/b]改变当前用户的默认创建权限(不建议用)[/b]
[/b][root@localhost blank]# umask
0022 //0-特殊权限位;022-用户权限位,权限掩码位;777减022得root的默认权限为755。
[root@localhost blank]# umask –S //有的系统没有这种命令
u=rwx,g=rx,o=rx
[root@localhost blank]# mkdir umaskdir
[root@localhost blank]# touch umaskfile
[root@localhost blank]# ll -d umaskdir umaskfile
drwxr-xr-x 2 root root 4096 02-28 21:07 umaskdir //目录权限755
-rw-r--r-- 1 root root 0 02-28 21:07 umaskfile //文件权限644
linux权限规则:缺省创建的文件不能授予可执行x权限 (644),root创建目录755,文件644
四、文件搜索命令
1[/b]、[/b]which[/b]([/b]/usr/bin/which[/b]),[/b]which [[/b]命令名称[/b]][/b],[/b]$which ls[/b]显示[/b]ls[/b]的别名及位置;[/b]$whereis ls[/b]可显示命令帮助文件目录[/b]
2[/b]、[/b]find[/b]([/b]/usr/bin/find[/b]),[/b]find [[/b]范围目录[/b]] [-[/b]条件[/b]][/b],[/b]查找时尽量用目录和条件缩小范围,[/b]-name [/b]文件名、[/b]-size [/b]文件大小、[/b]-user [/b]所有者、[/b]-type[/b]文件类型、[/b]-[/b]时间、[/b]-inum [/b]数字,可用[/b]-a,-o,-exec,-ok[/b]等[/b]
$find /etc -name init[/b] //[/b]在[/b]/etc[/b]目录中查找文件[/b]init[/b]
//-name 文件名,文件名不确定时可加*(匹配任意字符)或?(匹配单个字符),当要花很多时间查找很多文件时还可在后面加> file1(将查找结果保存到file1里):
#find /etc -name init* > test [/b]//[/b]查找所有[/b]init[/b]开头的文件并存入[/b]test[/b]文件中[/b]
[/b]#more test
/etc/init.d
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/rc.d/init.d
/etc/selinux/targeted/contexts/initrc_context
/etc/inittab
/etc/initlog.conf
$find / -size +204800[/b] //[/b]在根目录中查找大于[/b]204800*0.5KB=100MB[/b]的文件[/b]
//-size 文件大小,Linux以block数据块为文件单位,默认为512字节=0.5KB;大于+、小于-、等于=(很少用,因为很难确定一个文件大小就是多少数据快)
$find /home –user blank [/b]//[/b]在[/b]/home[/b]目录中查找所有者[/b]blank[/b]的文件[/b]
$find /etc –cmin -5[/b] //[/b]在[/b]/etc[/b]下查找[/b]5[/b]分钟内被修改过属性的文件和目录,在怀疑[/b]hacker[/b]攻击时常用[/b]
//-时间 {–+}数字,时间天用ctime,atime,mtime、分钟用cmin,amin,mmin、-之内,+超过,直接数字表示固定时间值;c(change)文件属性被修改过(所有者、权限);a(access)文件被浏览过;m(modify)文件内容被修改过。例find /etc –mmin -120查找2小时(120minute)之内(-)被修改(m)的文件。
$find /etc –name init* -a –type d [/b] //[/b]查找[/b]/etc[/b]下[/b]init[/b]开头并且类型是目录的[/b]
/etc/rc.d/init.d
//连接符:-a(and逻辑与)、-o(or逻辑或);-type 文件类型:f(二进制文件)、l(软链接文件)、d(目录)
# find /etc -name inittab -o -size +20480 [/b]//[/b]查找文件名为[/b]inittab[/b]或者大于[/b]10M[/b]的文件[/b]
/etc/selinux/targeted/modules/active/base.linked
/etc/selinux/targeted/modules/active/base.pp
/etc/inittab
# find /etc -name inittab -exec ls -l {} \; //[/b]查找[/b]inittab[/b]文件并显示文件信息(对查找的结果执行[/b]ls –l[/b])[/b]
[/b]-rw-r--r-- 1 root root 1670 Dec 25 23:06 /etc/inittab
//find … -{exec,ok} 命令 {} \;这是固定格式;{} \;的含义为{}查找的结果、\转义符(符号命令使用本身的含义)、;为结束符;-{exec,ok}exec直接执行、ok执行前询问
\转义符可用#which rm查看alias rm='rm -i',表示删除时加了提示信息,如果用#/rm file1将不提示直接删除
$ ls –i //[/b]当系统中出现怪异的文件时(有可能是这样创建的[/b]$touch “a b”[/b]或[/b]$touch -- -abc[/b])[/b]
[/b]652702 -abc 652701 a b 652700 test
$ find . -inum 652701 -ok rm {} \; //[/b]可用“[/b]. –inum [/b]数字“来通过[/b]i[/b]节点查找异常文件,并删除[/b]
[/b]< rm ... ./a b > ? y
实例:
当[/b]blank[/b]离职了,可用此查找所有[/b]/ home[/b]目录下[/b]blank[/b]的文件及目录,并强型删除。[/b]
$ find /home -user blank -exec rm -rf {} \;
$ find /home -user blank -ok rm -rf {} \; //[/b]删除询问[/b]
[/b]< rm ... /home/blank > ? n
< rm ... /home/blank/.mozilla > ? n
< rm ... /home/blank/.bashrc > ? n
< rm ... /home/blank/test > ? n
[/b]公司[/b]FTP[/b]服务器常出现磁盘空间不够,现在可以用[/b]find[/b]来查找大的无用文件了。[/b]
[/b][root@gsftp ~]# df -h
Filesystem 容量 已用 可用 已用% 挂载点
/dev/hda2 17G 2.6G 14G 17% /
/dev/hda1 99M 8.3M 86M 9% /boot
none 252M 0 252M 0% /dev/shm
/dev/hda5 4.9G 4.3G 301M 94% /home
[root@gsftp ~]# find /home -size +204800[/b] -ok rm -rf {} \;[/b]
< rm ... /home/ftpoffice/数据部/wyz/银行从业/个人理财.rar > ? y
< rm ... /home/ftpoffice/数据部/wyz/银行从业/公共基础.rar > ? y
< rm ... /home/ftpoffice/数据部/wyz/银行从业/风险管理.rar > ? y
< rm ... /home/ftpoffice/人事部/新建文件夹 (2).rar > ? y
< rm ... /home/ftpoffice/公用文件夹/工程部公共文件夹/Office2003SP3-KB923618-FullFile-CHS.exe > ? y
< rm ... /home/ftpoffice/工程部/照片 142.MOV > ? y
3[/b]、[/b]locate[/b]([/b]list files in databases,/usr/bin/locate[/b]),[/b]locate [[/b]文件关键字[/b]][/b],[/b]在系统文件数据库中查找文件或目录,速度很快,但当新文件没有及时进入系统数据库时会查找不到,所以一般配合[/b]updatedb[/b]([/b]update the slocate database,/usr/bin/updatedb,[/b]建立整个系统目录文件的数据库)来查找[/b]
# locate blank //[/b]当系统没有配置数据库生成时会提示警告[/b]
warning: locate: could not open database: /var/lib/slocate/slocate.db: No such file or directory
warning: You need to run the 'updatedb' command (as root) to create the database.
Please have a look at /etc/updatedb.conf to enable the daily cron job.
4[/b]、[/b]grep[/b]([/b]/bin/grep[/b]),[/b]grep [[/b]指定字符串[/b]] [[/b]文件名[/b]][/b],在文件中查找字符串匹配的行并输出[/b]
[/b]$ grep tftp /etc/services //查找services文件中所有包含tftp行的内容
Linux文件命名规则、文件处理命令、权限管理命令、文件搜索命令、帮助命令、压缩解压命令、网络通信命令、关机重启命令、Shell应用技巧
三、权限管理命令
1[/b]、[/b]chmod[/b]([/b]change the permissions mode of a file,/bin/chmod[/b]),[/b]
chmod [{ugo}{+-=}{rwx}] [[/b]文件或目录[/b]][/b]或[/b]
chmod [mode=421] [[/b]文件或目录[/b]][/b]
用root创建目录premission及文件filetest,改变filetest为777时,blank不能删除filetest;当目录premission改为777,filetest改回644时,blank(普通用户)是可以删除有w权限目录下的所有文件:
[root@localhost blank]# chmod 777 permission/filetest
[root@localhost blank]# ll -d permission permission/filetest
drwxr-xr-x 2 root root 4096 02-28 20:31 permission //目录对其他人只有rx权限
-rwxrwxrwx 1 root root 0 02-28 20:31 permission/filetest //文件已改为全权限
[root@localhost blank]# su blank
[blank@localhost ~]$ rm permission/file
[root@localhost blank]# su blank
[blank@localhost ~]$ export LANG= //切换为英文显示(防乱码)
[blank@localhost ~]$ rm permission/filetest
rm: cannot remove `permission/filetest': Permission denied //因目录无w权限,不能删除其中文件
[root@localhost blank]# chmod 777 permission
[root@localhost blank]# chmod 644 permission/filetest
[root@localhost blank]# ll -d permission permission/filetest
drwxrwxrwx 2 root root 4096 02-28 20:31 permission //改变目录为777全权限
-rw-r--r-- 1 root root 0 02-28 20:31 permission/filetest //改变文件回644,其他人只有读权限
[root@localhost blank]# su blank
[blank@localhost ~]$ export LANG=
[blank@localhost ~]$ rm permission/filetest
rm: remove write-protected regular empty file `permission/filetest'? y
[blank@localhost ~]$ ll -d permission permission/filetest
ls: permission/filetest: No such file or directory //文件已删除
drwxrwxrwx 2 root root 4096 Feb 28 20:57 permission
文件目录权限总结
字符 | 权限 | 对文件的含义 | 对目录的含义 |
r-4 | 读权限 | 可以查看文件内容:cat,more,head,tail | 可以列出目录中的内容:ls |
w-2 | 写权限 | 可以修改文件内容:echo,vi | 可以在目录中创建、删除文件[/b]:touch,mkdir,rm[/b] |
x-1 | 执行权限 | 可以执行文件:用于脚本,命令 | 可以进入目录:cd |
[/b]chown nobody file1改变文件file1的所有者为nobody(类似Windows下的Guest)
3[/b]、[/b]chgrp[/b]([/b]change file group ownership,/bin/chgrp[/b]),[/b]chgrp [[/b]用户[/b]] [[/b]文件或目录[/b]]
[/b]chgrp adm file1 (改变file1的所属组为adm)
4[/b]、[/b]umask[/b]([/b]the user file-creation mask[/b]),[/b]umask [-S][/b]或[/b]umask 0027[/b]改变当前用户的默认创建权限(不建议用)[/b]
[/b][root@localhost blank]# umask
0022 //0-特殊权限位;022-用户权限位,权限掩码位;777减022得root的默认权限为755。
[root@localhost blank]# umask –S //有的系统没有这种命令
u=rwx,g=rx,o=rx
[root@localhost blank]# mkdir umaskdir
[root@localhost blank]# touch umaskfile
[root@localhost blank]# ll -d umaskdir umaskfile
drwxr-xr-x 2 root root 4096 02-28 21:07 umaskdir //目录权限755
-rw-r--r-- 1 root root 0 02-28 21:07 umaskfile //文件权限644
linux权限规则:缺省创建的文件不能授予可执行x权限 (644),root创建目录755,文件644
四、文件搜索命令
1[/b]、[/b]which[/b]([/b]/usr/bin/which[/b]),[/b]which [[/b]命令名称[/b]][/b],[/b]$which ls[/b]显示[/b]ls[/b]的别名及位置;[/b]$whereis ls[/b]可显示命令帮助文件目录[/b]
2[/b]、[/b]find[/b]([/b]/usr/bin/find[/b]),[/b]find [[/b]范围目录[/b]] [-[/b]条件[/b]][/b],[/b]查找时尽量用目录和条件缩小范围,[/b]-name [/b]文件名、[/b]-size [/b]文件大小、[/b]-user [/b]所有者、[/b]-type[/b]文件类型、[/b]-[/b]时间、[/b]-inum [/b]数字,可用[/b]-a,-o,-exec,-ok[/b]等[/b]
$find /etc -name init[/b] //[/b]在[/b]/etc[/b]目录中查找文件[/b]init[/b]
//-name 文件名,文件名不确定时可加*(匹配任意字符)或?(匹配单个字符),当要花很多时间查找很多文件时还可在后面加> file1(将查找结果保存到file1里):
#find /etc -name init* > test [/b]//[/b]查找所有[/b]init[/b]开头的文件并存入[/b]test[/b]文件中[/b]
[/b]#more test
/etc/init.d
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/rc.d/init.d
/etc/selinux/targeted/contexts/initrc_context
/etc/inittab
/etc/initlog.conf
$find / -size +204800[/b] //[/b]在根目录中查找大于[/b]204800*0.5KB=100MB[/b]的文件[/b]
//-size 文件大小,Linux以block数据块为文件单位,默认为512字节=0.5KB;大于+、小于-、等于=(很少用,因为很难确定一个文件大小就是多少数据快)
$find /home –user blank [/b]//[/b]在[/b]/home[/b]目录中查找所有者[/b]blank[/b]的文件[/b]
$find /etc –cmin -5[/b] //[/b]在[/b]/etc[/b]下查找[/b]5[/b]分钟内被修改过属性的文件和目录,在怀疑[/b]hacker[/b]攻击时常用[/b]
//-时间 {–+}数字,时间天用ctime,atime,mtime、分钟用cmin,amin,mmin、-之内,+超过,直接数字表示固定时间值;c(change)文件属性被修改过(所有者、权限);a(access)文件被浏览过;m(modify)文件内容被修改过。例find /etc –mmin -120查找2小时(120minute)之内(-)被修改(m)的文件。
$find /etc –name init* -a –type d [/b] //[/b]查找[/b]/etc[/b]下[/b]init[/b]开头并且类型是目录的[/b]
/etc/rc.d/init.d
//连接符:-a(and逻辑与)、-o(or逻辑或);-type 文件类型:f(二进制文件)、l(软链接文件)、d(目录)
# find /etc -name inittab -o -size +20480 [/b]//[/b]查找文件名为[/b]inittab[/b]或者大于[/b]10M[/b]的文件[/b]
/etc/selinux/targeted/modules/active/base.linked
/etc/selinux/targeted/modules/active/base.pp
/etc/inittab
# find /etc -name inittab -exec ls -l {} \; //[/b]查找[/b]inittab[/b]文件并显示文件信息(对查找的结果执行[/b]ls –l[/b])[/b]
[/b]-rw-r--r-- 1 root root 1670 Dec 25 23:06 /etc/inittab
//find … -{exec,ok} 命令 {} \;这是固定格式;{} \;的含义为{}查找的结果、\转义符(符号命令使用本身的含义)、;为结束符;-{exec,ok}exec直接执行、ok执行前询问
\转义符可用#which rm查看alias rm='rm -i',表示删除时加了提示信息,如果用#/rm file1将不提示直接删除
$ ls –i //[/b]当系统中出现怪异的文件时(有可能是这样创建的[/b]$touch “a b”[/b]或[/b]$touch -- -abc[/b])[/b]
[/b]652702 -abc 652701 a b 652700 test
$ find . -inum 652701 -ok rm {} \; //[/b]可用“[/b]. –inum [/b]数字“来通过[/b]i[/b]节点查找异常文件,并删除[/b]
[/b]< rm ... ./a b > ? y
实例:
当[/b]blank[/b]离职了,可用此查找所有[/b]/ home[/b]目录下[/b]blank[/b]的文件及目录,并强型删除。[/b]
$ find /home -user blank -exec rm -rf {} \;
$ find /home -user blank -ok rm -rf {} \; //[/b]删除询问[/b]
[/b]< rm ... /home/blank > ? n
< rm ... /home/blank/.mozilla > ? n
< rm ... /home/blank/.bashrc > ? n
< rm ... /home/blank/test > ? n
[/b]公司[/b]FTP[/b]服务器常出现磁盘空间不够,现在可以用[/b]find[/b]来查找大的无用文件了。[/b]
[/b][root@gsftp ~]# df -h
Filesystem 容量 已用 可用 已用% 挂载点
/dev/hda2 17G 2.6G 14G 17% /
/dev/hda1 99M 8.3M 86M 9% /boot
none 252M 0 252M 0% /dev/shm
/dev/hda5 4.9G 4.3G 301M 94% /home
[root@gsftp ~]# find /home -size +204800[/b] -ok rm -rf {} \;[/b]
< rm ... /home/ftpoffice/数据部/wyz/银行从业/个人理财.rar > ? y
< rm ... /home/ftpoffice/数据部/wyz/银行从业/公共基础.rar > ? y
< rm ... /home/ftpoffice/数据部/wyz/银行从业/风险管理.rar > ? y
< rm ... /home/ftpoffice/人事部/新建文件夹 (2).rar > ? y
< rm ... /home/ftpoffice/公用文件夹/工程部公共文件夹/Office2003SP3-KB923618-FullFile-CHS.exe > ? y
< rm ... /home/ftpoffice/工程部/照片 142.MOV > ? y
3[/b]、[/b]locate[/b]([/b]list files in databases,/usr/bin/locate[/b]),[/b]locate [[/b]文件关键字[/b]][/b],[/b]在系统文件数据库中查找文件或目录,速度很快,但当新文件没有及时进入系统数据库时会查找不到,所以一般配合[/b]updatedb[/b]([/b]update the slocate database,/usr/bin/updatedb,[/b]建立整个系统目录文件的数据库)来查找[/b]
# locate blank //[/b]当系统没有配置数据库生成时会提示警告[/b]
warning: locate: could not open database: /var/lib/slocate/slocate.db: No such file or directory
warning: You need to run the 'updatedb' command (as root) to create the database.
Please have a look at /etc/updatedb.conf to enable the daily cron job.
4[/b]、[/b]grep[/b]([/b]/bin/grep[/b]),[/b]grep [[/b]指定字符串[/b]] [[/b]文件名[/b]][/b],在文件中查找字符串匹配的行并输出[/b]
[/b]$ grep tftp /etc/services //查找services文件中所有包含tftp行的内容
相关文章推荐
- Linux基础自学记录四1
- Linux基础自学记录一
- Linux基础自学记录三1
- Linux基础自学记录五
- Linux基础自学记录六-引导流程解析1
- Linux基础自学记录三3
- Linux基础自学记录四3
- Linux基础自学记录六-引导流程解析2
- Linux基础自学记录三2
- Linux基础自学记录三4
- Linux基础自学记录二
- Linux基础自学记录三5
- Linux基础自学记录七-软件包管理
- python基础3-1(自学代码记录)
- 和菜鸟一起学linux之DBUS基础学习记录
- DayDayUP_Python自学记录[1]_Python基础知识
- Linux学习记录(17)SSH协议及Linux日志基础
- 自学基础1_linux_2_看懂各类命令格式
- Linux自学笔记:01_Linux基础介绍
- 和菜鸟一起学linux之DBUS基础学习记录