Problem with array and creating directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with array and creating directories
# 1  
Old 03-15-2011
Error Problem with array and creating directories

I have an interesting requirement. I have declaried an array like :-
Code:
arr=`find . ! -name "." | xargs -I {} echo {} | cut -c 2-${#}`

Then i will try to access the array elements like :-
Code:
i=0
for i in ${arr[i]}; do

Here comes the confusions, the array elements are basically dir and files stored as strings
something like :-
"/etc/src/esr/11.2/aix" - This is a dir
"/etc/src/esr/11.2/aix/defalut.cfg" - This is a file inside the above dir

"/etc/src/esr/11.2/fn" - This is a dir
"/etc/src/esr/11.2/hpux/fn95anes.cfg" - This is a file inside the above dir

I want to create the same directories and files(with contents in the file) in destination as well.
That means i need to check whether the array element is dir/file then accordingly i need to create directories inside that i need to create files respectively

I did something like
Code:
if [[ -d $DIR/$i ]] ; then  (DIR and DEST are predefined location)
mkdir $DIR/$i $DEST/$i
elif [[ -f $DIR/$i ]] ; then
cp $DIR/$i $DEST/$i

But it is not properly creating files inside directories, please help me out

Thanks in advance
Renjesh

Last edited by Franklin52; 03-15-2011 at 08:14 AM.. Reason: Please use code tags
# 2  
Old 03-15-2011
I think if [[ -d $DIR/$i ]] should be if [[ -d $i ]]
You are having absolute paths in array element.

Or, do you mean to say you are having structure like this..
Code:
/a/b/c/etc/src/esr/11.2/aix
# where /a/b/c is the value of $DIR

In any case,Why do you need to do mkdir $DIR?
have a look at mkdir -p

I guess you need to backup your "files" in another location with the same directory structure.
If yes, get ONLY file list (not dirs) in array or in a file and try something like..

Code:
for i in ....
do
 dest=/path/prefix/${i%/*}
 mkdir -p $dest || echo "unable to create dir $dest"
 cp $i $dest
done

# 3  
Old 03-18-2011
DIR :- location form there i want to take backup(both files and directory needed)

DEST :- Location where i need the backed files

What ever is there in Array, i need to copy whether it is a file or directory. If it is a files, i need the content also .

Thanks
Renjesh Raju
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating new directories

I want to create a new system of directory structure for example mkdir -p ./iv.sac/resu/hhz.d However, I think that `mkdir -p` overwrites the directories. I want to avoid that and am doing something as follows in my bash script echo -e "\nCreating directories:" ... (6 Replies)
Discussion started by: kristinu
6 Replies

2. Shell Programming and Scripting

Problem creating a tar ball in different directories

Hi all. I'm hitting a problem creating a tar archive in one directory from files located in a different directory. It fails when I replace the absolute paths with variables in the script but works if I just run tar on the cmdln. E.g. #!/bin/ksh BASE=$PWD STAGE=$BASE/stage LOG=$BASE/log... (4 Replies)
Discussion started by: user052009
4 Replies

3. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

4. Shell Programming and Scripting

need help with creating directories and variables

i'm trying to write a script that has 2 variables, and uses the 1st variable as a number and the 2nd a name to create directories. so if you typed in ./myscript 5 week, it would create 5 directories named week1 - week5. whenever i run this, i get an error message saying week5 already exists, so i... (3 Replies)
Discussion started by: layne2kim
3 Replies

5. Homework & Coursework Questions

Creating directories within a directory?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Ok i need to create a directory within another directory in one command. I'm already in a directory to. I need to... (6 Replies)
Discussion started by: gangsta
6 Replies

6. Shell Programming and Scripting

Creating Directories

I have many pdf files with similar naming conventions as this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. It is a pdf file containing evaluations for these locations: AC41, AC85, AC86, AC128, AC129, MC171, and MC173. I want to create a directory for every location and put the... (3 Replies)
Discussion started by: ndnkyd
3 Replies

7. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

8. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

9. HP-UX

creating directories on the same box

I have two login accounts 'fup04a' and 'fup04c' on the same unix box after log in using 'fup04a' and if I try creating a directory 'new' drwxrwxr-x 2 fup04a fup04a 4096 Nov 14 14:06 new but if i try the same with fup04c drw-rw-r-x 2 fup04c fup04c 4096 Nov 14... (3 Replies)
Discussion started by: vivek_damodaran
3 Replies

10. UNIX for Dummies Questions & Answers

creating directories

how do i create directories using command in my unix system, i could do with knowing how to do it with absolute pathnames and relative pathnames please (3 Replies)
Discussion started by: carlvernon
3 Replies
Login or Register to Ask a Question