Switching between directories and mkdir/copy dir/file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switching between directories and mkdir/copy dir/file
# 1  
Old 04-11-2018
Switching between directories and mkdir/copy dir/file

I was trying to copy the files inside the path /home/user/check/Q1/dir/folder1/expected/n/a1.out1 and a1.out2 and a1.out3 to /home/user/check/Q2/dir/folder1/expected/n/

if n directory is not present at Q2/dir/folder1/expected/ then directory should be created first. And, script follow the config_file to switch between directories and files.


config_File:

Code:
cd dir/folder1
test: a1
test: a2
test: a3
cd dir/folder2
test: b1
test: b2
test: b3
cd dir/folder3
test: c1
test: c2
test: c3


Please suggest.

Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags as required by forum rules!

Last edited by RudiC; 04-12-2018 at 03:48 AM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 04-12-2018
Any attempts / ideas / thoughts from your side?
# 3  
Old 04-12-2018
Hi RudiC,

I have written a code to serve the purpose but looking for a more robust way to deal with it.


Code:
$ cat testscript.sh

#!/bin/bash

filename="config_file"


#!/bin/bash

filename="config_file"

destdir="/scratch_b/mannu/check/Q2"

#While loop to read line by line
while IFS= read -r line; do

    if [[ $line == cd* ]] ; then
       cdvar=`echo $line | awk '{print $2}'`
    fi

      echo "echo cdvar: $cdvar"

    if [[ $line == test* ]] ; then

        test=`echo $line | awk -F ':' '{print $2}'|sed -e 's/^[ \t]*//'`

        echo "test : $test"

        destfinal=`echo "$destdir/$cdvar/expected/n"`

        echo "destfinal: $destfinal"

        if [[ ! -e $destfinal ]]; then
           mkdir -p $destfinal
           echo " copy test : $cdvar/expected/n/$test"

           cp $cdvar/expected/n/$test $destdir/$cdvar/expected/n/
        else
           echo " copy test 2: $cdvar/expected/n/$test"

           cp $cdvar/expected/n/$test $destdir/$cdvar/expected/n/
       fi

    fi

done < "$filename"

# 4  
Old 04-12-2018
So you have a working solution? From a high level glimpse at it, I can't see an immediate reason for a failure. So - what's the problem with it? Where is it not robust enough?

Of course, there are some opportunities...
# 5  
Old 04-12-2018
Like if something gonna change in config_file like commenting any test or anything has to be written in the line apart from cd and test. Yes, for now, it's working but I'm looking for the solution/suggestion to make it stable for any future changes in config_file.

For example- config_file include commenting "#" and testfilter - they should not be processed

Code:
cd dir/folder1
test: a1
#test: a2
test: a3
testfilter: any node01
test: a4
testfilter:
cd dir/folder2
test: b1
test: b2
test: b3
testfilter: any node01
test: b4
testfilter:
cd dir/folder3
test: c1
test: c2
test: c3


Last edited by Mannu2525; 04-12-2018 at 06:30 AM..
# 6  
Old 04-12-2018
OK; how about - assuming you're in the .../check directory -
Code:
while read OP FN REST
  do    case "$OP" in
          \#*)          ;;
          cd)           SD="$FN"
                        TD="Q2/$FN/expected/n"
                        [ -d "$TD" ] ||  mkdir -p "$TD"
                        ;;
          test:)        cp -v Q1/"$SD"/expected/n/"$FN".out[123] "$TD"
                        ;;
          *)            echo "error: OP $OP found but no handler for it."
                        ;;
        esac
  done < config_file

Please add error handling to taste.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create a long list of directories with mkdir?

Hi... Thanks to read this... I want to use mkdir to create many directories listed in a text file, let's say. How do I do this? Sorry for this maybe very basic question :) (13 Replies)
Discussion started by: setub
13 Replies

2. Shell Programming and Scripting

Copy of "How to create a long list of directories with mkdir?"

To bakunin and corona688: My result when text in file is ms_ww_546 ms_rrL_99999 ms_nnn_67_756675 is https://www.unix.com/C:\Users\Fejoz\Desktop\ttt.jpg I hope you can see the picture. There is like a "whitespace character" after 2 of the 3 created directories. ---------- Post... (0 Replies)
Discussion started by: setub
0 Replies

3. UNIX for Dummies Questions & Answers

Issues in Csv file transfer copy from one dir to another

Hi Unix community, I got this code from you guys and I tried to modify it to use for my csv dir transfer basically i want the .csv file to copy itself and populate it to the archive dir. #!/bin/ksh dir1="/home/pumela/unixtestprod" cd "$dir1" echo "code is running" for srcd in... (13 Replies)
Discussion started by: phumaree
13 Replies

4. Shell Programming and Scripting

Make directories containing subdirs using mkdir

Hello everybody, I wonder if there's a way to make a directory that contains multiple subdirectories. For example, if I want to make /home/student under the current working directory, how do I do it? Can I do it using a single mkdir command or do I have make home first, cd into it and then make... (1 Reply)
Discussion started by: Yongfeng
1 Replies

5. Shell Programming and Scripting

Copy files and subdirs from dir to a new dir

Hello Comunity I am trying to make a bash shell script that it copies files and subdirs(with files) to a new dir. I would like the dest_dir to contain only subdirectories with files not other subdirs inside. it called : cpflatdir src_dir dest_dir Pleaze help me! Thank you in... (2 Replies)
Discussion started by: BTKBaaMMM
2 Replies

6. Shell Programming and Scripting

Copy files from input file with dir structure

hi, I want to copy files from source directory based on input file (or output of previous command) and i want to have the SAME DIRECTORY STRUCTURE. Note that i will have other files and directories which i dont want to copy to destination. For example, dir source has following content:... (22 Replies)
Discussion started by: dragon.1431
22 Replies

7. UNIX for Dummies Questions & Answers

Copy dir/file from one place to another.

Hello all. I'm not getting the hang of Paths. I have a dir w/files that I want to copy to another dir. Right now I am in the "source" directory. I want to copy it to Ch7. "cp -r source Ch7". Ch7 was already created. 1st msg.: cannot stat `source`: No such file or dir. I typed pwd & got... (3 Replies)
Discussion started by: Ccccc
3 Replies

8. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies

9. UNIX and Linux Applications

CPIO Problem, copy to the root dir / instead of current dir

HI all, I got a CPIO archive that contains a unix filesystem that I try to extract, but it extract to the root dir / unstead of current dir, and happily it detects my file are newer otherwise it would have overwrited my system's file! I tried all these commands cpio -i --make-directories <... (2 Replies)
Discussion started by: nekkro-kvlt
2 Replies
Login or Register to Ask a Question