简体   繁体   English

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

[英]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.

相关问题 使用find和exec命令重命名文件以删除Linux中多个文件的扩展名 - rename files to remove extension on multiple files in linux using a find and exec command 在 Linux 中查找并复制包含模式的多个文件 - FInd and copy multiple files that contain a pattern in Linux 如何在Linux中将具有特定扩展名的文件从一个目录复制到另一个目录 - How to copy files with a particular extension from one directory to another in linux Linux:删除多个文件的文件扩展名 - Linux: remove file extensions for multiple files 从Linux中的多个文件中删除文本行 - Remove line of text from multiple files in Linux 将文件从多个文件夹复制到另一个多个文件夹Linux - Copy files from multiple folders to another multiple folders Linux 重命名多个文件,同时在Linux上保持相同的扩展名 - Rename multiple files while keeping the same extension on Linux 如何在Linux / Unix中将多个文件从一个扩展名重命名为另一个扩展名? - How to rename multiple files from one extension to another in Linux / Unix? 在Linux中,如何将多个文件归档并压缩为一个文件并删除源文件? - In linux how to archive and compress multiple files into one and remove source files? 如何在Linux中复制多个文件的开头? - How do I copy the beginning of multiple files in Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM