2

I am working on project where i need to create this kind of Folder structure with FTP Account. I just have FTP account detail and no access to the server,

I can connect with FTP and can create directory manually but i need to create such a folder structure within ftp root directory.

    YYYY
    |_MM
    |_DD

2018
|_06
 |_01
 |_02
 |_03
 |_..............30
|_07
 |_01
 |_02
 |_03
 |_..............31

please advice what will be the best approach to finish it without manually creation of folder.

EDITED After some hint from comments/

I wrote this script to create the folder structure but the date concept didnt work with my scripts.

#/bin/bash
for i in $(seq -w 12)
do  
    cd /home/user/upload/2018/
    mkdir $i
    cd $i
    for j in $(seq -w 31)
    do 
        mkdir $j
    done
done
2
  • You could create such structure locally using bash and push it to the ftp server with mput command. Jun 1, 2018 at 11:29
  • @ArkadiuszDrabczyk, thanks that will be good approach, I will try to write such a bash script.
    – kunal
    Jun 1, 2018 at 11:31

2 Answers 2

1

The following bash script will create a directory structure you want:

#!/usr/bin/env bash

set -e

year=2018

mkdir "$year"

for month in {6..12}
do
    mkdir "$year"/"$month"
    days=$(cal "$month" "$year" | awk 'NF {DAYS = $NF}; END {print DAYS}')

    for ((i=1; i <= days; i++))
    do
    mkdir "$year"/"$month"/"$(printf "%02d\n" "$i")"
    done
done

Use tree to see how the structure looks like:

$ tree 2018
2018
├── 10
│   ├── 01
│   ├── 02
│   ├── 03
│   ├── 04
│   ├── 05
│   ├── 06
│   ├── 07
│   ├── 08
│   ├── 09
│   ├── 10
│   ├── 11
│   ├── 12
│   ├── 13
│   ├── 14
│   ├── 15
│   ├── 16
│   ├── 17
│   ├── 18
│   ├── 19
│   ├── 20
│   ├── 21
│   ├── 22
│   ├── 23
│   ├── 24
│   ├── 25
│   ├── 26
│   ├── 27
│   ├── 28
│   ├── 29
│   ├── 30
│   └── 31
├── 11
│   ├── 01
│   ├── 02
(...)

The method for checking how many days are in a given month comes from here.

11
  • The inner for next can be 1 mkdir command. All you need is the end day number of a month and do mkdir {01:endday} and it will make 01 through 31 if 31 days.
    – Rinzwind
    Jun 1, 2018 at 11:57
  • 1
    @Rinzwind: mkdir {01:31} creates {01:31} directory, I use GNU bash, version 4.3.46. I thought about using mkdir {01..$days} but shellcheck says: SC2051: Bash doesn't support variables in brace range expansions. Jun 1, 2018 at 12:04
  • new_fol.sh: 13: new_fol.sh: Syntax error: Bad for loop variable error comes at this line ` for ((i=1; i <= days; i++)) `
    – kunal
    Jun 1, 2018 at 12:05
  • 1
    I see that you named the script new_fol.sh - what if you run it like this: bash new_fol.sh? Jun 1, 2018 at 12:15
  • 1
    @Rinzwind: I know about sequence expressions, notice that I've also used them in my script. You mentioned mkdir {01:endday} which doesn't work for me even with Bash 4.4.19 I've just built form git. Jun 1, 2018 at 12:20
0

With GNU date, you could do it by incrementing the date directly and using path separators in the format e.g.

d='2018/06/01'
while [ "$d" != "2018/12/31" ]; do 
  mkdir -p "$d"
  d="$(date '+%Y/%m/%d' -d "$d + 1 day")"
done

Choose the start date and end date (inside the while condition) appropriately.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .