Linux查找Bash/Bourne文件,然后添加.sh

Linux查找Bash/Bourne文件,然后添加.sh,linux,bash,Linux,Bash,我是个笨蛋,但这是我的问题 我在一个文件夹中有不同的文件,我需要将.sh添加到其中一些文件中(只有bash Bourne文件)。 我还被迫使用File命令来知道是哪一个 我做到了:file folder/*| grep'Bourne'| mv这里的任何东西 File命令向我显示类型,然后我将它们导入grep,以找到我想要重命名的Bash/borneshell类型 现在我在这里储备了BC,我显然不能用管道把他们的名字改成BC,我不是用管道把他们的剧目放在哪里,而是用“文件信息”命令 我如何继续 我

我是个笨蛋,但这是我的问题

我在一个文件夹中有不同的文件,我需要将.sh添加到其中一些文件中(只有bash Bourne文件)。 我还被迫使用File命令来知道是哪一个

我做到了:
file folder/*| grep'Bourne'| mv这里的任何东西

File命令向我显示类型,然后我将它们导入grep,以找到我想要重命名的Bash/borneshell类型

现在我在这里储备了BC,我显然不能用管道把他们的名字改成BC,我不是用管道把他们的剧目放在哪里,而是用“文件信息”命令

我如何继续

我必须使用File、Xargs和最多三个管道

当回声看上去可能与您想要的相符时,请将其移除

我做了什么

使用
awk
而不是grep,这允许您仅输出与
文件
输出中的'Bourne'匹配的文件名

然后将这些名称输入到新的bash中
xargs-i
(我知道,我知道,它已被弃用,我应该使用-i“{}”,但只要它有效;))这使得
{}
成为从
awk
传递的文件名的占位符,只需使用file和awk:

file * |awk -F: '/Bourne/ {system("mv "$1" "$1".sh")}'
解释

file * - displays descriptions of the contents of files in the current directory
-F: sets separator to character:
/Bourne/ matches lines containing the word Bourne
system() executes a system command in awk (mv here)
$1 is the first field on the line, which is the name of the file
the characters " are arranged so that awk expands $1 as variables, not as a string

这有用吗?在一个代码块中管道化到一个循环中。与其他答案相比,这看起来有点笨拙,但了解这项技术很有用

file folder/* | awk '/Bourne/ {print $1}' | { while read Fname ; do mv "${Fname%:}" 
"${Fname%:}".sh; done; }
别忘了在最后一次}之前你需要这个空间

%正在删除文件名右侧的,您不希望这样

file folder/* | awk '/Bourne/ {print $1}' | { while read Fname ; do mv "${Fname%:}" 
"${Fname%:}".sh; done; }