Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've seen an app for Mac called Structurer (here a video http://www.youtube.com/watch?v=kXRjneFTs6Q) which given a structure of files and folders like this:

/folder1
/folder2
  /file1
  /folder2.1
    /file2.1

will create this files and folders in a existing place.

There is something like that for Ubuntu, or how can i build a shell script to get something like this?

If is possible would be cool also to be able to create files using templates.

share|improve this question

1 Answer 1

The tricky part is that you have files and folders shown the same way, and no simple way to tell them apart. For file 2.1 there is not a way to tell if it (/folder2/folder2.1/file2.1) is actually a folder or a file within folder2.1.

Yes your description says the word *file* so we know you mean file, but to a program how could it tell? A program might tell files from paths because one is followed at the same indentation level with another entry. BUT, that leads to a set of complicated and confusing rules.

I would suggest you use a keyword or just full qualified names. The easiest is:

/folder1/

/folder2/

/folder2/file1

/folder2/folder2.1/

/folder2/folder2.1/file2.1

The trailing slash means "This is a folder and not a file." Then you can have an easy script like this to create your directory structure. A couple warnings about this script.

  1. the higher level directories must be created first.
  2. I prepend a "." to the path so all directories created are relative to the one in which the script is run.
  3. I do no error checking for the content of the dir/path file.
#!/bin/sh -v
#
# builds a directory and file structure.
# directories must exists before referenced or file in the directory declared.
#
# sample input looks like (without the leading #):
# /folder1/
# /folder2/
# /folder2/file1
# /folder2/folder2.1/
# /folder2/folder2.1/file2.1
#
# make sure we have our one and only input parameter.
if [ $# != 1 ]; then
        echo "Usage: `basename $0` input_file_name"
        echo "input_file_name contains absolute dir paths with a trailing slash,"
        echo "or absolute file path/name with no trailing slash."
        exit 1
fi

# get the file name from the command line
FILE=$1

# make sure the input parameter specifies a file.
if [ ! -e ${FILE} ]; then
        echo "Sorry, the file ${FILE} does not exist."
        exit 1
fi

for LINE in $(cat $FILE)
do
        LAST=$(echo ${LINE} | awk -F/ '{print $(NF)}')
        # if file ends with a slash, this value is blank, if it is no slash, this is the file name.
        if [ "${LAST}XXX" = "XXX" ]; then
                # is empty, so it is directory, future feature to check exist already
                mkdir ".${LINE}"
        else
                # is not empty, so it is a file
                touch ".${LINE}"
        fi
done
exit 0

This will create the directories and files as shown in the input file. If the script is called create.sh and you have chmod 755 create.sh then the command ./create.sh data will generate your directories and files as described in the data file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.