Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 我想将ls的输出重定向到一个文件,并向输出文件中列出的每个文件附加一个常量字符串_Bash_File_Redirect_Ls - Fatal编程技术网

Bash 我想将ls的输出重定向到一个文件,并向输出文件中列出的每个文件附加一个常量字符串

Bash 我想将ls的输出重定向到一个文件,并向输出文件中列出的每个文件附加一个常量字符串,bash,file,redirect,ls,Bash,File,Redirect,Ls,我想将ls的输出重定向到一个文件,但也要向ls列出的每个条目附加一个字符串。例如: 假设我当前的控制器中有3个文件:a.txt、b.txt和c.txt 如果我这样做 ls>output.txt 这将把目录列表放入文件output.txt中,该文件将具有(大约) 现在,我想在每个文件名前面添加字母hello,因此file output.txt如下所示: hello a.txt hello b.txt hello c.txt 我该怎么做 #!/bin/bash for file in $(fin

我想将ls的输出重定向到一个文件,但也要向ls列出的每个条目附加一个字符串。例如:

假设我当前的控制器中有3个文件:a.txt、b.txt和c.txt

如果我这样做 ls>output.txt

这将把目录列表放入文件output.txt中,该文件将具有(大约)

现在,我想在每个文件名前面添加字母hello,因此file output.txt如下所示:

hello a.txt
hello b.txt
hello c.txt
我该怎么做

#!/bin/bash

for file in $(find . -type f -name "*.txt"); do 
  echo "hello $(basename $file)"; 
done
使用
find
查找当前目录中扩展名为
.txt
的每个文件,然后在文件名之前使用
echo
hello。使用
basename
仅获取文件名,否则它将显示为
/a.txt

如果只想在当前目录中搜索,而不想在子目录中搜索,则将
-depth 1
添加到
find
命令中

更新:回应评论:大多数Bash构造都可以通过适当地使用分号生成一行程序,因此您可以轻松地执行以下操作:

for file in $(find . -type f -name "*.txt"); do echo "hello $(basename $file)"; done

ls|sed's/^/Hello/”
,尝试阅读
find
并将
ls
替换为
find
,用于输入*;做回声“你好$i”;完成
Hi@PS,添加sed非常有效,非常感谢!!还有一个选项:
printf'hello%s\n'*
Hi zwbetz,我正在寻找一个命令行解决方案,因此您的解决方案可能可以工作,但不适用于我,谢谢
for file in $(find . -type f -name "*.txt"); do echo "hello $(basename $file)"; done