Monsieur Zartosht

Monsieur Zartosht

Who am I?

A very simple blog that belongs to a very simple full-stack web developer about his daily life and struggles through coding. here you will find many things such as music, movies, and code (lots of codes!)


What I write about


Recent Posts

Make duplicate of a file in terminal

Make several copy of a single file in linux terminal by looping in given range

Table of contents

    Hi,

    sometimes you need to make several fake files to test something. for example, I wanted to populate an HTML page with several images but I needed actual file not some loop in code to generate a single image several times! (I really can't remember the actual case :D)

    let's make it short, bring up the terminal in Linux and type cd to the desired folder:

    cd /PATH/TO/FOLDER

    next type the following:

    for i in {1..100}; do cp test.jpg "test$i.jpg"; done

    Explanation

    for i in {1..100}; do ...; done

    This is the actual loop, you put your logic after do (... part).

    i is the counter variable name that can be used inside logic.

    {1...100} is the loop range.

    cp test.jpg "test$i.jpg"

    This part copy src (test.jpg) to (test$i.jpg)

    Hope it helped :-)


    Posted in Terminal on by

    Share this post: