0

What I am currently struggling is to create multiple files and storing it into each directory. I have made 100 directories like this:

mkdir ./dir.{01..100}

What I want is to create 100 text files for each directory. So that the result will show like:

click on dir.01 at home dir, which has files named: 01.txt to 100.txt

Also, I want to compress all 100 directories, each containing 100 text files into one using tar.

I am struggling with:

  1. creating 100 text files each in 100 directories
  2. using tar to zip all 100 directories together.

I am more interested in making creating 100 text files IN 100 directories. Also I am MUCH MORE interested in how to use tar to join all 100 directories together in specific file (fdtar) for instance.

bfontaine
  • 18,169
  • 13
  • 73
  • 107
colbyjackson
  • 175
  • 2
  • 10
  • read the docs on bash about `for` loop. check examples like [this one](https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash). – user3159253 Sep 30 '17 at 22:17
  • Possible duplicate of [How do I iterate over a range of numbers defined by variables in Bash?](https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash) – user3159253 Sep 30 '17 at 22:17
  • That's already done. I know how to iterate over a range of numbers. I made directories in such manner --> ./dir/{01..100} which actually returns the samething – colbyjackson Sep 30 '17 at 22:20
  • `tar` is an archive format, not a compression one. Please be more specific. – bfontaine Sep 30 '17 at 23:43

2 Answers2

0

If I understand you need two things. First, you have 100 directories and need to create a file in each. With a for loop in bash run from the parent directory where all other directories you have created are:

for n in dir.*
do
  f=`echo $n | sed s/dir\.//`
  echo "This is file $n" >"$n/$f.txt"
done

Regarding tar that is even easier because tar will take multiple directories and glue them together. From the parent directory try:

tar cvf fd.tar dir.*

The c option will create the archive. v will tell tar to print all it is doing so you know what is happening. f directories.tar will create the archive with that name.

When you undo the tar operation, you will use:

tar xvf fd.tar

In this case x will extract the contents of the tar archive and will create all 100 directories for you at the directory from which you invoke it.

Note that I have used fd.tar and not fdtar as the .tar extension is the customary way to signal that the file is a tar archive.

Javier Elices
  • 2,066
  • 1
  • 16
  • 25
  • This will break in multiple places if one of the directories contains a space or some shell metacharacters. Always, *always* use double quotes around variables which contain file names. https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Oct 02 '17 at 04:39
  • Suggestion accepted, thank you. Although the problem was stated with no such cases. – Javier Elices Oct 02 '17 at 08:48
0

If you are fine with empty files,

touch ./dir.{01..100}/{01..100}.txt

If you need each file to contain something, use that as the driver in a loop:

for file in ./dir.{01..100}/{01..100}.txt; do
    printf "This is the file %s\n" "$file">"$file"
done

This could bump into ARG_MAX ("argument list too long") on some platforms, but it works fine on MacOS and should work fine on any reasonably standard Linux.

Splitting the loop into an inner and an outer loop could work around that problem:

for dir in ./dir.{01..100}; do
    for file in {01..100}.txt; do
        printf "This is file %s/%s\n" >"$dir/$file"
    done
done
tripleee
  • 175,061
  • 34
  • 275
  • 318