「Efficient Android Threading 笔记」- C1 Android Components and the Need for Multiprocessing Android RecyclerView Android Socket Programming Supporting Multiple Screens Make a Reusable UI in Android App Development 如何在 Android Studio 中包含 *.so library,并使用库中定义的方法? 使用 SpannableString 格式化字符串,实现前景色、下划线、超链接、图文混排等 如何使用 bound service 完成进程间通信? 创建自定义视图 Creating custom views 通过 Android Theme & Style 定制应用的样式 「译」Android ViewPropertyAnimator 介绍 Android Animation Interpolator - Android 动画插值器源码笔记 「译」Android Animation in Honeycomb by Chet Haase(Android 3.0系统中的动画机制) 从 Android Sample ApiDemos 中学习 android.animation API 的用法 如何学习 Android Animation? 如何实现 Android ListView「上拉加载更多」? 「译」向Big Nerd Ranch提问:为什么Fragment在Android App开发中非常重要? 分类整理我在 SegmentFault 上针对某些问题作的回答 Android Servcie 后台服务总结笔记 如何在Android设备旋转时暂存数据以保护当前的交互状态? Android Message Handler 消息处理机制总结笔记 如何获取FragmentTabHost中指定标签页的Fragment? Fragment子类必须包含一个public无参构造器 如何更新及替换ViewPager中的Fragment? 如何使用Android UI Fragment开发“列表-详情”界面? 一个Android音频文本同步的英文有声读物App的开发过程 「Android编程权威指南笔记」Android应用本地化 通过jfeinstein10/SlidingMenu实现Android侧滑菜单 为Ubuntu14.04部署Android App的Eclipse开发环境 「Android编程权威指南笔记」使用ListFragment显示列表 「Android编程权威指南笔记」SDK版本与兼容 「Android编程权威指南笔记」Android布局和组件 「Android编程权威指南笔记」UI Fragment 「Android编程权威指南笔记」Activity 第一次开发iOS App和Android的对比总结笔记 「App Training笔记」创建第一个应用 「App Training笔记」开发入门训练大纲 Android APP - 从远程FTP服务器下载文件到本地

Shell FAQs - 文件处理

2014年05月01日

查找并拷贝文件,包括完整的路径(保持文件目录结构)

find and copy files with whole path (preserve folder structure)

#从当前目录中查找名为"c-files* "的文件,排除.o二进制文件,并拷贝完整路径到destination文件夹
find . -type f \( -iname "c-files*" ! -iname "*.o" \) -print0 | rsync -av --files-from=- --from0 ./  ./destination

#从当前目录中查找名为"c-files* "的c和h文件, 并拷贝完整路径到destination文件夹
find . -type f -name 'c-files*.[ch]'  -print0 | rsync  -av --files-from=- --from0 ./  ./destination

参考

  • 上述命令来源 commandlinefu.com/commands/view/1481/rsync-find

  • Find command: Exclude / Ignore Files ( Ignore Hidden .dot Files )

    The parentheses must be escaped with a backslash, \( and \), to prevent them from being interpreted as special shell characters. 反斜杠防止圆括号被shell解释为特殊字符,\( \)是子表达式(理解对吗?)
    选项 -type f仅查找文件。
    选项 ` -or或,-and且,-not!`非。

  • find命令参考《shell脚本学习指南-10.4.3-find命令》

  • 选项说明

    ` -v, –verbose increase verbosity 显示拷贝的详细信息; -a, –archive archive mode; equals -rlptgoD (no -H,-A,-X)(?)。 -0, –from0 all *-from/filter files are delimited by 0s –files-from=FILE` read list of source-file names from FILE

  • Copying Directories with “rsync”

    One of the useful features of rsync is that when you use it copy directories, you can exclude files in a systematic way.

  • Rsync用法、实例、详解
  • 问题: 如何表达文件名字的中间部分?

使用exec完成相同的任务: Regex find and copy in bash (preserving folder structure)?- Stack Overflow

find ./ -type f \( -iname "c-files*" ! -iname "*.o" \) -exec cp --parents "{}"  ./destination \;

查找并拷贝文件,不包含路径

find /path/to/search/ -type f -name "regular-expression-to-find-files" | xargs cp -t /target/path/

上述命令来源 how to move or copy files listed by find command in unix
仅拷贝文件,不包含路径。所以当有重名文件时,不被拷贝。 cp: will not overwrite just-created dest-file with src-file

加上前缀/后缀以重命名文件

rename files with prefix/suffix

rename 's/(.*)$/prefix.$1/'  old filename

参考

  • 上述命令来源bash - How to rename with prefix/suffix? - Stack Overflow

    If rename isn’t available and you have to rename more than one file, shell scripting can really be short and simple for this. For example, to rename all .jpg to prefix*.jpg in the current directory:
    for filename in *.jpg; do mv “$filename” “prefix_$filename”; done;

  • 问题: 上述命令$1.suffix修改的结果是name.type.suffix。 name.type 如何修改为 name-suffix.type?

查找并删除文件 find and delete files

find . -type f -name "*.c" -print0 | xargs -0 rm

命令来源Find and delete .txt files in bash
当文件名中含有空格时,最好使用 -print0 的方式,以生成ASCII NUL字符串结束符。

find . -type f -name "*.c" | xargs rm That has the potential go gag on file names with spaces. If their find/xargs combo handles it, this is better: find . -name "*.txt" -print0 | xargs -0 rm to create ASCII NUL terminated strings.

$ find . -type f -name "*.c"
./find   .c
$ find . -type f -name "*.c" | xargs rm                 #文件名含有空格时,rm失败. rm ./find\ \ \ .c可以删除。
rm: cannot remove `./find': No such file or directory
rm: cannot remove `.c': No such file or directory
$ find . -type f -name "*.c" -print0 | xargs -0 rm      #rm成功
$ find ./1 -type f -name "*.c" | wc -l
0

下面的命令也可以删除,但难记。

$ find . -type f -name "*.c" -exec rm {} \;

新建文件

使用cat复制终端的输入(按ctrl+D结束输入end of the file)

cat newfile

使用echo重定向

echo "Hello World, I'm li2." > newfile

版本

li2 于上海闸北
2014-04-15 ~ 2014-05-15, v1


知识共享许可协议
li2的博客WeiYi.Li 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议进行许可。
© 2011-2022. All rights reserved by WeiYi.Li. Powerd by Jekyll & LinAnYa's Theme