Extract directory name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract directory name
# 8  
Old 02-02-2015
Hello Ymir,

Could you please try following code and let us know if this helps.
Code:
output=`ls -ltr | awk '{print $NF}' 
for i in ${output[@]}
do
 echo $i | 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
                     }'
done


Thanks,
R. Singh

Last edited by RavinderSingh13; 02-02-2015 at 01:13 AM.. Reason: Added/deleted some spaces
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 02-02-2015
First, never tell us: "i use your command and make this script but it wouldn't work"! Show us the exact output from running the script that explains to us exactly what didn't work. If you don't show us how it isn't working, it makes it a huge guessing game for all of us (especially those of us who are using a different operating system and shell than you're using).

Second, there is a HUGE difference between running a program and feeding its output to awk as RudiC suggested with:
Code:
ls -l | awk ...

and running a program, saving its output in a variable, executing a fixed string, and feeding the results of the command named by that string to awk as your code is doing:
Code:
output=$(ls -l $1) 
"output" | awk ...

Why are you storing the output from ls in a variable.

Third: You have an extraneous single quote before the -F option to awk.

And, fourth: I don't see how this script is going to work if you use ls -l instead of ls without the -l option. (With ls -l output, $1 in awk is never going to match Full nor level.)

Please try running your script more like RudiC suggested:
Code:
ls "$1" | awk -F"_" -v Ystd=$(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
                        }'

This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 02-02-2015
You assigned the ls result to the "output" variable. To get at its contents, the shell needs to expand it: $output, to keep the shell from swallowing redundant spaces etc, quote it: "$output". To pipe it into sth. else, echo it: echo "$output".
Why don't you pipe ls's results immediately, as I alluded in my post?
This User Gave Thanks to RudiC For This Post:
# 11  
Old 02-02-2015
Quote:
Originally Posted by RavinderSingh13
Hello Ymir,

Could you please try following code and let us know if this helps.
Code:
output=`ls -ltr | awk '{print $NF}' 
for i in ${output[@]}
do
 echo $i | 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
                     }'
done

Thanks,
R. Singh
Thank you dear Singh.
but your command doesn't work.

Quote:
Originally Posted by Don Cragun
First, never tell us: "i use your command and make this script but it wouldn't work"! Show us the exact output from running the script that explains to us exactly what didn't work. If you don't show us how it isn't working, it makes it a huge guessing game for all of us (especially those of us who are using a different operating system and shell than you're using).

Second, there is a HUGE difference between running a program and feeding its output to awk as RudiC suggested with:
Code:
ls -l | awk ...

and running a program, saving its output in a variable, executing a fixed string, and feeding the results of the command named by that string to awk as your code is doing:
Code:
output=$(ls -l $1) 
"output" | awk ...

Why are you storing the output from ls in a variable.

Third: You have an extraneous single quote before the -F option to awk.

And, fourth: I don't see how this script is going to work if you use ls -l instead of ls without the -l option. (With ls -l output, $1 in awk is never going to match Full nor level.)

Please try running your script more like RudiC suggested:
Code:
ls "$1" | awk -F"_" -v Ystd=$(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
                        }'

dear Don . i use the ssh to get the directory info. so i had to run and put it in a variable.
i use your shell command and it work but not the way i want it.
result:
Code:
        arch_ORCL_20150130_7303_1.bck
        arch_ORCL_20150131_7307_1.bck
        arch_ORCL_20150201_7311_1.bck
5       level1_ORCL_20150131_7305_1.bck
5       level1_ORCL_20150201_7309_1.bck

i want just return a value. your shell script return a value for each directory name.
let me put it this way:
i want check the directory for backup status. valid backup lifetime is less than two days. now
if the script return the "10" i found out i have a full backup which is valid.
if the script return the "5" i found out i have a level backup which is valid.
in other wise i dont have a valid backup file.
Quote:
Originally Posted by RudiC
You assigned the ls result to the "output" variable. To get at its contents, the shell needs to expand it: $output, to keep the shell from swallowing redundant spaces etc, quote it: "$output". To pipe it into sth. else, echo it: echo "$output".
Why don't you pipe ls's results immediately, as I alluded in my post?
thank you dear RudiC.
i use the ssh to get the directory info. so i had to run and put it in a variable.

Last edited by Ymir; 02-02-2015 at 08:07 AM..
# 12  
Old 02-02-2015
I don't see the necessity to use a variable if you run an ssh command. From what you showed in post #1, there is an entire directory listing to be processed, and you did not specify the output format desired.

Please provide the entire picture of what you need to do so we can provide a fit soulition.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 02-02-2015
Quote:
Originally Posted by RudiC
I don't see the necessity to use a variable if you run an ssh command. From what you showed in post #1, there is an entire directory listing to be processed, and you did not specify the output format desired.

Please provide the entire picture of what you need to do so we can provide a fit solution.
Ok. let me explain what exactly i want.
I'm using zenoss to monitor my servers. in the zenoss i cant handle the plain text. i just can use the numerical values. now i want create an alert to warn me if there was no back up files.
so i decided to wrote an script to do this.
now if you guys have better idea please share it with me.
i really appreciate any sort of help.
thank you.
# 14  
Old 02-02-2015
Sorry, I can't imagine the picture from what you post. Wildly guessing: Do you need a script to be run on every server that checks all files and, if a full/level backup found within the last two days, sends a number (10/5) back? How do you intend to send the date parameter if you can handle numbers only? How do you want to store the script?
This User Gave Thanks to RudiC For This Post:
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