Create directories from a file list.
Hi All,
I am trying to write a script to pick the directory name from a list of file. Here is a detailed picture. Have a file name LIST which contains the follwing for example /apps/oracle/product/test1 /apps/oracle/product/test2 /apps/oracle/product/test3 I need a script that reads these line from LIST and creates folders in /apps/oracle/product/test1 /backup/date/test1 after reading the first line /backup/date/test2 after readin the second line /backup/date/test3 and so on. I am very new to scripting and seek all you r help. Thanks. |
this smells like homework so what have you tried so far ?
i would look into programs like basename, cut, mkdir ... edit: i googled 'bash read line' and the first link is: http://www.linuxquestions.org/questi...84/#post714625 |
Hello bv2925, welcome to LQ,
you can use the manpages, execute (for example) the command Code:
man mkdir Markus |
OK, I'm feeling generous today -- here's a bare-bones shell program that will do what you want:
Code:
#!/bin/bash Hope this helps some. |
Hi schneidz
i have no clue about sctipting but somehow managed to write a small script to cat the file and mkdir a new file. I do not know where to start to just picking the file name in the end and create a new directory. Would be helpful if you can provide an example. |
Here's another shot at it (with lots of displays of what it's doing).
Let's assume that the last element of your input list is a file name (rather than a directory name): Code:
#!/bin/bash Code:
mkdir -p ${string} |
Hi Tronayne;
I have made some modifications to your script and this is the error i get. Can you please guide me as to where i am going wrong. + read -u 3 fname ++ echo /apps/hyperion/archivefiles/Sa_Xchgrate ++ sed 's#.*/\(test[^/]*\).*#\1#' + file=/apps/hyperion/archivefiles/Sa_Xchgrate + echo 'The file name is ' /apps/hyperion/archivefiles/Sa_Xchgrate The file name is /apps/hyperion/archivefiles/Sa_Xchgrate + dirnam=/apps/hyperion/archivefiles + echo 'The directory tree is ' /apps/hyperion/archivefiles The directory tree is /apps/hyperion/archivefiles ++ echo /apps/hyperion/archivefiles ++ sed 's/apps/apps/;s/hyperion/stage;s/archivefiles/misc/;s/2011-07-01/;' sed: -e expression #1, char 33: unknown option to `s' + string= + mkdir -p mkdir: too few arguments Try `mkdir --help' for more information. + cp -pr /apps/hyperion/archivefiles/Sa_Xchgrate cp: missing destination file Try `cp --help' for more information. The modified script is as follows: #!/bin/bash set -x stamp=$(date +%F) list=/apps/hyperion/archivefiles/list echo ${stamp} exec 3< ${list} while read -u 3 fname do file=$(echo ${fname} | sed 's#.*/\(test[^/]*\).*#\1#') echo "The file name is " ${file} dirnam=${fname%/*} echo "The directory tree is " ${dirnam} string=`echo ${dirnam} | sed 's/apps/apps/;s/hyperion/stage;s/archivefiles/misc/;s/'${stamp}'/;'` mkdir -p ${string} cp -pr ${string} ${fname} done # close the input file exec 3<&- |
Quote:
Code:
mkdir -p /backup/`date`/`basename /apps/oracle/product/test1` edit: ask your teacher about `backticks`. |
OK, let's back up here and take another look at what it is we're trying to accomplish.
You have a list of files or a list of directories (I'm not clear on which) that you want to back up to a different directory. The first thing you need to do is isolate the last element (that is either a file name or a directory name, but that doesn't matter). There are a number of ways to do that, one of which as suggested by others, is to use the basename utility; e.g., Code:
basename /apps/oracle/product/test1 Code:
basename /apps/hyperion/archivefiles/Sa_Xchgrate Code:
echo /apps/hyperion/archivefiles/Sa_Xchgrate | cut -d'/' -f5 So, instead of using the fancy-schmancy substring extraction and sed code Code:
sed 's#.*/\(test[^/]*\).*#\1#') Now, you want to have a directory tree, using your original post, something like this Code:
/backup/2011-07-02/Sa_Xchgrate So, we need a data stamp and we get that with Code:
stamp=$(date +%F) Code:
dirnam=/backup/${stamp} Then, because we've isolate the last element of the list -- which is either a file name or a directory name, doesn't matter -- we can use Code:
cp -pr /apps/hyperion/archivefiles/Sa_Xchgrate ${dirnam} That's pretty simple -- if, that is, it's what you really want, eh? So Code:
#!/bin/bash Hope this helps some. |
Hi Tronayne,
Thanks for the script. I have moved a little ahead with this where it creates the directory structure till the date. I would want to describe the scenario so that i could provide an in depth picture of the script. We have a file (List) which has a bunch of folder location names mentioned below. /apps/hyperion/archivefiles/ /apps/hyperion/archivefiles/Demo /apps/hyperion/archivefiles/Sample /apps/hyperion/archivefiles/Sa_Xchgrate /apps/hyperion/archivefiles/list The above files (each one) have a different list of filename's under each one which have to be backed up in to the new directories that are being create under the date directory in the backup location. So an example of the contents under Demo is /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Demo.app /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.otl /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.db /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.ind /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.esm /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.tct So the script has to read filename (List), Create a new directory (Demo), read filelist from demo(Demo.app,Basic.otl etc) and backup them up to the new backup location ex: /backup/Date/Demo/{Demo.app,Basic.otl etc} Hope I am clear. Thanks. |
The only important thing you need to know is the last element in you list; that will be the name of the directory you want to back up and it is extracted with the basename utility as in the shell program above. That is, you do not need to know the names of files in the directory, you only need the directory name because cp -pr does two things, -p preserves mode, ownership, timestamps and -r copy directories recursively. And, if you want to watch it do it, replace -pr with -prv.
The other only thing you have to do is edit the shell program and make Code:
# If you have one line in your file "List," say, /apps/hyperion/archivefiles/Demo and execute the shell program, you'll get a backup of the entire Demo tree (you do not need to enter anything else in List. If the content of List were to be Code:
/apps/hyperion/archivefiles/Demo Code:
/backup/${stamp}/Demo That ought to do it -- try it. Hope this helps some. |
Quote:
here is my attempt to understand what you need: Code:
[schneidz@hyper bv]$ cat demo.lst s. henry |
Hi schneidz
Thanks for the input but i am unable to achive what i am supposed to do. Here is the scenario in detail. I have a folder under which three files exist as listed below. The number of files will change depending on the number of applications. $ cd /apps/hyperion/archivefiles/ $ ls Demo Sample Sa_Xchgrate Each of these have a list of files to be backed up with there full path as shown below. $ cat Demo /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Demo.app /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.otl /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.db /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.ind /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.esm /apps/hyperion/products/Essbase/EssbaseServer/app/Demo/Basic/Basic.tct I have to write a program to read Demo, Create a new directory Demo under mkdir -p ${Backuplocation}/${DATE} and backup the files listed in demo Ex:Demo.app, Basic.otl etc into the new location ${Backuplocation}/${DATE}/Demo/ The number of files may change during the course of time. Next read Sample and repeat the procedure untill all the files in /apps/hyperion/archivefiles/ have been read and backed up. in the end tar ${Backuplocation}/${DATE}. Thanks in advance. |
Hi Schneidz,
I have set the Variables as mentioned below. set -x DATE=`date +"%d-%b-%Y"` Log='/apps/stage/misc/bkuptest/backup.log' #Location of the log file Backuplocation='/apps/stage/misc/bkuptest/backup' BACKUP_DIRECTORY=${Backuplocation}/${DATE} Archivelist='/apps/hyperion/archivefiles/' Thanks. |
Quote:
date +"%Y-%m-%d" date +"%Y-%j" most of your criteria is fairly elemental, at this point you just need to combine those conditions in an automated fashion using the variables you set above. this should get you started: Code:
[schneidz@hyper bv]$ mkdir -p $BACKUP_DIRECTORY/demo; cat /apps/hyperion/archivefiles/demo | while read line; do cp "$line" $BACKUP_DIRECTORY/demo; done |
All times are GMT -5. The time now is 03:57 PM. |