unix清除文件中的内容而不删除任何目录

时间:2014-02-16 10:42:24

标签: unix

我有一个目录“ADir”,包含递归的一些目录。

我可以运行什么unix命令来确保

1) directories are not deleted AND 
2) NO file is deleted
3) all the files currently present contain only one data and that is 0 

以及

的命令
1) directories are not deleted AND 
2) NO file is deleted
3) all the files currently present are  emptied out 

1 个答案:

答案 0 :(得分:2)

使用find(1)

  1. echo 0 > {} :( {}替换为匹配的文件路径)

    find ADir -type f -exec sh -c "echo 0 > {}"  \;
    
  2. > {}

    find ADir -type f -exec sh -c "> {}"  \;
    
  3. <强>更新

    对于大量文件,请使用单个shell实例:

    find Adir -type f | while read path; do echo 0 > $path; done
    
    find Adir -type f | while read path; do > $path; done