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
Related
I find myself in a situation similar to this question:
Linux: Overwrite all files in folder with specified data?
The answers there work nicely, however, they are for typed-out text. Allow me to provide context.
I have a Linux terminal which the following file structure: (with files & folders irrelevant to the question removed)
root/
empty.svg
svg/
257238.svg
297522.svg
a7yf872.svg
236y27fh.svg
38277.svg
... (~200 other .svg files with arbitrary names)
2903852.svg
The framework I am working with requires those .svg files to exist with those specific filenames, but obviously, it does not care about SVG image they contain. I do not plan on using such files and they take up a hefty amount of space on disk, so I wish to convert them all into empty SVGs, aka the empty.svg file on my root directory, which is a 12x12 transparent SVG file (124 bytes). This way the framework shouldn't error out like it did when I tried simply overwriting the raw data of those SVGs with plaintext using the answer of the question linked at the top of this question. I've tried many methods by trying to be creative with my basic Linux command-line knowledge but no success. How do I accomplish this?
TL;DR: How to recursively overwrite all files in a folder with the raw data of another file from Linux CLI?
Similar to the link, you can use tee command, but instead of echo use cat to copy file contents, where cat is the command to read the contents of the file.
cat empty.svg | tee svg/257238.svg svg/297522.svg <etc>
But if there are a lot of files in svg directory it will be useful to use loop to automate the previous command:
for f in svg/*; do
if [[ "$f" == *.svg ]]; then
cat empty.svg > "$f"
fi
done
Here we use pipes and redirections to connect commands and redirect previous command output.
This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 4 years ago.
New to Linux here, sorry for the (easy?) question:
I have a script in Linux called script_run that works fine if I run it once and manually designate filenames. The script reads an input file, interpolates the data, and then saves the interpolated data to an output file. I'd like the output file to have the same name, except with a "_interp" added. I want to automate the script to run for all files in the file folder directory. How do I do this? My attempt is below, and I am running the script in the file folder directory, but it fails to loop. Thank you for your help!
FILEFOLDER=$*
for FILES in $FILEFOLDER
do
script_run "
[--input ${FILES}]
[WriterStd --output tmp_output_${FILES}.txt]"
cat tmp_output_${FILES}.txt >> output_${FILES}_interp.txt
done
#!/bin/bash
FILES=`ls *.*`
for FILE in $FILES
do
script_run "[--input ${FILE}] [WriterStd --output tmp_output_${FILE}.txt]"
cat tmp_output_${FILE}.txt >> output_${FILE}_interp.txt
done
btw what's with this strange annotation [--input ${FILE}] ? Does your script explicitly requires a format like that?
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).
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/" ./*
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