Linux中的一些常见技巧

批量创建文件并自动按照编号命名

例如创建love1.txt、love2.txt、love3.txt……love20.txt。

# touch love{1..20}.txt

批量产生文件
Linux常见通配符

字符含义
*匹配0个或者多个字符
?匹配任意一个字符
[list]匹配list中的任意一个单一字符
[!list]匹配 除list 中的任意单一字符以外的字符
[c1-c2]匹配 c1-c2 中的任意单一字符 如:[0-9] [a-z]
{string1,string2,……}匹配 sring1 或 string2 (或更多)其一字符串
{c1..c2}匹配 c1-c2 中全部字符 如{1..10}

ManPage中的区段和说明

区段说明
1一般命令
2系统调用
3库函数,涵盖了C标准函数库
4特殊文件(通常是/dev中的设备)和驱动程序
5文件格式和约定
6游戏和屏保
7杂项
8系统管理命令和守护进程

要查看相应区段的内容,就在man后面加上相应区段的数字即可,如:

man 3 printf

这里写图片描述
所有的手册页遵循一个常见的布局,其为通过简单的ASCII文本展示而优化,而这种情况下可能没有任何形式的高亮或字体控制。一般包括以下部分内容:
NAME(名称)
该命令或函数的名称,接着是一行简介。

SYNOPSIS(概要)
对于命令,正式的描述它如何运行,以及需要什么样的命令行参数。对于函数,介绍函数所需的参数,以及哪个头文件包含该函数的定义。

DESCRIPTION(说明)
命令或函数功能的文本描述。

EXAMPLES(示例)
常用的一些示例。

SEE ALSO(参见)
相关命令或函数的列表。

也可能存在其他部分内容,但这些部分没有得到跨手册页的标准化。常见的例子包括:OPTIONS(选项),EXIT STATUS(退出状态),ENVIRONMENT(环境),BUGS(程序漏洞),FILES(文件),AUTHOR(作者),REPORTING BUGS(已知漏洞),HISTORY(历史)和COPYRIGHT(版权)。

通常man手册中的内容很多,你可能不太容易找到你想要的结果,不过幸运的是你可以在man中使用搜索,/你要搜索的关键字,查找到后你可以使用n键切换到下一个关键字所在处,shift+n为上一个关键字所在处。使用Space(空格键)翻页,Enter(回车键)向下滚动一行,或者使用j,k(vim编辑器的移动键)进行向前向后滚动一行。按下h键为显示使用帮助(因为man使用less作为阅读器,实为less工具的帮助),按下q退出。

想要获得更详细的帮助,你还可以使用info命令,不过通常使用man就足够了。如果你知道某个命令的作用,只是想快速查看一些它的某个具体参数的作用,那么你可以使用--help参数,大部分命令都会带有这个参数,如:

# ls --help

用户以及文件权限的管理

who am i命令

# who am i

这里写图片描述
从左至右依次是打开该终端的用户名(要查看当前登录用户的用户名,去掉空格直接使用whoami即可),第二列的pts/1中pts表示伪终端,所谓伪是相对于/dev/tty设备而言的,七个使用[Ctrl]+[Alt]+[F1]~[F7]进行切换的/dev/tty设备么,这是“真终端”,伪终端就是当你在图形用户界面使用/dev/tty7时每打开一个终端就会产生一个伪终端,pts/1后面那个数字就表示打开的伪终端序号。第三列则表示当前伪终端的启动时间。
真终端
这里写图片描述
who命令
-a 打印能打印的全部
-d 打印死掉的进程
-m 同am i,mom likes
-q 打印当前登录用户数及用户名
-u 打印当前登录用户登录信息
-r 打印运行等级
退出登录可以使用exit命令或者Ctrl+D

查看用户所属的组名
1.groups命令

# groups abc

这里写图片描述
其中冒号之前表示用户,后面表示该用户所属的用户组。
2.查看/etc/group文件

# cat /etc/group|grep -E "root|abc"

这里写图片描述
/etc/group文件的说明:
group_name:password:GID:user_list
其中password字段为一个’x’并不是说密码就是它,只是表示密码不可见而已。

将用户添加到特定的组中:
例如:将abc用户加入到root组中

# usermod -G root abc

这里写图片描述
删除用户:

userdel abc -r

-r参数表示删除家目录和邮件池。

ls命令
-A 列出除了 . 及 .. 以外的任何项目

文件和目录管理

tree命令显示文件系统的目录结构

# tree /

新建文件
1. 新建空白文件
使用touch命令创建空白文件,关于touch命令,其主要是来更改已有文件的时间戳的(比如,最近访问时间,最近修改时间),但其在不加任何参数的情况下,只指定一个文件名,则可以创建一个为指定文件名的空白文件(不会覆盖已有同名文件),当然你也可以同时指定该文件的时间戳。
2. 创建目录
使用-p参数,同时创建父目录(如果不存在该父目录),如下我们同时创建一个多级目录(这在有时候安装软件,配置安装路径时非常有用)

# mkdir -p father/son/grandson

这里写图片描述
3. 目录的复制

# cp -r a b

这里写图片描述
批量重命名
要实现批量重命名,mv命令就有点力不从心了,我们可以使用一个看起来更专业的命令rename来实现。不过它是要用perl正则表达式来作为参数。

# 现实用通配符批量创建5个文件
$ touch file{1..5}.txt
# 批量将这5个后缀为.txt的文本文件重命名为以.c为后缀的文件
$ rename 's/\.txt/\.c/' *.txt
# 批量将这5个文件,文件名改为大写
$ rename 'y/a-z/A-Z/' *.c

查看文件内容:
cat用于正序显示;tac用于逆序显示。常用-n参数来显示行号。但是使用nl命令就更加专家了!

file命令查看文件类型

# file /bin/ls 

这里写图片描述

环境变量与文件查找

1. Shell变量
使用declare命令声明一个tmp变量:

root@ubuntu:~# declare tmp
root@ubuntu:~# 

使用=号赋值运算符为变量tmp赋值为abc

root@ubuntu:~# tmp=abc
root@ubuntu:~# 

读取变量的值,使用echo命令和$符号$符号用于表示引用一个变量的值,初学者经常会忘记输入)。

root@ubuntu:~# echo $tmp
abc
root@ubuntu:~#

这里写图片描述
关于变量的命名规则和C语言一样:变量只能包含字母、数字、下划线,并且不能以数字开头!

2. 环境变量
环境变量就是作用域比自定义变量要大,如shell的环境变量作用于自身和它的子进程。在所有的Unix和类Unix系统中,每个进程都有其各自的环境变量设置,且默认情况下,当一个进程被创建时,处理创建过程中明确指定的话,它将继承其父进程的绝大部分环境设置。shell程序也作为一个进程运行在操作系统之上,而我们在shell中运行的大部分命令都将以shell的子进程的方式运行。
这里写图片描述

通常我们会涉及到的环境变量有三种:

  • 当前shell进程私有用户自定义变量,如上面我们创建的tmp变量,只在当前shell中有效。
  • shell本身内建的变量。
  • 从自定义变量导出的环境变量。
    也有三个与上述三种环境变量相关的命令,setenvexport。这三个命令很相似,都可以用于打印相关环境变量,区别在于涉及的是不同范围的环境变量,详见下表:
命令说明
set显示当前shell所有环境变量,包括其内建环境变量(与shell外观等相关),用户自定义变量及导出的环境变量
env显示与当前用户相关的环境变量,还可以让命令在指定环境中运行
export显示从shell中导出成环境变量的变量,也能通过它将自定义变量导出为环境变量

这里写图片描述
可以更直观的使用vimdiff工具比较一下它们之间的差别:

root@ubuntu:~# tmp=shiyanlou
root@ubuntu:~# export tmp_env=shiyanlou
root@ubuntu:~# env|sort>env.txt
root@ubuntu:~# export|sort>export.txt
root@ubuntu:~# set|sort>set.txt

上述操作将命令输出通过管道|使用sort命令排序,再重定向到对象文本文件中:

root@ubuntu:~# vimdiff env.txt export.txt set.txt 

这里写图片描述
关于环境变量,可以简单的理解成在当前进程的子进程是否有效,有效则为环境变量,否则不是(有些人也将所有变量统称为环境变量,只是以全局环境变量和局部环境变量进行区分,我们只要理解它们的实质区别即可)。我们这里用export命令来体会一下,先在shell中设置一个变量temp=shiyanlou,然后再新创建一个子shell查看temp变量的值:
这里写图片描述
注意:为了与普通变量区分,通常我们习惯将环境变量名设为大写
3. 命令的查找路径与顺序
我们在shell中输入一个命令,shell是怎么知道在哪去找到这个命令然后执行的呢。这是通过环境变量PATH来进行搜索的,熟悉windows的用户可能知道windows中的也是有这么一个path环境变量。这个PATH里面就保存了shell中执行的命令的搜索路径。

root@ubuntu:~# echo $PATH

默认情况下会看到以下输出:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

4. 添加 自定义环境变量
例如在/root/mybin目录下有一个hello的可执行程序,如果我们需要在其他的地方也能够执行这个命令就需要将/root/mybin目录加入到环境变量。

root@ubuntu:~# PATH=$PATH:/root/mybin/
root@ubuntu:~# hello
Hello World!

注意这里一定要使用绝对路径。这样还并没有很好的解决问题,因为我给PATH环境变量追加了一个路径,它也只是在当前shell有效,我一旦退出终端,再打开就会发现又失效了。有没有方法让添加的环境变量全局有效又或者每次启动shell时自动执行上面添加自定义路径到PATH的命令了。
编辑/etc/environment文件加入自定义目录并执行source /etc/environment(使立即生效,该命令也可以使用. /代替【圆点和斜杠之间有一个空格】)

root@ubuntu:~# vim /etc/environment 
root@ubuntu:~# source /etc/environment 

变量修改:

变量设置方式说明
${变量名#匹配字串}从头向后开始匹配,删除符合匹配字串的最短数据
${变量名##匹配字串}从头向后开始匹配,删除符合匹配字串的最长数据
${变量名%匹配字串}从尾向前开始匹配,删除符合匹配字串的最短数据
${变量名%%匹配字串}从尾向前开始匹配,删除符合匹配字串的最长数据
${变量名/旧的字串/新的字串}将符合旧字串的第一个字串替换为新的字串
${变量名//旧的字串/新的字串}将符合旧字串的全部字串替换为新的字串
root@ubuntu:~# echo $path 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/root/mybin
root@ubuntu:~# path=${path%/root/mybin}
root@ubuntu:~# echo $path
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:
root@ubuntu:~#

删除环境变量:

root@ubuntu:~# unset tmp

5. 文件查找
这里写图片描述

root@ubuntu:~# whereis who
who: /usr/bin/who /usr/share/man/man1/who.1.gz
root@ubuntu:~# 

查找/etc目录下所有以sh开头的文件

root@ubuntu:~# #更新数据库
root@ubuntu:~# updatedb
root@ubuntu:~# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
root@ubuntu:~# 

查找/usr/share/下所有jpg文件:

root@ubuntu:~# locate /usr/share/*.jpg
/usr/share/cups/doc-root/images/smiley.jpg
/usr/share/doc/libsane/umax/sane-logo.jpg
/usr/share/doc/libsane/umax/sane-umax-advanced.jpg
/usr/share/doc/libsane/umax/sane-umax-histogram.jpg
/usr/share/doc/libsane/umax/sane-umax-standard.jpg
/usr/share/doc/libsane/umax/sane-umax-text.jpg
/usr/share/doc/libsane/umax/sane-umax-text2.jpg
/usr/share/doc/libsane/umax/sane-umax-text4.jpg
/usr/share/doc/libsane/umax/sane-umax.jpg
/usr/share/tomcat6-examples/examples/jsp/jsp2/jspx/textRotate.jpg
root@ubuntu:~# 
root@ubuntu:~# which ls 
/bin/ls
root@ubuntu:~# 

在指定目录下搜索指定文件:

root@ubuntu:~# find /etc/ -name passwd     
/etc/cron.daily/passwd
/etc/init.d/passwd
/etc/pam.d/passwd
/etc/passwd
root@ubuntu:~# 

注意find命令的路径是作为第一个参数的, 基本命令格式为 find [path] [option] [action]
与时间相关的参数:
-atime 最后访问时间
-ctime 创建时间
-mtime 最后修改时间

列出home目录中,当天(24小时之内)有改动的文件:

root@ubuntu:~# find ~ -mtime 0
/root
/root/.bashrc
/root/hello.c
/root/mybin
/root/mybin/hello
/root/env.txt
/root/.viminfo
/root/set.txt
/root/.bash_history
/root/export.txt
root@ubuntu:~# 

Linux实现黑客帝国数字雨:

 sudo apt-get update;sudo apt-get install cmatrix

这里写图片描述

root@ubuntu:~# sudo apt-get install libaa-bin
root@ubuntu:~# aafire

这里写图片描述
彩色的版本:

root@ubuntu:~# apt-get install caca-utils
root@ubuntu:~# cacademo
root@ubuntu:~# cacafire
root@ubuntu:~# cacaview <pic_file>

这里写图片描述
小蜜蜂游戏:

root@ubuntu:~# apt-get install ninvaders
root@ubuntu:~# ninvaders

这里写图片描述

文件打包和压缩

常见的压缩格式

文件后缀名说明
*.zipzip程序打包压缩的文件
*.rarrar程序压缩的文件
*.7zbzip2程序压缩的文件
*.tartar程序打包,未压缩的文件
*.gzgzip程序(GNU zip)压缩的文件
*.xzxz程序压缩的文件
*.bz2bzip2程序压缩的文件
*.tar.gztar打包,gzip程序压缩的文件
*.tar.xztar打包,xz程序压缩的文件
*tar.bz2tar打包,bzip2程序压缩的文件
*.tar.7ztar打包,7z程序压缩的文件

1. zip打包压缩命令

root@ubuntu:~# sudo apt-get install zip
root@ubuntu:~# zip -rqo root.zip ~
root@ubuntu:~# du -h root.zip 
52K root.zip
root@ubuntu:~# 

上述命令将/root目录打包成一个文件,并查看了打包后文件的大小和类型。-r参数表示递归打包包含子目录的全部内容,-q参数表示为安静模式,即不向屏幕输出信息,-o,表示输出文件,需在其后紧跟打包输出文件名。后面使用du命令查看打包后文件的大小。

设置压缩级别为9和1(9最大,1最小),重新打包

root@ubuntu:~# zip -r -9 -qo root_9.zip ~ -x ~/*.zip
root@ubuntu:~# zip -r -1 -qo root_1.zip ~ -x ~/*.zip

最后那个-x是为了排除我们上一次创建的zip文件,否则又会被打包进这一次的压缩文件中,不过注意:这里只能使用绝对路径,否则不起作用。

然后我们再用du命令分别查看默认压缩级别、最低、最高压缩级别及未压缩的文件的大小

root@ubuntu:~# du -h -d 0 *.zip ~ | sort
280K    /root
52K root.zip
52K root_9.zip
60K root_1.zip
root@ubuntu:~# 

创建加密zip文件使用-e参数:

root@ubuntu:~# zip -erqo root_encryption.zip ~
Enter password: 
Verify password: 
root@ubuntu:~#

注意: 关于zip命令,还有一点希望你注意,因为windows系统与Linux/Unix在文本文件格式上的一些兼容问题,比如换行符(为不可见字符),在windows为CR+LF(Carriage-Return+Line-Feed:回车加换行),而在Linux/Unix上为LF(换行),所以如果在不加处理的情况下,在linux上编辑的文本,在windows系统上打开可能看起来是没有换行的。如果你想让你在linux创建的zip压缩文件在windows上解压后没有任何问题,使用-l参数。
2. 使用unzip加压zip文件
将root.zip解压到当前目录:

root@ubuntu:~# unzip root.zip 
Archive:  root.zip
   creating: root/
  inflating: root/.bashrc            
   creating: root/.cache/
 extracting: root/.cache/motd.legal-displayed  
   creating: root/.ssh/
  inflating: root/.ssh/id_rsa.pub    
  inflating: root/.ssh/id_rsa        
 extracting: root/hello.c            
 extracting: root/a12                
  inflating: root/.profile           
 extracting: root/a1                 
   creating: root/mybin/
  inflating: root/mybin/hello        
  inflating: root/env.txt            
  inflating: root/.viminfo           
  inflating: root/set.txt            
  inflating: root/.bash_history      
   creating: root/.aptitude/
 extracting: root/.aptitude/config   
 extracting: root/a                  
   creating: root/.vim/
  inflating: root/.vim/.netrwhist    
  inflating: root/export.txt         
 extracting: root/a.txt              
 extracting: root/b.txt              
root@ubuntu:~#

使用安静模式,将文件解压到指定目录

root@ubuntu:~# unzip -q root.zip -d ziptest
root@ubuntu:~# cd ziptest/;ls
root
root@ubuntu:~/ziptest# ls root/
a  a.txt  a1  a12  b.txt  env.txt  export.txt  hello.c  mybin  set.txt
root@ubuntu:~/ziptest# 

如果你不想解压只想查看压缩包的内容你可以使用-l参数:

root@ubuntu:~# unzip -l root.zip 
Archive:  root.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2015-03-09 17:33   root/
     3107  2015-03-09 16:17   root/.bashrc
        0  2014-10-30 12:17   root/.cache/
        0  2014-10-30 12:17   root/.cache/motd.legal-displayed
        0  2014-11-10 18:22   root/.ssh/
      393  2014-11-10 18:22   root/.ssh/id_rsa.pub
     1675  2014-11-10 18:22   root/.ssh/id_rsa
       75  2015-03-09 15:59   root/hello.c
        0  2014-11-10 01:05   root/a12
      140  2012-04-19 17:15   root/.profile
        0  2014-11-10 01:05   root/a1
        0  2015-03-09 15:59   root/mybin/
     7159  2015-03-09 15:59   root/mybin/hello
     1961  2015-03-09 15:30   root/env.txt
     5942  2015-03-09 16:40   root/.viminfo
   198222  2015-03-09 15:31   root/set.txt
     3339  2015-03-09 17:26   root/.bash_history
        0  2014-10-26 08:05   root/.aptitude/
        0  2014-10-26 08:05   root/.aptitude/config
        0  2014-11-10 01:05   root/a
        0  2014-12-20 22:02   root/.vim/
       99  2014-12-20 22:02   root/.vim/.netrwhist
     2328  2015-03-09 15:30   root/export.txt
        6  2014-11-10 01:55   root/a.txt
        6  2014-11-10 01:56   root/b.txt
---------                     -------
   224452                     25 files
root@ubuntu:~# 

注意: 使用unzip解压文件时我们同样应该注意兼容问题,不过这里我们关心的不再是上面的问题,而是中文编码的问题,通常windows系统上面创建的压缩文件,如果有有包含中文的文档或以中文作为文件名的文件时默认会采用GBK或其它编码,而linux上面默认使用的是UTF-8编码,如果不加任何处理,直接解压的话可能会出现中文乱码的问题(有时候它会自动帮你处理),为了解决这个问题,我们可以在解压时指定编码类型。

root@ubuntu:~# unzip -O GBK 中文压缩文件.zip

使用-O(英文字母,大写o)参数指定编码类型
3. rar打包压缩命令
安装rar工具

root@ubuntu:~# sudo apt-get install rar unrar

从指定文件或目录创建压缩包或添加文件到压缩包

root@ubuntu:~# ls
a  a.txt  a1  a12  b.txt  env.txt  export.txt  hello.c  mybin  set.txt  ziptest
root@ubuntu:~# rar a root.rar .

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Creating archive root.rar

Adding    ./.bashrc                                                   OK 
Adding    ./hello.c                                                   OK 
Adding    ./a12                                                       OK 
Adding    ./.profile                                                  OK 
Adding    ./a1                                                        OK 
Adding    ./env.txt                                                   OK 
Adding    ./.viminfo                                                  OK 
Adding    ./set.txt                                                   OK 
Adding    ./.bash_history                                             OK 
Adding    ./a                                                         OK 
Adding    ./export.txt                                                OK 
Adding    ./a.txt                                                     OK 
Adding    ./b.txt                                                     OK 
Done
root@ubuntu:~# 

上面的命令使用a参数添加一个目录~到一个归档文件中,如果该文件不存在就会自动创建。注意:rar的命令参数没有-,如果加上会报错。

查看而不解压包(l参数):

root@ubuntu:~# rar l root.rar 

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Archive root.rar

 Name             Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 .bashrc          3107     1458  46% 09-03-15 16:17 -rw-r--r-- 8739D947 m3c 2.9
 hello.c            75       85 113% 09-03-15 15:59 -rw-r--r-- 81B79215 m3c 2.9
 a12                 0        8   0% 10-11-14 01:05 -rw-r--r-- 00000000 m3c 2.9
 .profile          140      124  88% 19-04-12 17:15 -rw-r--r-- 28580804 m3c 2.9
 a1                  0        8   0% 10-11-14 01:05 -rw-r--r-- 00000000 m3c 2.9
 env.txt          1961      770  39% 09-03-15 15:30 -rw-r--r-- 41D69BBA m3c 2.9
 .viminfo         5942     1116  18% 09-03-15 16:40 -rw------- 84591984 m3c 2.9
 set.txt        198222    33591  16% 09-03-15 15:31 -rw-r--r-- B86F03B0 m3c 2.9
 .bash_history     3856     1394  36% 09-03-15 18:10 -rw------- F76B2F17 m3c 2.9
 a                   0        8   0% 10-11-14 01:05 -rw-r--r-- 00000000 m3c 2.9
 export.txt       2328      805  34% 09-03-15 15:30 -rw-r--r-- 313F2196 m3c 2.9
 a.txt               6       17 283% 10-11-14 01:55 -rw-r--r-- 31963516 m3c 2.9
 b.txt               6       17 283% 10-11-14 01:56 -rw-r--r-- DD3861A8 m3c 2.9
-------------------------------------------------------------------------------
   13           215643    39401  18%

root@ubuntu:~# 

解压rar文件:
解压到当前目录

root@ubuntu:~# unrar x root.rar 

UNRAR 4.00 beta 3 freeware      Copyright (c) 1993-2010 Alexander Roshal


Extracting from root.rar


.bashrc already exists. Overwrite it ?
[Y]es, [N]o, [A]ll, n[E]ver, [R]ename, [Q]uit A

Extracting  .bashrc                                                   OK 
Extracting  hello.c                                                   OK 
Extracting  a12                                                       OK 
Extracting  .profile                                                  OK 
Extracting  a1                                                        OK 
Extracting  env.txt                                                   OK 
Extracting  .viminfo                                                  OK 
Extracting  set.txt                                                   OK 
Extracting  .bash_history                                             OK 
Extracting  a                                                         OK 
Extracting  export.txt                                                OK 
Extracting  a.txt                                                     OK 
Extracting  b.txt                                                     OK 
All OK
root@ubuntu:~#

解压到指定目录:

root@ubuntu:~# mkdir tmp;unrar e root.rar tmp

UNRAR 4.00 beta 3 freeware      Copyright (c) 1993-2010 Alexander Roshal


Extracting from root.rar

Extracting  tmp/.bashrc                                               OK 
Extracting  tmp/hello.c                                               OK 
Extracting  tmp/a12                                                   OK 
Extracting  tmp/.profile                                              OK 
Extracting  tmp/a1                                                    OK 
Extracting  tmp/env.txt                                               OK 
Extracting  tmp/.viminfo                                              OK 
Extracting  tmp/set.txt                                               OK 
Extracting  tmp/.bash_history                                         OK 
Extracting  tmp/a                                                     OK 
Extracting  tmp/export.txt                                            OK 
Extracting  tmp/a.txt                                                 OK 
Extracting  tmp/b.txt                                                 OK 
All OK
root@ubuntu:~# ls tmp
a  a.txt  a1  a12  b.txt  env.txt  export.txt  hello.c  set.txt

rar命令参数非常多,不过它在linux下面真心不好用啊,所以上面只涉及了一些基本操作。
- tar打包工具
tar原本只是一个打包工具,只是同时还是实现了对7z,gzip,xz,bzip2等工具的支持,这些个压缩工具本身只能实现对文件或目录(单独压缩目录中的文件)的压缩,没有实现对文件的打包压缩,所以我们也无需再单独去学习其他几个工具掌握一个tar就好,而且tar的解压和压缩都是同一个命令,只需参数不同而已,就比较方便使用。
不进行压缩只是进行打包(创建归档文件)和解包的操作。

  • 创建一个tar包(归档文件)
root@ubuntu:/# tar -cf root.tar ~
tar: Removing leading `/' from member names
root@ubuntu:/# ls
bin   dev  home        lib         media  opt   root      run   selinux  sys  usr  vmlinuz
boot  etc  initrd.img  lost+found  mnt    proc  root.tar  sbin  srv      tmp  var

-c表示创建一个tar包文件,-f用于指定创建的文件名,注意文件名必须紧跟在-f参数之后,比如不能写成tar -fc shiyanlou.tar,可以写成tar -f shiyanlou.tar -c ~。你还可以加上-v参数以可视的的方式输出打包的文件。上面会自动去掉表示绝对路径的/,你也可以使用-P保留绝对路径符。
将包解到指定目录(-x和-C参数):

root@ubuntu:~# mkdir tmp;tar -xvf root.tar -C tmp
root/
root/.bashrc
root/.cache/
root/.cache/motd.legal-displayed
root/ziptest/
root/ziptest/root/
root/ziptest/root/.bashrc
root/ziptest/root/.cache/
root/ziptest/root/.cache/motd.legal-displayed
root/ziptest/root/.ssh/
root/ziptest/root/.ssh/id_rsa.pub
root/ziptest/root/.ssh/id_rsa
root/ziptest/root/hello.c
root/ziptest/root/a12
root/ziptest/root/.profile
root/ziptest/root/a1
root/ziptest/root/mybin/
root/ziptest/root/mybin/hello
root/ziptest/root/env.txt
root/ziptest/root/.viminfo
root/ziptest/root/set.txt
root/ziptest/root/.bash_history
root/ziptest/root/.aptitude/
root/ziptest/root/.aptitude/config
root/ziptest/root/a
root/ziptest/root/.vim/
root/ziptest/root/.vim/.netrwhist
root/ziptest/root/export.txt
root/ziptest/root/a.txt
root/ziptest/root/b.txt
root/.ssh/
root/.ssh/id_rsa.pub
root/.ssh/id_rsa
root/hello.c
root/a12
root/.profile
root/a1
root/mybin/
root/mybin/hello
root/env.txt
root/.viminfo
root/set.txt
root/.bash_history
root/.aptitude/
root/.aptitude/config
root/a
root/.vim/
root/.vim/.netrwhist
root/tmp/
root/tmp/.bashrc
root/tmp/hello.c
root/tmp/a12
root/tmp/.profile
root/tmp/a1
root/tmp/env.txt
root/tmp/.viminfo
root/tmp/set.txt
root/tmp/.bash_history
root/tmp/a
root/tmp/export.txt
root/tmp/a.txt
root/tmp/b.txt
root/export.txt
root/a.txt
root/b.txt
root@ubuntu:~# 

查看而不解包(-t参数):

root@ubuntu:~# tar -tf root.tar 
root/
root/.bashrc
root/.cache/
root/.cache/motd.legal-displayed
root/ziptest/
root/ziptest/root/
root/ziptest/root/.bashrc
root/ziptest/root/.cache/
root/ziptest/root/.cache/motd.legal-displayed
root/ziptest/root/.ssh/
root/ziptest/root/.ssh/id_rsa.pub
root/ziptest/root/.ssh/id_rsa
root/ziptest/root/hello.c
root/ziptest/root/a12
root/ziptest/root/.profile
root/ziptest/root/a1
root/ziptest/root/mybin/
root/ziptest/root/mybin/hello
root/ziptest/root/env.txt
root/ziptest/root/.viminfo
root/ziptest/root/set.txt
root/ziptest/root/.bash_history
root/ziptest/root/.aptitude/
root/ziptest/root/.aptitude/config
root/ziptest/root/a
root/ziptest/root/.vim/
root/ziptest/root/.vim/.netrwhist
root/ziptest/root/export.txt
root/ziptest/root/a.txt
root/ziptest/root/b.txt
root/.ssh/
root/.ssh/id_rsa.pub
root/.ssh/id_rsa
root/hello.c
root/a12
root/.profile
root/a1
root/mybin/
root/mybin/hello
root/env.txt
root/.viminfo
root/set.txt
root/.bash_history
root/.aptitude/
root/.aptitude/config
root/a
root/.vim/
root/.vim/.netrwhist
root/tmp/
root/tmp/.bashrc
root/tmp/hello.c
root/tmp/a12
root/tmp/.profile
root/tmp/a1
root/tmp/env.txt
root/tmp/.viminfo
root/tmp/set.txt
root/tmp/.bash_history
root/tmp/a
root/tmp/export.txt
root/tmp/a.txt
root/tmp/b.txt
root/export.txt
root/a.txt
root/b.txt
root@ubuntu:~# 

有时候我们使用tar备份文件当你在其他主机还原时希望保留文件的属性(-p参数)和备份链接指向的源文件而不是链接本身(-h参数)

root@ubuntu:~# tar -cphf etc.tar /etc/

对于创建不同的压缩格式的文件,对于tar来说是相当简单的,你需要的也只是换一个参数而已,这里我就以使用gzip工具创建*.tar.gz文件为例来说明。
- 我们只需要在创建tar文件的基础上添加-z参数,使用gzip来压缩文件

root@ubuntu:~# tar -czf root.tar.gz ~
  • 解压*.tar.gz文件
root@ubuntu:~# tar -xzf root.tar.gz 

现在我们要使用其他的压缩工具创建或解压相应文件只需要更改一个参数即可:

压缩文件格式参数
*.tar.gz-z
*.tar.xz-J
*tar.bz2-j

文件系统操作

1.查看磁盘使用情况(df)

root@ubuntu:~# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda5       18587076 1700196  15919664  10% /
udev              288580       4    288576   1% /dev
tmpfs             118956     428    118528   1% /run
none                5120       0      5120   0% /run/lock
none              297388       0    297388   0% /run/shm
cgroup            297388       0    297388   0% /sys/fs/cgroup
root@ubuntu:~#

通常我们只关心第一行中的内容/dev/sda5【sd-串口硬盘,a第一块硬盘,5表示第5分区】。“1k-blocks”表示以磁盘块大小的方式显示容量,后面为相应的以块大小表示的已用和可用容量。

2.使用du命令查看文件的块的数量
默认同样以blocks的大小展示,加上-h参数,以更易读的方式展示。-d参数指定查看目录的深度。

root@ubuntu:~# #只查看一级目录的信息
root@ubuntu:~# du -hd 0 ~
2.9M    /root
root@ubuntu:~# #查看二级目录的信息
root@ubuntu:~# du -hd 1 ~
4.0K    /root/.cache
284K    /root/ziptest
12K     /root/.ssh
1.4M    /root/root
12K     /root/mybin
4.0K    /root/.aptitude
8.0K    /root/.vim
808K    /root/tmp
2.9M    /root
root@ubuntu:~#

简单的磁盘管理

创建虚拟磁盘(dd命令):
dd命令用于转换和复制文件,不过它的复制不同于cp。硬件的设备驱动(如硬盘)和特殊设备文件(如/dev/zero和/dev/random)都像普通文件一样,只要在各自的驱动程序中实现了对应的功能,dd也可以读取自和/或写入到这些文件。这样,dd也可以用在备份硬件的引导扇区、获取一定数量的随机数据或者空数据等任务中。dd程序也可以在复制时处理数据,例如转换字节序、或在ASCII与EBCDIC编码间互换。
dd的命令行语句与其他的Linux程序不同,因为它的命令行选项格式为选项=值,而不是更标准的–选项 值-选项 值。dd默认从标准输入中读取,并写入到标准输出中,但可以用选项if(input file,输入文件)和of(output file,输出文件)来重定向。
bs(block size)用于指定块大小(缺省单位为Byte,也可为其指定如’K’,’M’,’G’等单位),count用于指定块数量。只读取总共10个字节的数据。

root@ubuntu:~# dd if=/dev/stdin of=/dev/stdout bs=10 count=1
hello
hello
0+1 records in
0+1 records out
6 bytes (6 B) copied, 2.72536 s, 0.0 kB/s
root@ubuntu:~# dd if=/dev/stdin of=test bs=10 count=1
hello
0+1 records in
0+1 records out
6 bytes (6 B) copied, 3.59807 s, 0.0 kB/s
root@ubuntu:~# cat test 
hello
root@ubuntu:~# du -b test 
6   test
root@ubuntu:~# 

dd在拷贝的同时还可以实现数据转换,那下面就举一个简单的例子:将输出的英文字符转换为大写再写入文件。

root@ubuntu:~# dd if=/dev/stdin of=test bs=10 count=1 conv=ucase
welcome
0+1 records in
0+1 records out
8 bytes (8 B) copied, 3.61307 s, 0.0 kB/s
root@ubuntu:~# cat test 
WELCOME
root@ubuntu:~#

使用dd命令创建虚拟镜像文件
1.从/dev/zero设备创建一个容量为256M的空文件

root@ubuntu:~# dd if=/dev/zero of=virtual.img bs=1M count=256 
256+0 records in
256+0 records out
268435456 bytes (268 MB) copied, 5.43987 s, 49.3 MB/s
root@ubuntu:~# du -h virtual.img 
257M    virtual.img
root@ubuntu:~# 

2.然后我们要将这个文件格式化(写入文件系统)
使用mkfs命令格式化磁盘(我们这里是自己创建的虚拟磁盘镜像)
在命令行输入mkfs然后按下Tab键,你可以看到很多个以mkfs为前缀的命令,这些不同的后缀其实就是表示着不同的文件系统,可以用mkfs格式化成的文件系统。
这里写图片描述
使用下面的命令来将我们的虚拟磁盘镜像格式化为ext4文件系统:

root@ubuntu:~# mkfs.ext4 virtual.img 

这里写图片描述
如果你想想知道linux支持哪些文件系统你可以输入
ls lib/modules/$(uname -r)/kernel/fs查看:
这里写图片描述

用户在Linux/Unix的机器上打开一个文件以前,包含该文件的文件系统必须先进行挂载的动作,此时用户要对该文件系统执行mount的指令以进行挂载。通常是使用在USB或其他可移除存储设备上,而根目录则需要始终保持挂载的状态。又因为Linux/Unix文件系统可以对应一个文件而不一定要是硬件设备,所以可以挂载一个包含文件系统的文件到目录树。
使用mount命令查看已经挂载的文件系统:

root@ubuntu:~# mount

这里写图片描述
输出的结果中每一行表示一个设备或虚拟设备,每一行最前面是设备名,然后是on 后面是挂载点,type后面表示文件系统类型,再后面是挂载选项(比如可以在挂载时设定以只读方式挂载等等)。mount命令的一般格式如下:

mount [options] [source] [directory]

常用操作:

mount [-o [操作选项]] [-t 文件系统类型] [-w|--rw|--ro] [文件系统源] [挂载点]

例如挂载我们创建的虚拟磁盘镜像到/mnt目录(可以省略文件系统类型):

root@ubuntu:~# mount -o loop -t ext4 virtual.img /mnt/          
root@ubuntu:~# umount /mnt/ #卸载已挂载的设备
root@ubuntu:~# mount -o loop --ro virtual.img /mnt/ #以read-only方式挂载

/dev/loop设备的说明:在类Unix系统中,/dev/loop(或称vnd (vnode disk)、lofi(循环文件接口))是一种伪设备,这种设备使得文件可以如同块设备一般被访问。 在使用之前,循环设备必须与现存文件系统上的文件相关联。这种关联将提供给用户一个应用程序接口,接口将允许文件视为块特殊文件(参见设备文件系统)使用。因此,如果文件中包含一个完整的文件系统,那么这个文件就能如同磁盘设备一般被挂载。 这种设备文件经常被用于光盘或是磁盘镜像。通过循环挂载来挂载包含文件系统的文件,便使处在这个文件系统中的文件得以被访问。这些文件将出现在挂载点目录。如果挂载目录中本身有文件,这些文件在挂载后将被禁止使用。

使用fdisk为磁盘分区
查看分区表信息:root@ubuntu:~# fdisk -l
这里写图片描述
进入磁盘分区模式:root@ubuntu:~# fdisk virtual.img
这里写图片描述
1. 输入 m 显示所有命令列示。
2. 输入 p 显示硬盘分割情形。
3. 输入 a 设定硬盘启动区。
4. 输入 n 设定新的硬盘分割区。
4.1. 输入 e 硬盘为[延伸]分割区(extend)。
4.2. 输入 p 硬盘为[主要]分割区(primary)。
5. 输入 t 改变硬盘分割区属性。
6. 输入 d 删除硬盘分割区属性。
7. 输入 q 结束不存入硬盘分割区属性。
8. 输入 w 结束并写入硬盘分割区属性。
这里写图片描述

使用losetup命令建立镜像与回环设备的关联

root@ubuntu:~# #如果提示设备忙你也可以使用其它的回环设备,"ls /dev/loop*"参看所有回环设备
root@ubuntu:~# losetup /dev/loop0 virtual.img 
root@ubuntu:~# #解除回环设备
root@ubuntu:~# losetup -d /dev/loop0

命令的执行顺序和管道

我们使用which来查找是否安装某个命令,如果找到就执行该命令,否则什么也不做。

root@ubuntu:~#  which cowsay>/dev/null && echo "cowsay 已安装!"

你如果没有安装cowsay,你可以先执行一次上述命令,你会发现什么也没发生,你再安装好之后你再执行一次上述命令,你也会发现一些惊喜。
我们可以使用$?环境变量获取上一次命令的返回结果(0表示success)。||在这里就是与&&相反的控制效果,当上一条命令执行结果为≠0($?≠0)时则执行它后面的命令。

root@ubuntu:~#which cowsay>/dev/null || echo "cowsay 没有安装!"

结合&&和||完成一些操作(注意顺序不可颠倒):

root@ubuntu:~# which cowsay>/dev/null && echo "已安装!"||echo "未安装!"

这里写图片描述
管道
管道是一种通信机制,它表现在前一个进程的输出是后一个进程的输入。管道又分为匿名管道和具名管道(这里将不会讨论在源程序中使用系统调用创建并使用管道的情况,它与命令行的管道在内核中实际都是采用相同的机制)。我们在使用一些过滤程序时经常会用到的就是匿名管道,在命令行中由|分隔符表示,|在前面的内容中我们已经多次使用到了。具名管道简单的说就是有名字的管道,通常只会在源程序中用到具名管道。

先感受一下管道的魅力,比如下面你想查看一下/etc目录下有哪些文件和目录,于是你使用ls命令来查看:

root@ubuntu:~# ll /etc

你会发现一屏根本显示不玩,这时候你就只有选择使用滚动条或快捷键滚动窗口来查看了,不过这时候如果你会使用管道那情况就会不一样了,比如你可以这样:

root@ubuntu:~# ll /etc/|less

打印/etc/passwd文件中以:为分隔符的第1个字段和第6个字段分别表示用户名和其家目录:

root@ubuntu:~# cut /etc/passwd -d ':' -f 1,6

这里写图片描述
打印`/etc/passwd文件中每一行的前N个字符:

# 前五个(包含第五个)
$ cut /etc/passwd -c -5
# 前五个之后的(包含第五个)
$ cut /etc/passwd -c 5-
# 第五个
$ cut /etc/passwd -c 5
# 2到5之间的(包含第五个)
$ cut /etc/passwd -c 2-5

统计/etc目录下目录的个数:

root@ubuntu:~# ls -d /etc/*/|wc -l
114
root@ubuntu:~#

简单的文本处理

  1. tr
    来删除一段文本信息中的某些文字。或者将其进行转换.
tr [option]...SET1 [SET2]
选项说明
-d删除和set1匹配的字符,注意不是全词匹配也不是按字符顺序匹配
-s去除set1指定的在输入文本中连续并重复的字符
# 删除 "hello shiyanlou" 中所有的'o','l','h'
$ echo 'hello shiyanlou' | tr -d 'olh'
# "hello" 中的ll,去重为一个l
$ echo 'hello' | tr -s 'l'
# 将输入文本,全部转换为大写或小写输出
$ cat /etc/passwd | tr '[:lower:]' '[:upper:]'
# 上面的'[:lower:]' '[:upper:]'你也可以简单的写作'[a-z]' '[A-Z]',当然反过来将大写变小写也是可以的

如换行符windows为CR+LF(\r\n),unix/linux为LF(\n),你使用cat -A 文本 你可以看到文本中包含的不可见特殊字符。linux的\n表现出来就是一个$,而windows/dos的表现为^M。可以使用dos2unix和unix2dos这两个工具,也可以使用下面的命令。

root@ubuntu:~# cat -A linux.txt 
Hello$
root@ubuntu:~# tr '\n' '\r\n' < linux.txt > windows.txt
root@ubuntu:~# cat -A linux.txt windows.txt 
Hello$
Hello^Mroot@ubuntu:~#
  1. col
    col命令可以将Tab换成对等数量的空格建,或反转这个操作。这用来格式化代码很爽啊。
root@ubuntu:~# col [option]
选项说明
-x将Tab转换为空格
-h将空格转换为Tab(默认选项)
# 查看/etc/protocols中的不可见字符,可以看到很多^I,这其实就是Tab键转义成可见字符的符号
$ cat -A /etc/protocols
# 使用col -x将/etc/protocols中的Tab转换为空格,然后再使用cat查看,你发现^I不见了
$ cat /etc/protocols | col -x | cat -A

这里写图片描述

数据流重定向

文件描述符:文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于UNIX、Linux这样的操作系统。
使用tee命令同时重定向多个文件:

root@ubuntu:~# echo "hello"|tee file1 file2 file3
hello
root@ubuntu:~# cat file1 file2 file3
hello
hello
hello
root@ubuntu:~# 

正则表达式

字符描述
\将下一个字符标记为一个特殊字符、或一个原义字符。例如,“n”匹配字符“n”。“\n”匹配一个换行符。序列“\\”匹配“\”而“\(”则匹配“(”。
^匹配输入字符串的开始位置。
$匹配输入字符串的结束位置。
{n}n是一个非负整数。匹配确定的n次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的两个o。
{n,}n是一个非负整数。至少匹配n次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”。
{n,m}m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次。例如,“o{1,3}”将匹配“fooooood”中的前三个o。“o{0,1}”等价于“o?”。请注意在逗号和两个数之间不能有空格。
*匹配前面的子表达式零次或多次。例如,zo*能匹配“z”、“zo”以及“zoo”。*等价于{0,}。
+匹配前面的子表达式一次或多次。例如,“zo+”能匹配“zo”以及“zoo”,但不能匹配“z”。+等价于{1,}。
?匹配前面的子表达式零次或一次。例如,“do(es)?”可以匹配“do”或“does”中的“do”。?等价于{0,1}。
?当该字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串“oooo”,“o+?”将匹配单个“o”,而“o+”将匹配所有“o”。
.匹配除“\n”之外的任何单个字符。要匹配包括“\n”在内的任何字符,请使用像“(.|\n)”的模式。
(pattern)匹配pattern并获取这一匹配的子字符串。该子字符串用于向后引用。要匹配圆括号字符,请使用“\(”“\)”
x|y匹配x或y。例如,“z|food”能匹配“z”或“food”。“(z|f)ood”则匹配“zood”或“food”。
[xyz]字符集合(character class)。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”。其中特殊字符仅有反斜线\保持特殊含义,用于转义字符。其它特殊字符如星号、加号、各种括号等均作为普通字符。脱字符^如果出现在首位则表示负值字符集合;如果出现在字符串中间就仅作为普通字符。连字符 - 如果出现在字符串中间表示字符范围描述;如果如果出现在首位则仅作为普通字符。
[^xyz]排除型(negate)字符集合。匹配未列出的任意字符。例如,“[^abc]”可以匹配“plain”中的“plin”。
[a-z]字符范围。匹配指定范围内的任意字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符。
[^a-z]排除型的字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]”可以匹配任何不在“a”到“z”范围内的任意字符。

这里写图片描述
1.grep命令
grep支持三种正则表达式引擎,分别用三个参数指定(大多数情况下只会使用到ERE和BRE):

参数说明
-EPOSIX扩展正则表达式,ERE
-GPOSIX基本正则表达式,BRE
-PPerl正则表达式,PCRE

常用参数:

参数说明
-b将二进制文件作为文本来进行匹配
-c统计以模式匹配的数目
-i忽略大小写
-n显示匹配文本所在行的行号
-v反选,输出不匹配行的内容
-r递归匹配查找
-A nn为正整数,表示after的意思,除了列出匹配行之外,还列出后面的n行
-B nn为正整数,表示before的意思,除了列出匹配行之外,还列出前面的n行
–color=auto将输出中的匹配项设置为自动颜色显示

这里写图片描述

root@ubuntu:~# # 将匹配以'z'开头以'o'结尾的所有字符串
root@ubuntu:~# echo 'zero\nzo\nzoo'|grep 'z.*o'
zero\nzo\nzoo
root@ubuntu:~# # 将匹配以'z'开头以'o'结尾,中间包含一个任意字符的字符串
root@ubuntu:~# echo 'zero\nzo\nzoo'|grep 'z.o'
zero\nzo\nzoo
root@ubuntu:~# # 将匹配以'z'开头,以任意多个'o'结尾的字符串
root@ubuntu:~# echo 'zero\nzo\nzoo'|grep 'zo*'
zero\nzo\nzoo
root@ubuntu:~# 
root@ubuntu:~# # grep默认是区分大小写的,这里将匹配所有的小写字母
root@ubuntu:~# echo '1234\nabcd' | grep '[a-z]'
root@ubuntu:~# # 将匹配所有的数字
root@ubuntu:~# echo '1234\nabcd' | grep '[0-9]'
root@ubuntu:~# # 将匹配所有的数字
root@ubuntu:~# echo '1234\nabcd' | grep '[[:digit:]]'
root@ubuntu:~# # 将匹配所有的小写字母
root@ubuntu:~# echo '1234\nabcd' | grep '[[:lower:]]'
root@ubuntu:~# # 将匹配所有的大写字母
root@ubuntu:~# echo '1234\nabcd' | grep '[[:upper:]]'
root@ubuntu:~# # 将匹配所有的字母和数字,包括0-9,a-z,A-Z
root@ubuntu:~# echo '1234\nabcd' | grep '[[:alnum:]]'
root@ubuntu:~# # 将匹配所有的字母
root@ubuntu:~# echo '1234\nabcd' | grep '[[:alpha:]]'

这里写图片描述
之所以要使用特殊符号,是因为上面的[a-z]不是在所有情况下都管用,这还与主机当前的语系有关,即设置在LANG环境变量的值,zh_CN.UTF-8的话[a-z],即为所有小写字母,其它语系可能是大小写交替的如,”a A b B…z Z”,[a-z]中就可能包含大写字母。所以在使用[a-z]时请确保当前语系的影响,使用[:lower:]则不会有这个问题。

行首符: ^ 与行尾符: $
‘^[A-Z]’ 表示以大写字母开头。
‘[^A-Z]’ 表示除了大写字母 A-Z 的所有字符。

grep -n '^the' regular_express.txt   # 匹配行首为the的字符行
grep -n '^[A-Z]' regular_express.txt # 匹配以大写字母开头的字符行    
grep -n 'd$' regular_express.txt    # 匹配以d结尾的行
grep -n '^$' regular_express.txt    # 匹配空行     

Linux中的任意一个字符用’.’(小数点)表示。

范围限定{}
由于 { 与 } 在 shell 中有特殊意义,需要用到转义字符\。

grep -n 'o\{2\}' regular_express.txt        # 查找连续的两个 o 字符
grep -n 'go\{2,5\}g' regular_express.txt    #查找 g 后面接 2 到 5 个 o,然后再接 g 的字符串

2.sed流编辑器
它不会修改文件,除非使用 shell 重定向来保存结果。默认情况下,所有的输出行都被打印到屏幕上。

sed 编辑器逐行处理文件(或输入),并将结果发送到屏幕。具体过程如下:首先 sed 把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。sed 每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。处理完输入文件的最后一行后,sed 便结束运行。sed 把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。

如果要修改原文件,可使用-i 选项。

sed [参数]... [执行命令] [输入文件]...
# 形如:
$ sed -i '1s/shiyanlou/happy/' test # 表示将test文件中第一行的"sad"替换为"happy"

这里写图片描述

参数说明
-n安静模式,只打印受影响的行,默认打印输入数据的全部内容
-e用于在脚本中添加多个执行命令一次执行,在命令行中执行多个命令通常不需要加该参数
-f filename指定执行filename文件中的命令
-r使用扩展正则表达式,默认为标准正则表达式
-i将直接修改输入文件内容,而不是打印到标准输出设备

sed执行命令的格式:

[n1][,n2]command
[n1][~step]command
# 其中一些命令可以在后面加上作用范围,形如:
$ sed -i 's/sad/happy/g' test # g表示全局范围
$ sed -i 's/sad/happy/4' test # 4表示指定行中的第四个匹配字符串

其中n1,n2表示输入内容的行号,它们之间为,逗号则表示从n1到n2行,如果为~波浪号则表示从n1开始以step为步进的所有行;command为执行动作,下面为一些常用动作指令:

命令说明
s行内替换
c整行替换
a插入到指定行的后面
i插入到指定行的前面
p打印指定行,通常与-n参数配合使用
d删除指定行

sed操作举例

先找一个用于练习的文本文件

$ cp /etc/passwd ~

打印指定行:

# 打印2-5行
$ nl passwd | sed -n '2,5p'
# 打印奇数行
$ nl passwd | sed -n '1~2p'
# d表示删除行
nl regular_express.txt | sed '2,5d' # 将 regular_express.txt 的内容列出并打印行号,同时,将 2-5 行删除显示
nl regular_express.txt | sed '2d'   # 删除第二行显示
nl regular_express.txt | sed '3,$d' # 删除第三行到最后一行, $定位到最后一行
sed -i '1d' regular_express.txt     # 在原文件中删除第 1 行

# a表示在行后加上字符串,i表示在行前添加字符串
nl regular_express.txt | sed '2a test' # 在第二行后添加 test 字符串
nl regular_express.txt | sed '2i test' # 在第二行前添加 test 字符串
nl regular_express.txt | sed '2a test\ntest' # 在第二行后加入两行 test,“\n”表示换行符

# c表示替换行
 nl regular_express.txt | sed '2,5c No 2-5 number' # 将 2-5 行内容取代为 No 2-5 number

这里写图片描述

awk文本处理语言

它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。
awk所有的操作都是基于pattern(模式)—action(动作)对来完成的,如下面的形式:

$ pattern {action}

你可以看到就如同很多编程语言一样,它将所有的动作操作用一对{}花括号包围起来。其中pattern通常是是表示用于匹配输入的文本的“关系式”或“正则表达式”,action则是表示匹配后将执行的动作。在一个完整awk操作中,这两者可以只有其中一个,如果没有pattern则默认匹配输入的全部文本,如果没有action则默认为打印匹配内容到屏幕。
awk处理文本的方式,是将文本分割成一些“字段”,然后再对这些字段进行处理,默认情况下,awk以空格作为一个字段的分割符,不过这不是固定了,你可以任意指定分隔符。
awk命令基本格式

awk [-F fs] [-v var=value] [-f prog-file | 'program text'] [file...]

其中-F参数用于预先指定前面提到的字段分隔符(还有其他指定字段的方式) ,-v用于预先为awk程序指定变量,-f参数用于指定awk命令要执行的程序文件,或者在不加-f参数的情况下直接将程序语句放在这里,最后为awk需要处理的文本输入,且可以同时输入多个文本文件。
打印文本到终端:

$ awk '{print}' test

这里写图片描述
在这个操作中我是省略了patter,所以awk会默认匹配输入文本的全部内容,然后在”{}”花括号中执行动作,即print打印所有匹配项。
- 将test的第一行的每个字段单独显示为一行

$ awk '{
> if(NR==1){
> print $1 "\n" $2 "\n" $3
> } else {
> print}
> }' test

# 或者
$ awk '{
> if(NR==1){
> OFS="\n"
> print $1, $2, $3
> } else {
> print}
> }' test

这里写图片描述
NR表示当前读入的记录数,你可以简单的理解为当前处理的行数,OFS表示输出时的字段分隔符,默认为” “空格,如上图所见,我们将字段分隔符设置为\n换行符,所以第一行原本以空格为字段分隔的内容就分别输出到单独一行了。然后是$N其中N为相应的字段号,这也是awk的内建变量,它表示引用相应的字段,因为我们这里第一行只有三个字段,所以只引用到了$3。除此之外另一个这里没有出现的$0,它表示引用当前记录(当前行)的全部内容。
- 将test的第二行的以点为分段的字段换成以空格为分隔

$ awk -F'.' '{
> if(NR==2){
> print $1 "\t" $2 "\t" $3
> }}' test

awk内置变量
这里写图片描述

Linux中的软件安装

apt-get:
在执行安装操作时,首先apt-get 工具会在本地的一个数据库中搜索 软件的相关信息,并根据这些信息在相关的服务器上下载软件安装,既然是在线安装软件,为啥会在本地的数据库中搜索?要解释这个问题就得提到几个名词了:
- 软件源镜像服务器
- 软件源
我们需要定期从服务器上下载一个软件包列表,使用 apt-get update 命令来保持本地的软件包列表是最新的(有时你也需要手动执行这个操作,比如更换了软件源),而这个表里会有软件依赖信息的记录,对于软件依赖,我举个例子:我们安装 w3m 软件的时候,而这个软件需要 libgc1c2 这个软件包才能正常工作,这个时候 apt-get 在安装软件的时候会一并替我们安装了,以保证 w3m 能正常的工作。
我们可以用它来在线安装、卸载和升级软件包等,下面列出一些apt-get包含的常用的一些工具:

工具说明
install其后加上软件包名,用于安装一个软件包
update从软件源镜像服务器上下载/更新用于更新本地软件源的软件包列表
upgrade升级本地可更新的全部软件包,但存在依赖问题时将不会升级,通常会在更新之前执行一次update
dist-upgrade解决依赖关系并升级(存在一定危险性)
remove移除已安装的软件包,包括与被移除软件包有依赖关系的软件包,但不包含软件包的配置文件
autoremove移除之前被其他软件包依赖,但现在不再被使用的软件包
purge与remove相同,但会完全移除软件包,包含其配置文件
clean移除下载到本地的已经安装的软件包,默认保存在/var/cache/apt/archives/
autoclean移除已安装的软件的旧版本软件包

apt-get的常用参数:

参数说明
-y自动回应是否安装软件包的选项,在一些自动化安装脚本中使用这个参数将十分有用
-s模拟安装
-q静默安装方式,指定多个q或者-q=#,#表示数字,用于设定静默级别,这在你不想要在安装软件包时屏幕输出过多时很有用
-f修复损坏的依赖关系
-d只下载不安装
–reinstall重新安装已经安装但可能存在问题的软件包
–install-suggests同时安装APT给出的建议安装的软件包

重新安装软件

$ apt-get --reinstall install w3m

软件搜索

$ apt-cache search softname1 softname2 softname3……

使用dpkg安装本地deb包:

参数说明
-i安装指定deb包
-R后面加上目录名,用于安装该目录下的所有deb安装包
-rremove,移除某个已安装的软件包
-I显示deb包文件的信息
-s显示已安装软件的信息
-S搜索已安装的软件包
-L显示已安装软件包的目录信息
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
https://github.com/iBotPeaches/Apktool Introduction Basic First lets take a lesson into apk files. apks are nothing more than a zip file containing resources and compiled java. If you were to simply unzip an apk like so, you would be left with files such as classes.dex and resources.arsc. $ unzip testapp.apk Archive: testapp.apk inflating: AndroidManifest.xml inflating: classes.dex extracting: res/drawable-hdpi/ic_launcher.png inflating: res/xml/literals.xml inflating: res/xml/references.xml extracting: resources.arsc However, at this point you have simply inflated compiled sources. If you tried to view AndroidManifest.xml. You'd be left viewing this. P4F0\fnversionCodeversionNameandroid*http://schemas.android.com/apk/res/androidpackageplatformBuildVersionCodeplatformBuildVersionNamemanifestbrut.apktool.testapp1.021APKTOOL Obviously, editing or viewing a compiled file is next to impossible. That is where Apktool comes into play. $ apktool d testapp.apk I: Using Apktool 2.0.0 on testapp.apk I: Loading resource table... I: Decoding AndroidManifest.xml with resources... I: Loading resource table from file: 1.apk I: Regular manifest package... I: Decoding file-resources... I: Decoding values */* XMLs... I: Baksmaling classes.dex... I: Copying assets and libs... $ Viewing AndroidManifest.xml again results in something much more human readable <?xml version="1.0" encoding="utf-8" standalone="no"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="brut.apktool.testapp" platformBuildVersionCode="21" platformBuildVersionName="APKTOOL"/> In addition to XMLs, resources such as 9 patch images, layouts, strings and much more are correctly decoded to source form. Decoding The decode option on Apktool can be invoked either from d or decode like shown below. $ apktool d foo.jar // decodes foo.jar to foo.jar.out folder $ apktool decode foo.jar // decodes foo.jar to foo.jar.out folder $ apktool d bar.apk // decodes bar.apk to bar folder $ apktool decode bar.apk // decodes bar.apk to bar folder $ apktool d bar.apk -o baz // decodes bar.apk to baz folder Building The build option can be invoked either from b or build like shown below $ apktool b foo.jar.out // builds foo.jar.out folder into foo.jar.out/dist/foo.jar file $ apktool build foo.jar.out // builds foo.jar.out folder into foo.jar.out/dist/foo.jar file $ apktool b bar // builds bar folder into bar/dist/bar.apk file $ apktool b . // builds current directory into ./dist $ apktool b bar -o new_bar.apk // builds bar folder into new_bar.apk $ apktool b bar.apk // WRONG: brut.androlib.AndrolibException: brut.directory.PathNotExist: apktool.yml // Must use folder, not apk/jar file InfoIn order to run a rebuilt application. You must resign the application. Android documentation can help with this. Frameworks Frameworks can be installed either from if or install-framework, in addition two parameters -p, --frame-path <dir> - Store framework files into <dir> -t, --tag <tag> - Tag frameworks using <tag> Allow for a finer control over how the files are named and how they are stored. $ apktool if framework-res.apk I: Framework installed to: 1.apk // pkgId of framework-res.apk determines number (which is 0x01) $ apktool if com.htc.resources.apk I: Framework installed to: 2.apk // pkgId of com.htc.resources is 0x02 $ apktool if com.htc.resources.apk -t htc I: Framework installed to: 2-htc.apk // pkgId-tag.apk $ apktool if framework-res.apk -p foo/bar I: Framework installed to: foo/bar/1.apk $ apktool if framework-res.apk -t baz -p foo/bar I: Framework installed to: foo/bar/1-baz.apk Migration Instructions v2.1.1 -> v2.2.0 Run the following commands to migrate your framework directory Apktool will work fine without running these commands, this will just cleanup abandoned files unix - mkdir -p ~/.local/share; mv ~/apktool ~/.local/share windows - move %USERPROFILE%\apktool %USERPROFILE%\AppData\Local v2.0.1 -> v2.0.2 Update apktool to v2.0.2 Remove framework file $HOME/apktool/framework/1.apk due to internal API update (Android Marshmallow) v1.5.x -> v2.0.0 Java 1.7 is required Update apktool to v2.0.0 aapt is now included inside the apktool binary. It's not required to maintain your own aapt install under $PATH. (However, features like -a / --aapt are still used and can override the internal aapt) The addition of aapt replaces the need for separate aapt download packages. Helper Scripts may be found here Remove framework $HOME/apktool/framework/1.apk Eagle eyed users will notice resources are now decoded before sources now. This is because we need to know the API version via the manifest for decoding the sources Parameter Changes Smali/baksmali 2.0 are included. This is a big change from 1.4.2. Please read the smali updates here for more information -o / --output is now used for the output of apk/directory -t / --tag is required for tagging framework files -advance / --advanced will launch advance parameters and information on the usage output -m / --match-original is a new feature for apk analysis. This retains the apk is nearly original format, but will make rebuild more than likely not work due to ignoring the changes that newer aapt requires After [d]ecode, there will be new folders (original / unknown) in the decoded apk folder original = META-INF folder / AndroidManifest.xml, which are needed to retain the signature of apks to prevent needing to resign. Used with -c / --copy-original on [b]uild unknown = Files / folders that are not part of the standard AOSP build procedure. These files will be injected back into the rebuilt APK. apktool.yml collects more information than last version SdkInfo - Used to repopulate the sdk information in AndroidManifest.xml since newer aapt requires version information to be passed via parameter packageInfo - Used to help support Android 4.2 renamed manifest feature. Automatically detects differences between resource and manifest and performs automatic --rename-manifest-package on [b]uild versionInfo - Used to repopulate the version information in AndroidManifest.xml since newer aapt requires version information to be passed via parameter compressionType - Used to determine the compression that resources.arsc had on the original apk in order to replicate during [b]uild unknownFiles - Used to record name/location of non-standard files in an apk in order to place correctly on rebuilt apk sharedLibrary - Used to help support Android 5 shared library feature by automatically detecting shared libraries and using --shared-lib on [b]uild Examples of new usage in 2.0 vs 1.5.x Old (Apktool 1.5.x) New (Apktool 2.0.x) apktool if framework-res.apk tag apktool if framework-res.apk -t tag apktool d framework-res.apk output apktool d framework.res.apk -o output apktool b output new.apk apktool b output -o new.apk v1.4.x -> v1.5.1 Update apktool to v1.5.1 Update aapt manually or use package r05-ibot via downloading Mac, Windows or Linux Remove framework file $HOME/apktool/framework/1.apk Intermediate Framework Files As you probably know, Android apps utilize code and resources that are found on the Android OS itself. These are known as framework resources and Apktool relies on these to properly decode and build apks. Every Apktool release contains internally the most up to date AOSP framework at the time of the release. This allows you to decode and build most apks without a problem. However, manufacturers add their own framework files in addition to the regular AOSP ones. To use apktool against these manufacturer apks you must first install the manufacturer framework files. Example Lets say you want to decode HtcContacts.apk from an HTC device. If you try you will get an error message. $ apktool d HtcContacts.apk I: Loading resource table... I: Decoding resources... I: Loading resource table from file: 1.apk W: Could not decode attr value, using undecoded value instead: ns=android, name=drawable W: Could not decode attr value, using undecoded value instead: ns=android, name=icon Can't find framework resources for package of id: 2. You must install proper framework files, see project website for more info. We must get HTC framework resources before decoding this apk. We pull com.htc.resources.apk from our device and install it $ apktool if com.htc.resources.apk I: Framework installed to: 2.apk Now we will try this decode again. $ apktool d HtcContacts.apk I: Loading resource table... I: Decoding resources... I: Loading resource table from file: /home/brutall/apktool/framework/1.apk I: Loading resource table from file: /home/brutall/apktool/framework/2.apk I: Copying assets and libs... As you can see. Apktool leveraged both 1.apk and 2.apk framework files in order to properly decode this application. Finding Frameworks For the most part any apk in /system/framework on a device will be a framework file. On some devices they might reside in /data/system-framework and even cleverly hidden in /system/app or /system/priv-app. They are usually named with the naming of "resources", "res" or "framework". Example HTC has a framework called com.htc.resources.apk, LG has one called lge-res.apk After you find a framework file you could pull it via adb pull /path/to/file or use a file manager application. After you have the file locally, pay attention to how Apktool installs it. The number that the framework is named during install corresponds to the pkgId of the application. These values should range from 1 to 9. Any APK that installs itself as 127 is 0x7F which is an internal pkgId. Internal Frameworks Apktool comes with an internal framework like mentioned above. This file is copied to $HOME/apktool/framework/1.apk during use. Warning Apktool has no knowledge of what version of framework resides there. It will assume its up to date, so delete the file during Apktool upgrades Managing framework files Frameworks are stored in $HOME/apktool/framework for Windows and Unix systems. Mac OS X has a slightly different folder location of $HOME/Library/apktool/framework. If these directories are not available it will default to java.io.tmpdir which is usually /tmp. This is a volatile directory so it would make sense to take advantage of the parameter --frame-path to select an alternative folder for framework files. Note Apktool has no control over the frameworks once installed, but you are free to manage these files on your own. Tagging framework files Frameworks are stored in the naming convention of: <id>-<tag>.apk. They are identified by pkgId and optionally custom tag. Usually tagging frameworks isn't necessary, but if you work on apps from many different devices and they have incompatible frameworks, you will need some way to easily switch between them. You could tag frameworks by: $ apktool if com.htc.resources.apk -t hero I: Framework installed to: /home/brutall/apktool/framework/2-hero.apk $ apktool if com.htc.resources.apk -t desire I: Framework installed to: /home/brutall/apktool/framework/2-desire.apk Then: $ apktool d HtcContacts.apk -t hero I: Loading resource table... I: Decoding resources... I: Loading resource table from file: /home/brutall/apktool/framework/1.apk I: Loading resource table from file: /home/brutall/apktool/framework/2-hero.apk I: Copying assets and libs... $ apktool d HtcContacts.apk -t desire I: Loading resource table... I: Decoding resources... I: Loading resource table from file: /home/brutall/apktool/framework/1.apk I: Loading resource table from file: /home/brutall/apktool/framework/2-desire.apk I: Copying assets and libs... You don't have to select a tag when building apk - apktool automatically uses the same tag, as when decoding. Smali Debugging Warning SmaliDebugging has been marked as deprecated in 2.0.3, and removed in 2.1. Please check SmaliIdea for a debugger. Apktool makes possible to debug smali code step by step, watch variables, set breakpoints, etc. General information Generally we need several things to run Java debugging session: debugger server (usually Java VM) debugger client (usually IDE like IntelliJ, Eclipse or Netbeans) client must have sources of debugged application server must have binaries compiled with debugging symbols referencing these sources sources must be java files with at least package and class definitions, to properly connect them with debugging symbols In our particular situation we have: server: Monitor (Previously DDMS), part of Android SDK, standard for debugging Android applications - explained here client: any JPDA client - most of decent IDEs have support for this protocol. sources: smali code modified by apktool to satisfy above requirements (".java" extension, class declaration, etc.). Apktool modifies them when decoding apk in debug mode. binaries: when building apk in debug mode, apktool removes original symbols and adds new, which are referencing smali code (line numbers, registers/variables, etc.) Info To successfully run debug sessions, the apk must be both decoded and built in debug mode. Decoding with debug decodes the application differently to allow the debug rebuild option to inject lines allowing the debugger to identify variables and types.-d / --debug General instructions Above information is enough to debug smali code using apktool, but if you aren't familiar with DDMS and Java debugging, then you probably still don't know how to do it. Below are simple instructions for doing it using IntelliJ or Netbeans. Decode apk in debug mode: $ apktool d -d -o out app.apk Build new apk in debug mode: $ apktool b -d out Sign, install and run new apk. Follow sub-instructions below depending on IDE. IntelliJ (Android Studio) instructions In IntelliJ add new Java Module Project selecting the "out" directory as project location and the "smali" subdirectory as content root dir. Run Monitor (Android SDK /tools folder), find your application on a list and click it. Note port information in last column - it should be something like "86xx / 8700". In IntelliJ: Debug -> Edit Configurations. Since this is a new project, you will have to create a Debugger. Create a Remote Debugger, with the settings on "Attach" and setting the Port to 8700 (Or whatever Monitor said). The rest of fields should be ok, click "Ok". Start the debugging session. You will see some info in a log and debugging buttons will show up in top panel. Set breakpoint. You must select line with some instruction, you can't set breakpoint on lines starting with ".", ":" or "#". Trigger some action in application. If you run at breakpoint, then thread should stop and you will be able to debug step by step, watch variables, etc. Netbeans instructions In Netbeans add new Java Project with Existing Sources, select "out" directory as project root and "smali" subdirectory as sources dir. Run DDMS, find your application on a list and click it. Note port information in last column - it should be something like "86xx / 8700". In Netbeans: Debug -> Attach Debugger -> select JPDA and set Port to 8700 (or whatever you saw in previous step). Rest of fields should be ok, click "Ok". Debugging session should start: you will see some info in a log and debugging buttons will show up in top panel. Set breakpoint. You must select line with some instruction, you can't set breakpoint on lines starting with ".", ":" or "#". Trigger some action in application. If you run at breakpoint, then thread should stop and you will be able to debug step by step, watch variables, etc. Limitations/Issues Because IDE doesn't have full sources, it doesn't know about class members and such. Variables watching works because most of data could be read from memory (objects in Java know about their types), but if for example, you watch an object and it has some nulled member, then you won't see, what type this member is. 9Patch Images Docs exist for the mysterious 9patch images here and there. (Read these first). These docs though are meant for developers and lack information for those who work with already compiled 3rd party applications. There you can find information how to create them, but no information about how they actually work. I will try and explain it here. The official docs miss one point that 9patch images come in two forms: source & compiled. source - You know this one. You find it in the source of an application or freely available online. These are images with a black border around them. compiled - The mysterious form found in apk files. There are no borders and the 9patch data is written into a binary chunk called npTc. You can't see or modify it easily, but Android OS can as its quicker to read. There are problems related to the above two points. You can't move 9patch images between both types without a conversion. If you try and unpack 9patch images from an apk and use it in the source of another, you will get errors during build. Also vice versa, you cannot take source 9patch images directly into an apk. 9patch binary chunk isn't recognized by modern image processing tools. So modifying the compiled image will more than likely break the npTc chunk, thus breaking the image on the device. The only solution to this problem is to easily convert between these two types. The encoder (which takes source to compiled) is built into the aapt tool and is automatically used during build. This means we only need to build a decoder which has been in apktool since v1.3.0 and is automatically ran on all 9patch images during decode. So if you want to modify 9patch images, don't do it directly. Use apktool to decode the application (including the 9patch images) and then modify the images. At that point when you build the application back, the source 9patch images will be compiled. Other FAQ What about the -j switch shown from the original YouTube videos? Read Issue 199. In short - it doesn't exist. Is it possible to run apktool on a device? Sadly not. There are some incompatibilities with SnakeYAML, java.nio and aapt Where can I download sources of apktool? From our Github or Bitbucket project. Resulting apk file is much smaller than original! Is there something missing? There are a couple of reasons that might cause this. Apktool builds unsigned apks. This means an entire directory META-INF is missing. New aapt binary. Newer versions of apktool contain a newer aapt which optimizes images differently. These points might have contributed to a smaller than normal apk There is no META-INF dir in resulting apk. Is this ok? Yes. META-INF contains apk signatures. After modifying the apk it is no longer signed. You can use -c / --copy-original to retain these signatures. However, using -c uses the original AndroidManifest.xml file, so changes to it will be lost. What do you call "magic apks"? For some reason there are apks that are built using modified build tools. These apks don't work on a regular AOSP Android build, but usually are accompanied by a modified system that can read these modified apks. Apktool cannot handle these apks, therefore they are "magic". Could I integrate apktool into my own tool? Could I modify apktool sources? Do I have to credit you? Actually the Apache License, which apktool uses, answers all these questions. Yes you can redistribute and/or modify apktool without my permission. However, if you do it would be nice to add our contributors (brut.all, iBotPeaches and JesusFreke) into your credits but it's not required. Where does apktool store its framework files? unix - $HOME/.local/share/apktool mac - $HOME/Library/apktool windows - $HOME/AppData/Local/apktool Options Utility Options that can be executed at any time. -version, --version Outputs current version. (Ex: 1.5.2) -v, --verbose Verbose output. Must be first parameter -q, --quiet Quiet output. Must be first parameter -advance, --advanced Advance usage output Decode These are all the options when decoding an apk. --api <API> The numeric api-level of the smali files to generate (defaults to targetSdkVersion) -b, --no-debug-info Prevents baksmali from writing out debug info (.local, .param, .line, etc). Preferred to use if you are comparing smali from the same APK of different versions. The line numbers and debug will change among versions, which can make DIFF reports a pain. -f, --force Force delete destination directory. Use when trying to decode to a folder that already exists --keep-broken-res - Advanced If there is an error like "Invalid Config Flags Detected. Dropping Resources...". This means that APK has a different structure then Apktool can handle. This might be a newer Android version or a random APK that doesn't match standards. Running this will allow the decode, but then you have to manually fix the folders with -ERR in them. -m, --match-original - Used for analysis Matches files closest as possible to original, but prevents rebuild. -o, --output <DIR> The name of the folder that apk gets written to -p, --frame-path <DIR> The folder location where framework files should be stored/read from -r, --no-res This will prevent the decompile of resources. This keeps the resources.arsc intact without any decode. If only editing Java (smali) then this is the recommend for faster decompile & rebuild -s, --no-src This will prevent the disassemble of the dex files. This keeps the apk classes.dex file and simply moves it during build. If your only editing the resources. This is recommended for faster decompile & rebuild -t, --frame-tag <TAG> Uses framework files tagged via <TAG> Rebuild These are all the options when building an apk. -a, --aapt <FILE> Loads aapt from the specified file location, instead of relying on path. Falls back to $PATH loading, if no file found -c, --copy-original - Will still require signature resign post API18 Copies original AndroidManifest.xml and META-INF folder into built apk -d, --debug Adds debuggable="true" to AndroidManifest file. -f, --force-all Overwrites existing files during build, reassembling the resources.arsc file and classes.dex file -o, --output <FILE> The name and location of the apk that gets written -p, --frame-path <DIR> The location where framework files are loaded from
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值