常用Linux命令实例精简版

Jul 23, 2014

1        ls命令 (List DirectoryContents)

列出目录下的内容

# ls                           //列出当前目录文件下的内容

# ls -l                      //long listing fashion, 以详情方式列出当前目录下的内容

# ls –lS                   //列出文件时按照文件大小排序,大文件在前

# ls –ltr                   //将最近修改的文件排在最后

# ls -a                     //列出当前目录下的所有内容,包括隐藏文件夹, Linux中以 “.”开头的就是隐藏文件

# ls -F                     //显示时会在文件夹的前面加上"/"

# ls c*                    //列出名字以'c'字符开头的的文件或文件夹,只列出文件夹 可以用ls c*/

参考http://www.tecmint.com/15-basic-ls-command-examples-in-linux/

 

Note: 可以通过 “# man [命令]”或在命令后加--help选项,查看命令帮助

2        cd命令 (ChangeDirectory)

切换目录

# cd /mydoc1/mydoc2              // 切换到指定目录mydoc1/mydoc2

# cd                                               //切换到当前用户主目录 /home/username

# cd ~                                              //切换到当前用户主目录 /home/username

# cd /                                               //切换到系统根目录

3        sudo命令 (SuperUser Do or Substitute User Do)

用户获得administrator权限来执行命令。例如作为普通用户默认情况下不能安装应用,则可以通过加上sudo来执行该命令。

 

Note:sudo 允许用户借用超级用户的权限,而"su"命令实际上是允许用户以超级用户登录,所以sudosu更安全。不建议使用sudo或者su来处理日常用途,因为它可能导致严重的错误。

参考http://www.linux.com/learn/tutorials/306766:linux-101-introduction-to-sudo

4        pwd命令 (PrintWorking Directory)

 

# pwd // 输出当前所在的目录

5        mkdir 命令 (MakeDirectory)

# mkdir mydoc                                     // 创建目录mydoc

# mkdir –m 777 mydoc                      //-mode, 创建一个权限为777(即权限为rwxrwxrwx)的目录

# mkdir –vp mydoc1/mydoc2         // -verbose 每次创建目录都显示信息

 

//---------------- -parent创建多级目录,如果某个目录不存在, 则创建该目录已经存在不会报错

# mkdir –p mydoc1/mydoc2             // 创建目录mydoc1/mydoc2

# mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}   // 创建一个项目的目录结构

 

注意只有在拥有写权限的目录下才能创建。如果文件夹已经存在会返回错误。

参考:UNIX 高手的 10 个习惯

http://www.ibm.com/developerworks/cn/aix/library/au-badunixhabits.html

6        cp 命令 (Copy)

复制

# cp a.log mydoc1                      // 拷贝a.log到mydoc1子目录中

# cp –r mydoc1 mydoc2           // --recurve 递归拷贝mydoc1及其子目录到mydoc2

7        rm 命令 (Remove)

# rm *.log                                      // 删除所有后缀为log的文件

# rm –r mydoc1                           // 递归删除mydoc1及其子目录的内容

8        mv 命令 (Move)

用来移动文件或改名

# mv test.log test.txt                          // 将test.log重命名为test.txt

# mv -i test.log test.txt                        // 如果test.txt已存在,询问是否覆盖

# mv -f test.log test.txt                        // 如果test.txt已存在,直接覆盖

# mv test1.txt test2.txt mydoc        // 移动test1.txt,test2.txt到mydoc目录

9        chmod命令

更改文件/目录的访问权限

 

查看文件权限时,有10个字符,分表表示所有者,同组用户和其他用户对文件的访问权限,如:

-rw-r--r--

第一个字符’-’表示这是一个文件,如果是’d’则表示是目录

后面9个字符每三个分别表示3种用户的权限:

r read 用数字表示是4

w write 用数字表示是2

x execute 用数字表示是1

- 删除权限,用数字表示是0

 

# chmod 777 test.txt                   // 用数字表示,更改test.txt的权限为可读写

# chmod –R 777 mydoc             // 更改mydoc 文件夹的权限为可读写

# chmod a+w test.txt                  // 为所有用户增加写权限,a all,+w 增加write权限

# chmod a-x test.log                   // 删除所有用户的执行权限

10  tar命令

创建或访问tar格式的archived 文件

 

//----------------------打包

# tar -cvf target.tar test1.txt test2.txt             //将test1和test2打包为target.tar,不压缩,-c create, -v  verbosely showprogress,  -f  后面的参数是archived file 的名字

# tar -zcvf target.tar.gz folder1                          //以gzip格式压缩并打包,-z gzip

 

//----------------------解包

# tar -xvf target.tar                                                // 解包target.tar文件, -xextract

# tar –zxvf target.tar.gz                                        // 解压target.tar.gz

# tar –xvf target.tar –C /home/html               // 解包到/home/html

# tar -zxvf target.tar.gz test.log                         //只解包其中的test.log文件

 

//----------------------查看

# tar –tvf target.tar                                               //查看target.tar的内容, -t list contents

# tar –ztvf target.tar.gz                                        // 查看target.tar.gz的内容

 

参考:http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/

11  find 命令

find是一个很强大的搜索命令。

 

# find . –name test.txt                         // 查找当前目录下的test.txt,“.” 代表当前目录

# find /home –iname test.txt           // 查找home目录下的test.txt 忽略大小写(-iname,ignore name)

 

//------------------------将查找结果输入到文件中

# find /home –iname test.txt >result.txt

 

//------------------------按类型文件类型查找

# find / -type d –name test               // 查找”/”目录下的test文件夹

# find . -type f -name test.php         // 查找当前目录下的test.php文件

 

//-------------------------匹配模式

# find . -type f -name "*.php"          // 查找当前目录下的php文件

find . -type f -not -name"*.html"                                                                              // 非 .html 文件

 

//------------------------- 多个查找条件

# find . -type f \( -name "*.c"-o -name "*.sh" \)                           // *.c and *.sh files, –o代表or

# find . -type f \( -name"*cache" -o -name "*xml" -o -name "*html" \)        // *cache *xml *html

# find ./test -name 'abc*' ! -name '*.php'                                                               // 以abc开头的非php文件

 

 

//------------------------查找空文件/目录

# find /tmp -type f –empty

# find /tmp -type d –empty

 

//------------------------查找隐藏文件

# find /tmp -type f -name ".*"

 

//------------------------查找并执行某动作 find+ exec, 命令中的”{} \;” 是执行exec命令必须的

# find . -type f -name "*.txt"-exec rm -f {} \;                 // 删除后缀为txt的文件

# find / -type f -perm 0777 -print -execchmod 644 {} \;    // 更改777权限的文件为644

# find / -type d -perm 777 -print -execchmod 755 {} \;     // 更改777权限的目录为755

 

// 查找pl文件并列出详情,-d参数指示只列出文件夹的信息,不会列出其内容

# find . -name "*.pl" -exec ls-ld {} \;                      

 

# find . -type f -exec ls -s {} \; | sort-n -r | head -5               // 在当前目录及子目录查找最大的5个文件

# find . -type f -exec ls -s {} \; | sort-n | head -5                   // 在当前目录及子目录查找最小的5个文件

 

//------------------------基于时间的查找

# find / -mtime 50                                                // 查找最近50天修改过的文件

# find / -atime 50                                                  // 查找最近50天访问过的文件

# find / -mtime +50 –mtime -100                     // 查找50天-100天内修改过的文件

# find / -cmin -60                                                   // 查找1小时内改变过的文件

# find / -mmin -60                                                 // 查找1小时内修改过的文件

 

//------------------------基于大小的查找

# find / -size +50M -size -100M                         // 查找大小50-100M之间的文件

 

//------------------------限制查找的深度,对于目录很深时比较有用

# find ./test -maxdepth 2 -name *.php          // 查找深度为2

 

参考:http://www.tecmint.com/35-practical-examples-of-linux-find-command/

 

A collection of Unix/Linux find commandexamples.

http://alvinalexander.com/unix/edu/examples/find.shtml

 

25+examples of Linux find command – search files from command line.

https://www.linux.com/community/blogs/133-general-linux/732993

12  grep 命令(global search regular expression and print out the line)

查找内容

# grep “error” test.log                // 在test.log中查找”error”

# grep–i “error” test.log            //在test.log中查找”error”,忽略大小写

# grep –iw “error” test.log        // -w whole world

# grep –l “error” test1.log test2.log        // 只显示包含查找内容的文件名

 

# grep “error” *.log                     // 在所有log文件中查找”error”

$ grep -r "error" *                        //在当前目录及子目录所有文件中查找”error”

 

# grep “error(*)” test.log           //在test.log中查找匹配error(*)的内容

 

//-------------------------------------------find+ grep

# find . -type f -name "*.java"-exec grep -l “PowUp” {} \; //在java文件中查找”PowUp”

 

参考:http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/

 

 

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值