TransWikia.com

create a directory for every file and generate "n" copies for each file

Unix & Linux Asked by edwin reyes on January 25, 2021

while I was looking for a solution for my files, I found something that is perfect, I include the answer here:
How do I create a directory for every file in a parent directory

but now, my problem is how can generate 50 copies to the directories generated by each file
I was dealing with the following command line

ls -p | grep -v / | xargs -t -n1 -i bash -c 'for i in {1..50}; do cp {} "{}_folder/copy${i}_{}" ; done'

to get the following

-file1.csv---->folder_for_file1---->1copy_file1.csv,2copy_file1.csv,3copy_file1.csv........50copy_file1.csv
-file2.csv---->folder_for_file2---->1copy_file2.csv,2copy_file2.csv,3copy_file2.csv........50copy_file2.csv
-file3.csv---->folder_for_file3---->1copy_file3.csv,2copy_file3.csv,3copy_file3.csv........50copy_file3.csv
...
-file256.csv---->folder_forfile256---->1copy_file256.csv,2copy_file256.csv,3copy_file256.csv........50copy_file256.csv

How can I match this with the previous answer??, include the functional code of that answer

cd ParentFolder
for x in ./*.csv; do
  mkdir "${x%.*}" && mv "$x" "${x%.*}"

done

all the credits to the person who generated this great answer
and thanks in advance to everyone

One Answer

Fifty strings, each prefixed by a number between 1 and 50 and followed by some string $name, can be created using {1..50}"$name" in the bash shell.

To write something to all of these at once, use tee:

tee {1..50}"$name" >/dev/null <somefile

... where somefile is a file whose data should be duplicated fifty times. The tee utility duplicates its standard input stream into each of the files named on its command line, and to standard output (which is why we redirect its standard output stream to /dev/null).

Creating a directory for each of the regular files in some $topdir directory:

[ -n "$topdir" ] && cd -- "$topdir" || exit 1

for name in *; do
    if [ ! -f "$name" ] || [ -h "$name" ]; then
        # $name refers to non-regular file,
        # or to a symbolic link pointing to a regular file.
 
        continue
    fi

    mkdir -- "${name%.*}"
done

The -- is used to signal the end of command line options. It is handy to use when we don't know whether $name (in this instance) may start with a dash, as in -farse.txt, which could be misinterpreted as one or several options when using it with mkdir (or tee or rm below).

Note that the mkdir call would fail if the filename does not contain a dot, since the script would attempt to create a directory whose name is identical to an existing file.

Modifying this to duplicate the file fifty times in the newly created directory (if the directory was created), and to remove the original file (it's unclear from your question whether you want the original removed or not, so I have commented out the rm -f for safety):

[ -n "$topdir" ] && cd -- "$topdir" || exit 1

for name in *; do
    if [ ! -f "$name" ] || [ -h "$name" ]; then
        # $name refers to non-regular file,
        # or to a symbolic link pointing to a regular file.
 
        continue
    fi

    destdir=${name%.*}

    if mkdir -- "$destdir" && tee -- "$destdir"/{1..50}"$name" >/dev/null <"$name"
    then
        printf 'Would remove "%s"n' "$name"
        #rm -f -- "$name"
    fi
done

Without using tee and instead using a simple loop to copy the file fifty times:

[ -n "$topdir" ] && cd -- "$topdir" || exit 1

for name in *; do
    if [ ! -f "$name" ] || [ -h "$name" ]; then
        # $name refers to non-regular file,
        # or to a symbolic link pointing to a regular file.
 
        continue
    fi

    destdir=${name%.*}

    mkdir -- "$destdir" || continue

    for newname in {1..50}"$name"; do
        cp -- "$name" "$destdir/$newname"
    done

    printf 'Would remove "%s"n' "$name"
    #rm -f -- "$name"
done

Note that un-commenting the rm -f at the end would remove the original file even if none of the cp in the loop succeeds (leading to data loss).

Answered by Kusalananda on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP