linux下文件夹对比

diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。diff在命令行中打印每一个行的改动。最新版本的diff还支持二进制文件。diff程序的输出被称为补丁 (patch),因为Linux系统中还有一个patch程序,可以根据diff的输出将a.c的文件内容更新为b.c。diff是svn、cvs、git等版本控制工具不可或缺的一部分。

1.命令格式:

diff[参数][文件1或目录1][文件2或目录2]

2.命令功能:

diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有当输入为文本文件时才有效。以逐行的方式,比较文本文件的异同处。如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。

3.命令参数:

-a     所有的文件都视为文本文件来逐行比较,甚至他们似乎不是文本文件.
-b     忽略空格引起的变化.
-B     忽略插入删除空行引起的变化.
-c     使用上下文输出格式.
-i     忽略大小写.
-P     在目录比较中,如果那个文件只在其中的一个目录中找到,那么它被视为在另一个目录中是一个空文件.
-q     仅报告文件是否相异,不报告详细的差异.
-r     当比较目录时,递归比较任何找到的子目录.

4、用例
diff -c -a -b -B -r A B
比较后的结果:
Only in A: aa
diff -c -a -b -B -r A/a.txt B/a.txt
*** A/a.txt     2016-11-07 09:21:57.089821250 +0800
--- B/a.txt     2016-11-07 09:17:19.231957097 +0800
***************
*** 1,6 ****
! ppackage com.ectrip.pms.app.interceptor;
! asdfdafafaf
! af 
  import com.ectrip.framework.util.StringUtil;
  import com.ectrip.pms.app.util.Const;
  import com.ectrip.pms.channel.app.vo.custom.CustomVO;
--- 1,5 ----
! package com.ectrip.pms.app.interceptor;
!
  import com.ectrip.framework.util.StringUtil;
  import com.ectrip.pms.app.util.Const;
  import com.ectrip.pms.channel.app.vo.custom.CustomVO;
***************
*** 27,33 ****
          String channel = (String) request.getAttribute("channel");
          CustomVO user = session == null ? null : (CustomVO) session.getAttribute(Const.SESSION_USER);
          if (user == null) {
!             // 删除登录cookieasfsdf
              WebUtil.removeCookie(request, response, "loginName");
              // ajax请求添加header判断登录状态,app请求
              if (isAjax ||StringUtil.isEmpty(channel) ||  channel.equalsIgnoreCase("APP")) {
--- 25,31 ----
          String channel = (String) request.getAttribute("channel");
          CustomVO user = session == null ? null : (CustomVO) session.getAttribute(Const.SESSION_USER);
          if (user == null) {
!             // 删除登录cookie
              WebUtil.removeCookie(request, response, "loginName");
              // ajax请求添加header判断登录状态,app请求
              if (isAjax ||StringUtil.isEmpty(channel) ||  channel.equalsIgnoreCase("APP")) {
***************
*** 45,48 ****
--- 42,49 ----
      } 

  }
+ :x 
+
+ :xOnly in A: aa

说明:
“+” 比较的文件的后者比前着多一行
“-” 比较的文件的后者比前着少一行
“!” 比较的文件两者有差别的行

[b]diff -c -a -b -B -r A B > ABdir.diff [/b]
比较后的差异文件到ABdir.diff文件中

[b]diff -i -b -B -r -y A B > ABdir.diff [/b]
横向对比
结果:
【code="java"】
diff -i -b -B -r -y A/a.txt B/a.txt
ppackage com.ectrip.pms.app.interceptor;                      | package com.ectrip.pms.app.interceptor;
asdfdafafaf                                                   |
af                                                            <
import com.ectrip.framework.util.StringUtil;                    import com.ectrip.framework.util.StringUtil;
import com.ectrip.pms.app.util.Const;                           import com.ectrip.pms.app.util.Const;
import com.ectrip.pms.channel.app.vo.custom.CustomVO;           import com.ectrip.pms.channel.app.vo.custom.CustomVO;
import com.ectrip.web.framework.util.WebUtil;                   import com.ectrip.web.framework.util.WebUtil;
import org.springframework.web.servlet.handler.HandlerInterce   import org.springframework.web.servlet.handler.HandlerInterce

import javax.servlet.http.HttpServletRequest;                   import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;                  import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;                          import javax.servlet.http.HttpSession;

/**                                                             /**
 * Class Describe                                                * Class Describe
 * <p/>                                                          * <p/>
 * User: yangguang Date: 16/10/15 Time: 下午12:58                * User: yangguang Date: 16/10/15 Time: 下午12:58
 */                                                              */
public class LoginInterceptor extends HandlerInterceptorAdapt   public class LoginInterceptor extends HandlerInterceptorAdapt

                                                                    @Override
    @Override                                                       public boolean preHandle(HttpServletRequest request, Http
    public boolean preHandle(HttpServletRequest request, Http               throws Exception {
            throws Exception {                                          Boolean isAjax = WebUtil.isAjax(request);
        Boolean isAjax = WebUtil.isAjax(request);                       HttpSession session = WebUtil.getSession(request, fal
        HttpSession session = WebUtil.getSession(request, fal           String channel = (String) request.getAttribute("chann
        String channel = (String) request.getAttribute("chann           CustomVO user = session == null ? null : (CustomVO) s
        CustomVO user = session == null ? null : (CustomVO) s           if (user == null) {
        if (user == null) {                                   (
            // 删除登录cookieasfsdf                           |             // 删除登录cookie
            WebUtil.removeCookie(request, response, "loginNam               WebUtil.removeCookie(request, response, "loginNam
            // ajax请求添加header判断登录状态,app请求                       // ajax请求添加header判断登录状态,app请求
            if (isAjax ||StringUtil.isEmpty(channel) ||  chan               if (isAjax ||StringUtil.isEmpty(channel) ||  chan
                response.addHeader("loginStatus", "0");                         response.addHeader("loginStatus", "0");
                response.setStatus(401);                                        response.setStatus(401);
                                                                                return false;
                return false;                                               } else {
            } else {                                                            response.sendRedirect("login.jsp");
                response.sendRedirect("login.jsp");                         }
            }                                                               return false;
            return false;                                               }
        }                                                               return true;
        return true;
                                                                    }
    }
                                                                }
}                                                             (
                                                              > :x
                                                              >
                                                              > :x
                                                              >

说明:
“|”表示前后2个文件内容有不同
“<”表示后面文件比前面文件少了1行内容
“>”表示后面文件比前面文件多了1行内容

猜你喜欢

转载自starlight-520.iteye.com/blog/2335881