[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Duplicating a partition's directory structure - How?



On 2017-01-03 12:13, Richard Owlett wrote:
> I wish to duplicate a partition's directory structure without any of the
> existing file contents. The immediate application is a heavily customized
> version of an installation DVD. There are two underlying goals. I wish to reuse
> some existing utilities which expect to find data in a particular branch of the
> directory tree. The second is very similar in that a person familiar with a
> structure would assume that certain types of information will be in a particular
> sub-directory.
> 
> Also this will be an educational experience as I expect the answer will be
> elegant in its simplicity and point me towards chasms in my understanding of Linux.
> 
> Thank you.

In cases where I can't use rsync I use this function in bash:

#Make directories based on the contents of a file (one dir per line)
function mkdir_from_file {
  #Verify first argument (source file)
  if [ -z "$1" ]; then
    echo "[ERROR] No file given!"
    return 1
  fi
  if [ ! -f "$1" ]; then
    echo "[ERROR] '$1' is not a file or does not exist!"
    return 1
  fi
  #Read a line from the given file and make a directory using that line as a
  #path. Each line is used "as is". No validity checking is done.
  while read -r LINE; do
    #Skip lines beginning with a hash
    [[ "$LINE" = "\#*" ]] && continue
    #Strip trailing comments and spaces
    LINE="${LINE%%#*}"
    LINE="${LINE%% *}"
    #Skip if no path remains
    [[ "$LINE" == "" ]] && continue
    #Make a dir using the line as a path
    mkdir -p "$LINE"
  done < "$1"
}

You can easily build the input file with find -type d

Sometimes I need to add a new directory structure to systems. In those cases I
just write the wanted dirs to a file and run this script. Maybe you can adjust
it to your needs.

HTH

Grx HdV



Reply to: