处理空间在bash的文件名 [英] Handling spaces in filenames in bash

查看:140
本文介绍了处理空间在bash的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的目录结构:

  ParentDir
 |
  - 子方向1
     |
      - 文件One.txt
      - 文件
 |
  - 儿童方向2
     |
      - 文件Two.txt
      - 文件

我试图找出所有的 .TXT 文件和计数总和在其中的行数,所以我做这样的事情:

  TXTFILES = $(找到。-name* .TXT)
猫$ TXTFILES |厕所-l

但问题是,我得到的,

 猫:./Child:没有这样的文件或目录
猫:方向2 /文件:没有这样的文件或目录
猫:Two.txt:没有这样的文件或目录
猫:./Child:没有这样的文件或目录
猫:方向1 /文件:没有这样的文件或目录
猫:One.txt:没有这样的文件或目录

我如何处理迪尔和放大器的空间;文件在这种情况下名字呢?是否有可能的没有的使用循环?


解决方案

 找到。 -name* .TXT-exec猫 - '{}'; |厕所-l

这运行单独为每个文件(小于理想的),但文件名是直接从通过找到不被shell解析。因此,文件名可以是任何东西那是在命令行上有问题。

I have this dir structure:

ParentDir
 |
 -- Child Dir1
     |
     -- File One.txt
     -- File
 |
 -- Child Dir2
     |
     -- File Two.txt
     -- File

I'm trying to find out all the .txt files and count the sum total of number of lines in them, so I do something like this:

TXTFILES=$(find . -name '*.txt')
cat $TXTFILES | wc -l

but the problem is, I get,

cat: ./Child: No such file or directory
cat: Dir2/File: No such file or directory
cat: Two.txt: No such file or directory
cat: ./Child: No such file or directory
cat: Dir1/File: No such file or directory
cat: One.txt: No such file or directory

How do I handle spaces in the Dir & File names in such a situation? Is it possible without using a loop?

解决方案

find . -name '*.txt' -exec cat -- '{}' ';' | wc -l

This runs cat individually for each file (which is less than ideal), but the filename is passed directly from find to cat without being parsed by the shell. So the filename could be anything that's problematic on the command line.

这篇关于处理空间在bash的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆