Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating runtime directory structure while copying files on unix

Status
Not open for further replies.

skuthe

Programmer
Sep 10, 2002
33
0
0
US
Hi,
I am trying to clean files from various directories and copy them under "cleaned" directory, but I want to create the directory structure of the cleaned file under the cleaned directory at runtime.

F.eg: I cleaned a file using SED command as below..

sed 's/\/users\/ciimdrp/g' $fname > /users/ciimdrp/cleaned/$fname.
The value of $fname here is ./aop/sqr/test.txt
After cleaning the file, if the ./aop/sqr does not exists under the /users/ciimdrp/cleaned directory then the program must create that directory under the cleaned dir path and copy the file there else just copy the file or overwrite the existing file.
I hope I have conveyed properly what I am looking for !.

Thank you in advance.
 
Code:
#!/bin/bash

# general purpose simple backup
backup() {
  local from=$1 to=$2 cut=$3; shift 3
  local IFS=$'\x02' # should not appear in filenames
  [[ $cut == same ]] && cut=$from

  while read src dst; do
    echo $src --\>\> "$to/$dst"
    
    [[ $do_cp ]] && {
      [[ ! -d $to/$dst ]] && mkdir -p $to/$dst
      ${cp=cp} -a "$src" "$to/$dst/" || { echo failed; return 1; }
    }
  done < <( find $from -type f |
    awk -v SEP="$IFS" -v cut="$cut" '{ printf "%s%s", $0, SEP
      sub(cut, ""); sub(/\/[^\/]+$/, "/"); print }' )
}

backup /tmp
do_cp=1 backup /lib /tmp/test same

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
man dirname
man mkdir

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top