Extract directory name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract directory name
# 1  
Old 02-01-2015
Extract directory name

Hello guys.
i have several backup servers which i have to monitor them daily.
now i want an script to use the backup directory and put it in output variable.
my backup files look like this:
Code:
[ll -hrt
total 151G
-rw-r----- 1 oracle oinstall  68G Jan 23 00:21 full_ORCL_20150122_7277_1.bck
-rw-r----- 1 oracle oinstall 1.3G Jan 23 00:24 arch_ORCL_20150123_7279_1.bck
-rw-r----- 1 oracle oinstall 444M Jan 24 21:12 level1_ORCL_20150124_7281_1.bck
-rw-r----- 1 oracle oinstall 1.3G Jan 24 21:16 arch_ORCL_20150124_7283_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 24 21:16 c-1229942357-20150124-01
-rw-r----- 1 oracle oinstall 664M Jan 25 21:16 level1_ORCL_20150125_7285_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 25 21:16 c-1229942357-20150125-00
-rw-r----- 1 oracle oinstall 1.2G Jan 25 21:35 arch_ORCL_20150125_7287_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 25 21:35 c-1229942357-20150125-01
-rw-r----- 1 oracle oinstall 870M Jan 26 21:32 level1_ORCL_20150126_7289_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 26 21:32 c-1229942357-20150126-00
-rw-r----- 1 oracle oinstall 1.2G Jan 26 21:42 arch_ORCL_20150126_7291_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 26 21:42 c-1229942357-20150126-01
-rw-r----- 1 oracle oinstall 1.2G Jan 27 21:16 level1_ORCL_20150127_7293_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 27 21:16 c-1229942357-20150127-00
-rw-r----- 1 oracle oinstall 1.2G Jan 27 21:33 arch_ORCL_20150127_7295_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 27 21:33 c-1229942357-20150127-01
-rw-r----- 1 oracle oinstall 1.4G Jan 28 21:31 level1_ORCL_20150128_7297_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 28 21:31 c-1229942357-20150128-00
-rw-r----- 1 oracle oinstall 1.2G Jan 28 21:51 arch_ORCL_20150128_7299_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 28 21:51 c-1229942357-20150128-01
-rw-r----- 1 oracle oinstall  68G Jan 30 07:01 full_ORCL_20150129_7301_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 30 07:02 c-1229942357-20150130-00
-rw-r----- 1 oracle oinstall 1.4G Jan 30 07:05 arch_ORCL_20150130_7303_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 30 07:05 c-1229942357-20150130-01
drwxr-xr-x 2 oracle oinstall  16K Jan 31 21:01 rman
-rw-r----- 1 oracle oinstall 436M Jan 31 21:13 level1_ORCL_20150131_7305_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 31 21:13 c-1229942357-20150131-00
-rw-r----- 1 oracle oinstall 1.4G Jan 31 21:16 arch_ORCL_20150131_7307_1.bck
-rw-r----- 1 oracle oinstall  11M Jan 31 21:17 c-1229942357-20150131-01

as you can see file names contain the date and the type of backup(just level and full are important to me)
now i want return the values like this:
if the backup date belong to today or yesterday and backup type was full return 10
if the backup date belong to today or yesterday and backup was level return 5
and if non of them was true return 0.

i have no idea how can:
1- put the files name in a string.
2- how can extract the last backup date and type?
3- how can compare the date with current day?

sorry for asking questions like this. i just start shell scripting but this one is kind of emergency.
thank you so much.
# 2  
Old 02-01-2015
The methods for determining yesterday's date vary from system to system and shell to shell...

What operating system and shell are you using?
What is your timezone setting?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-01-2015
Quote:
Originally Posted by Don Cragun
The methods for determining yesterday's date vary from system to system and shell to shell...

What operating system and shell are you using?
What is your timezone setting?
thank you dear Don.
I'm using red hat. but i don't know what kind of shell I'm using!!
my time zone is: IRST.
date output is: Sun Feb 1 10:08:39 IRST 2015
thank you.
# 4  
Old 02-01-2015
What should your output look like?
Code:
awk -F"_" -vYstd=$(date +"%Y%m%d" -d"-4 day")\
        '$3>Ystd        {if ($1 ~ "full") printf "10"
                         if ($1 ~ "level") printf "5"
                         sub (/^.* /,"")
                         printf "\t%s\n", $0
                        }
        ' file
10   full_ORCL_20150129_7301_1.bck
     arch_ORCL_20150130_7303_1.bck
5    level1_ORCL_20150131_7305_1.bck
     arch_ORCL_20150131_7307_1.bck

My system doesn't recognize the IRST time zone.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-01-2015
Quote:
Originally Posted by RudiC
What should your output look like?
Code:
awk -F"_" -vYstd=$(date +"%Y%m%d" -d"-4 day")\
        '$3>Ystd        {if ($1 ~ "full") printf "10"
                         if ($1 ~ "level") printf "5"
                         sub (/^.* /,"")
                         printf "\t%s\n", $0
                        }
        ' file
10   full_ORCL_20150129_7301_1.bck
     arch_ORCL_20150130_7303_1.bck
5    level1_ORCL_20150131_7305_1.bck
     arch_ORCL_20150131_7307_1.bck

My system doesn't recognize the IRST time zone.
Than you dear Don. as always your post was so helpful.
now i should put the [ll -hrt] result into the output.
am i right?
# 6  
Old 02-01-2015
I'm not sure what [ll -hrt]should do. Any command's result, mayhap ls -l, can be piped into that awk cmd:
Code:
ls -l | awk ' ... '

This User Gave Thanks to RudiC For This Post:
# 7  
Old 02-02-2015
Quote:
Originally Posted by RudiC
I'm not sure what [ll -hrt]should do. Any command's result, mayhap ls -l, can be piped into that awk cmd:
Code:
ls -l | awk ' ... '

thank you dear RudiC.
i use your command and make this script but it wouldn't work.
whould you please take a look at it and tell me where i did wrong?
Code:
output=$(ls -l $1) 
"output" | awk '-F"_" -vYstd=$(date +"%Y%m%d" -d"-4 day")\
        '$3>Ystd        {if ($1 ~ "full") printf "10"
                         if ($1 ~ "level") printf "5"
                         sub (/^.* /,"")
                         printf "\t%s\n", $0
                        }'

I'm so sorry for asking such a stupid question but i really need to make it work until tomorrow.

Last edited by Ymir; 02-02-2015 at 01:57 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract directory path from a parameter

i was attempting to extract a directory path that was passed from a parameter with this code vdir=`dirname $p1` echo current directory $vdir it does not work when the parameter passed has wild card on it. for example $ sh sample1.sh "/sbin/log/c*.log" dirname: extra operand... (2 Replies)
Discussion started by: wtolentino
2 Replies

2. Shell Programming and Scripting

Download and extract to a specific directory

Trying to download and extract a file to a specific folder, but getting an error. What am I doing wrong? Is there a way to rename the download if desired? Thank you :). curl --url https://github.com/arq5x/bedtools2/releases/download/v2.26.0/bedtools-2.26.0.tar.gz | tar -xz --output... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

4. UNIX for Dummies Questions & Answers

Extract directory from full file name?

I think I know what this is doing, but the 'eval' is confusing fname=$(echo ${lineItem} | awk 'BEGIN {FS=";"}{print $1}') fname=${fname%%+(])} fname=${fname##+(])} eval "fname=${fname}" The first line extracts the contents of the line preceeding the ";" 2nd & 3rd lines trim the value (I... (5 Replies)
Discussion started by: jdorn001
5 Replies

5. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

6. Shell Programming and Scripting

Extract directory from a file path

Im trying to extract a directory from a path entered by the user Lets say the path is path=/home/bliss/files/myfile.txt i wanna extract "/home/bliss/files" from $path ... how can i do this? (4 Replies)
Discussion started by: mrudula009
4 Replies

7. UNIX for Advanced & Expert Users

tar extract to different directory

Hi, I created a tar file of a directory dir1 from /tmp in the following way $pwd /tmp $tar -cvf dir1.tar dir1 (dir1 will have say file1) Now i want to extract it in the directory /tmp/dir2 so that the directory dir1 is also created and extracted... (1 Reply)
Discussion started by: ammu
1 Replies

8. UNIX for Dummies Questions & Answers

How to extract archive to a specified directory

Hi, I would like to extract the files from an archive which I have copied from a different server which has different file structures to my server. When I do a tar xvf archive_name, I get the error saying the file or directory cannot be found. How do I specify a desginated directory to... (4 Replies)
Discussion started by: john_trinh
4 Replies

9. UNIX for Advanced & Expert Users

extract a sub directory form a tar file

anyone know if it is possable to extract a subdirectory in a tar file. IE tarfile contains parent dir -sub dir A -sub dir B I want to extract sub dir B. (2 Replies)
Discussion started by: Optimus_P
2 Replies

10. UNIX for Dummies Questions & Answers

redirecting tar extract to another directory

Is it possible to redirect the output from 'tar xvf' to another directory? The taped tar image is extracting to my / dircetory - even though i'm running the command from /backups. The contents list of the tape shows files created from /livebackups/... Thanks Richard (7 Replies)
Discussion started by: colesy
7 Replies
Login or Register to Ask a Question