简体   繁体   中英

(Linux) Recursively overwrite all files in folder with data from another file

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM