Renaming Files With Same Name, Different Extension in Linux: [duplicate] - linux

This question already has answers here:
find a pattern in files and rename them [closed]
(4 answers)
Closed 8 years ago.
Say I have a listing of files with the same name, but different file extensions:
name.a
name.b
name.c
...
name.z
and want to rename them to:
newname.a
newname.b
newname.c
...
newname.z
How could I do this rename operation in one bash command?

You can use rename utility:
rename 's/^name\./newname./' name.*

You can use parameter expansion:
for f in name.*; do
ext="${f##*.}"
mv "$f" "newname.$ext"
done
There is an excellent write-up about it here

Related

copy and rename file in linux bash [duplicate]

This question already has answers here:
Rename multiple files while keeping the same extension on Linux
(4 answers)
Closed 3 years ago.
I'd like to copy and rename multiple files within the same folder. Just like I have the files
foo.c foo.h and want to use them as a template for new files named bar.c bar.h.
cp foo.* bar.*
would describe what I mean but won't work.
using rename will just overwrite the old files.
Is there some simple solution for this or do I have to create a whole script that opens a folder in /tmp, copy there, rename there and move back?
I just found the answer myself with that wonderful tool mcp
mcp 'foo.*' 'bar.#1'
You can do it with a simple for loop and some string manipulation
#!/bin/bash
# for each file following the pattern "foo."
for i in foo.*
do
# copy file to "bar" + original extension
cp $i bar.${i#foo.}
done

How to copy a particular extension file from a directory and sub-directory to other directory using batch script or linux cmmand [duplicate]

This question already has answers here:
Recursive copy of a specific file type maintaining the file structure in Unix/Linux? [closed]
(7 answers)
Closed 6 years ago.
lets say i have a dir C:\Project\File\ this directory contains folder1,folder2,folder3 and so on.
folder1, folder2 and folder3 have files with extension .txt
now i want to copy all the .txt file from all the folder to a folder test in path C:\Users\Use\Desktop\Test
How to do this using batch scripting or using linux command.
as batchfile: for (recursive /r) all .txt files copy the file to <new path>\<name>.<extension> (%%~nxa):
for /r %%a in (*.txt) do #ECHO copy "%%a" "%C:\Users\Use\Desktop\Test\%%~nxa"
if you want to try on command line, use a single % instead of double %%.
Remove #ECHO, if the output is what you want to do.
use find with exec:
find C:\\Project\\File -name "*.txt" -exec cp {} C:\\Users\\Use\\Desktop\\Test \;
I'm not sure about the directories because you mention windows style but want find (unix style).

Ubuntu - bulk file rename [duplicate]

This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 2 years ago.
I have a folder containing a sequence of files whose names bear the form filename-white.png. e.g.
images
arrow-down-white.png
arrow-down-right-white.png
...
bullets-white.png
...
...
video-white.png
I want to strip out the -white bit so the names are simply filename.png. I have played around, dry run with -n, with the Linux rename command. However, my knowledge of regexes is rather limited so I have been unable to find the right way to do this.
If you are in the directory above images, the command is
rename "s/-white.png/.png/" images/*
If your current directory is images, then run rename "s/-white.png/.png/" ./* instead. To do a dry run, just attach a -n like you said:
rename -n "s/-white.png/.png/" images/*
or
rename -n "s/-white.png/.png/" ./*

How do you mass rename files in Linux? [duplicate]

This question already has answers here:
How to rename with prefix/suffix?
(10 answers)
Closed 7 years ago.
there are 30 files named like this:
sample_kmer_41_2.lib1.bowtie.file1.27.faabyss_sample
I need the output to be:
abyss_sample_kmer_41_2.lib1.bowtie.file1.27.fa
I don't know how to do this, I've already screwed the names up a lot. thanks for any help.
Use the rename utility:
rename 's/(.*)\.fa(.*)_sample$/$2_$1/' *
rename is part of the perl package and is installed by default on debian-like systems. There is a different and incompatible rename utility that is provided as part of the util-linux packages.
If you have the Perl-based prename (possibly named rename) command, then:
prename 's/(.*)abyss_sample$/abyss_$1/' sample_kmer_*
The exact regex to use depends on how the names have been damaged. This should work for the example name given; it may need tweaking to work with other names.
You could use a for loop. Since I'm always nervous about this stuff I'd recommend doing an echo first to make sure everything looks groovy...
#!/bin/bash
for FILE in sample_kmer_41_2*abyss_sample ; do
NEWNAME=`echo $FILE | sed -e 's/abyss_sample//'`
echo mv $FILE $NEWNAME
done
Provided that doesn't need tweaking, you can remove the echo or make modifications as necessary.

Linux/Cygwin recursively copy file change extension [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm looking for a way to recursively find files with extension X (.js) and make a copy of the file in the same directory with extension Y (.ts).
e.g. /foo/bar/foobar.js --> /foo/bar/foobar.js and /foo/bar/foobar.ts
/foo/bar.js --> /foo/bar.js and /foo/bar.ts etc etc
My due diligence:
I was thinking of using find & xargs & cp and brace expansion (cp foobar.{js,ts}) but xargs uses the braces to denote the list of files passed from xargs. This makes me sad as I just recently discovered the awesome-sauce that is brace expansion/substitution.
I feel like there has to be a one-line solution but I'm struggling to come up with one.
I've found ideas for performing the task: copying the desired to a new directory and then merging this directory with the new one; recursively run a renaming script in each directory; copy using rsync; use find, xargs and cpio.
As it stands it appears that running a renaming script script like this is what I'll end up doing.
find . -name "*.js" -exec bash -c 'name="{}"; cp "$name" "${name%.js}.ts"' \;
Using find, you can execute a command directly on a file that you've found, by using the -exec option; you don't need to pipe it through xargs. It takes the command name followed by arguments to the command, followed by a single argument ;, which you have to escape to avoid the shell interpreting it. find will replace any occurrence of {} in the command name or arguments with the file found.
In order call a command with the appropriate ending substituted, there are multiple approaches you can take, but a simple one is to use Bash's parameter expansion. You need to define a shell parameter that contains the name (in this case, I creatively chose name={}), and then you can use parameter expansion on it. ${variable%suffix} strips off suffix from the value of $variable; I then add on .ts to the end, and have the name I'm looking for.

Resources