[英]Linux : Copy Multiple files and remove it's extension
I have got a directory containing files of type *.cpp.So i would like to copy each file in the directory and to paste it in the same directory by using 我有一个包含* .cpp.So类型文件的目录。我想复制目录中的每个文件并使用它将其粘贴到同一目录中
cp -a *.cpp
with an option to remove the .cpp while pasting.Is it possible ? 可以选择在粘贴时删除.cpp。这可能吗?
Here is a simple bash script. 这是一个简单的bash脚本。 This script assumes that the file name only contains one "."
此脚本假定文件名仅包含一个“。” character and splits based on that.
基于此的角色和分裂。
#!/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
You can do this just by means of bash parameter extension, as mentioned in the bash manual: 您可以通过bash参数扩展来执行此操作,如bash手册中所述:
${parameter%%word} Remove matching suffix pattern.
$ {parameter %% word}删除匹配的后缀模式。 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
Can use rename , optionally with -f to force rewrite existing files. 可以使用重命名 ,可选地使用-f强制重写现有文件。
rename -f 's/\.ext$//' *.ext
To preview actions, but don't change files, use -n switch (No action). 要预览操作,但不要更改文件,请使用-n开关(无操作)。
This is not copy but move :-( 这不是复制,但移动:-(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.