Linux-find 和 exec来批处理指定目录下所有头文件和Cpp文件去掉注释行(23)

比如,我们只想处理login文件夹里的*.cpp和*.h(其它文件不动)

比如login/1.h如下所示:

 

比如login/1.cpp如下所示:

 

1.首先复制login文件夹到output下

mkdir output
cp login/ output/login -rf

 

2.然后删除output/login下的*.cpp和*.h(这样保留下的就是非CPP和头文件了)

find output/login -name '*.h' -type f -exec rm -rf {} \;   //删除*.h
find output/login -name '*.cpp' -type f -exec rm -rf {} \;

 

3.然后通过g++批处理来将注释去掉(如果是C语言则用gcc)

find login -name '*.h' -type f -exec g++ -E -fpreprocessed -P -dD  -c {} -o output/{} \;      //批处理*.h
find login -name '*.cpp' -type f -exec g++ -E -fpreprocessed -P -dD  -c {} -o output/{} \;    //批处理*.cpp
查看结果(不仅去掉注释,而且还自动排版了)
vi output/login/1.h:

vi output/login/1.cpp:

 


Linux命令 find 和 exec作用

 

find命令的一个选项,如下所示: (1)在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行 find ./ -name "*.txt" -exec grep "bin" {} \;

 (2)在当前目录下(包含子目录),查找10天前的log文文件 find ./ -name ".log" -mtime +10 -exec ls {} \;  ( {}和\;中间有空格,\和;中间没有空格,是连接的)

 (3)在当前目录下(包含子目录),删除10天前的log文文 find ./ -name ".log" -mtime +10 -exec rm {} \;    

 (4)在当前目录下(包含子目录),删除所有txt文件 find ./ -name "*.txt" -exec rm {} \;

 

find -name compile.sh -exec ls -l {} \;      //查找当前目录所有compile.sh内容,{}表示将找到的内容填过来

find -name compile.sh -exec chmod +x {} \;    //添加可执行属性

 

 

 
posted @ 2020-01-04 18:14  诺谦  阅读(295)  评论(0编辑  收藏  举报