Limit for bash loop

I have a problem with mass creation files and directories. I want to create something like this:

dummy_directory_1/dummy_file_1 dummy_directory_2/dummy_file_2 dummy_directory_3/dummy_file_3 

using a loop:

 for(( i=1; $i <=1000; i++ )); do mkdir $(date --date="$i day ago" +%Y%m%d%H%M%S); touch $(date --date="$i day ago" +%Y%m%d%H%M%S)/$(date --date="$i day ago" +%Y%m%d%H%M%S)_file; done 

Not all files are created because I get the following errors:

 touch: cannot touch `20140211230556/20140211230556_file': No such file or directory touch: cannot touch `20131105230559/20131105230559_file': No such file or directory touch: cannot touch `20130529000604/20130529000604_file': No such file or directory 

Did you know that bash / linux has some limitations for creating a file? I had a similar problem when I made a script to search for many files using grep. If I change the cycle $i<=10 , it works. Please, help.

+6
source share
1 answer

Your code only works if two date calls generating the directory name are executed within the same second, if this happens to change between them, you will get two different values ​​for the pointer name.

First try storing the value in a variable:

 for(( i=1; i <= 1000; i++ )); do name=$(date --date="$i day ago" +%Y%m%d%H%M%S) mkdir -p "$name" && touch "$name/${name}_file" || break done 
+10
source

All Articles