Linux : 复制多个文件并删除其扩展名

Linux : Copy Multiple files and remove it's extension

我有一个包含 *.cpp.So 类型文件的目录,我想复制目录中的每个文件,然后使用

将其粘贴到同一目录中
 cp -a *.cpp 

有删除 .cpp 的选项,而 pasting.Is 可能吗?

这是一个简单的 bash 脚本。此脚本假定文件名仅包含一个“.”。性格和分裂基于此。

#!/bin/sh

for f in *.cpp; do

#This line splits the file name on the delimiter "."
baseName=`echo $f | cut -d "." -f 1`
newExtension=".new"

cp $f $baseName$newExtension

done

您可以通过 bash 参数扩展来完成此操作,如 bash 手册中所述:

${parameter%%word} Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ``%'' case) or the longest matching pattern (the ``%%'' case) deleted.
...

for i in *.cpp
do
     cp -a $i ${i%%.cpp}
done

可以使用 rename,可选择 -f 强制重写现有文件。

rename -f 's/\.ext$//' *.ext

要预览操作但不更改文件,请使用 -n 开关(无操作)。

这不是复制而是移动:-(