12-01-2008
ksh - walking back up a directory PATH
Hi there,
I am putting a script together to apply a label to a directory structure but it can't be done down the directory structure, i must start at the end and work back and this is what i am struggling with.
for example, i will supply a path as an argument to a script, e.g
dir1/dir2/dir3/dir4/dir5/dir6
i then want to loop round but each time remove the end element of the path e.g
1st pass = dir1/dir2/dir3/dir4/dir5/dir6
2nd pass = dir1/dir2/dir3/dir4/dir5
3rd pass = dir1/dir2/dir3/dir4
4th pass = dir1/dir2/dir3
5th pass = dir1/dir2
6th pass = dir1
The path will not always be 6 entries long though, it could be anywhere in between 2 and 10 entries long.
Has anybody done this before, or give me pointers on which utility within ksh i can use, e.g awk, nawk etc.
many thanks in advance
Steve
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
hiyas
I am trying to get back to the root directory: I went into MAIL directory and now I can't get back to the root directory. What are the commands...
I have '?' coming up and I cannot proceed with this, HELP
Cheers (1 Reply)
Discussion started by: etravels
1 Replies
2. UNIX for Dummies Questions & Answers
Say, I want to move into dir library from current dir, on prompt if I type in
cd li
if it is followed by pressing 'Tab' key then complete dir name appears.
Would there anyone know, how we can make into effect this path autocompletion?
I am using 'ksh'. (5 Replies)
Discussion started by: videsh77
5 Replies
3. Linux
Hey all,
Is there any way out to get/restore the deleted file in Linux ? As we do have Recycle Bin in Windows OS.
as i deleted some files.. it is available in .snapshot
but its a large files.. i cont use "mv" command to copy back.
it there any way .. such like
in window we click restore... (1 Reply)
Discussion started by: mail2sant
1 Replies
4. Shell Programming and Scripting
Hi,
I am trying to figure out if there is a way to make linux take me back to the previous directory I was working in. For instance, I am in
/home/username/directory1
Then if I cd into /home/username/directory1/temp1/temp2/temp3
I would like to immediately go back to the previous... (2 Replies)
Discussion started by: Legend986
2 Replies
5. Shell Programming and Scripting
Hi,
I have a file abcd.txt which has contents in the form of full path file names i.e.
$home> vi abcd.txt
/a/b/c/r1.txt
/q/w/e/r2.txt
/z/x/c/r3.txt
Now I want to retrieve only the directory path name for each row
i.e
/a/b/c/
/q/w/e/
How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies
6. UNIX for Dummies Questions & Answers
Some time ago I was using a linux system and someone had set up and
alias called "back" that got me back to the directory I was last in, which
was very useful. I've now switched to using a mac, and don't have those aliases available. Can anyone tell me how I could make a an alias that
would... (2 Replies)
Discussion started by: ac2011
2 Replies
7. UNIX for Dummies Questions & Answers
~/assign1b/assign1/york/>
your current directory is york
how do you go back to your home directory with using relative pathname?
not using cd alone?
:( (3 Replies)
Discussion started by: blackendstars
3 Replies
8. UNIX for Dummies Questions & Answers
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
9. UNIX for Beginners Questions & Answers
Hello,
I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies
10. UNIX for Beginners Questions & Answers
What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies
LEARN ABOUT DEBIAN
file::dircompare
DirCompare(3pm) User Contributed Perl Documentation DirCompare(3pm)
NAME
File::DirCompare - Perl module to compare two directories using callbacks.
SYNOPSIS
use File::DirCompare;
# Simple diff -r --brief replacement
use File::Basename;
File::DirCompare->compare($dir1, $dir2, sub {
my ($a, $b) = @_;
if (! $b) {
printf "Only in %s: %s
", dirname($a), basename($a);
} elsif (! $a) {
printf "Only in %s: %s
", dirname($b), basename($b);
} else {
print "Files $a and $b differ
";
}
});
# Version-control like Deleted/Added/Modified listing
my (@listing, @modified); # use closure to collect results
File::DirCompare->compare('old_tree', 'new_tree', sub {
my ($a, $b) = @_;
if (! $b) {
push @listing, "D $a";
} elsif (! $a) {
push @listing, "A $b";
} else {
if (-f $a && -f $b) {
push @listing, "M $b";
push @modified, $b;
} else {
# One file, one directory - treat as delete + add
push @listing, "D $a";
push @listing, "A $b";
}
}
});
DESCRIPTION
File::DirCompare is a perl module to compare two directories using a callback, invoked for all files that are 'different' between the two
directories, and for any files that exist only in one or other directory ('unique' files).
File::DirCompare has a single public compare() method, with the following signature:
File::DirCompare->compare($dir1, $dir2, $sub, $opts);
The first three arguments are required - $dir1 and $dir2 are paths to the two directories to be compared, and $sub is the subroutine
reference called for all unique or different files. $opts is an optional hashref of options - see OPTIONS below.
The provided subroutine is called for all unique files, and for every pair of 'different' files encountered, with the following signature:
$sub->($file1, $file2)
where $file1 and $file2 are the paths to the two files. For 'unique' files i.e. where a file exists in only one directory, the subroutine
is called with the other argument 'undef' i.e. for:
$sub->($file1, undef)
$sub->(undef, $file2)
the first indicates $file1 exists only in the first directory given ($dir1), and the second indicates $file2 exists only in the second
directory given ($dir2).
OPTIONS
The following optional arguments are supported, passed in using a hash reference after the three required arguments to compare() e.g.
File::DirCompare->compare($dir1, $dir2, $sub, {
cmp => $cmp_sub,
ignore_unique => 1,
});
cmp By default, two files are regarded as different if their contents do not match (tested with File::Compare::compare). That default
behaviour can be overridden by providing a 'cmp' subroutine to do the file comparison, returning zero if the two files are equal, and
non-zero if not.
E.g. to compare using modification times instead of file contents:
File::DirCompare->compare($dir1, $dir2, $sub, {
cmp => sub { -M $_[0] <=> -M $_[1] },
});
ignore_cmp
If you want to see all corresponding files, not just 'different' ones, set the 'ignore_cmp' flag to tell File::DirCompare to skip its
file comparison checks i.e.
File::DirCompare->compare($dir1, $dir2, $sub,
{ ignore_cmp => 1 });
ignore_unique
If you want to ignore files that only exist in one of the two directories, set the 'ignore_unique' flag i.e.
File::DirCompare->compare($dir1, $dir2, $sub,
{ ignore_unique => 1 });
SEE ALSO
File::Dircmp, which provides similar functionality (and whose directory walking code I've adapted for this module), but a simpler
reporting-only interface, something like the first example in the SYNOPSIS above.
AUTHOR AND CREDITS
Gavin Carr <gavin@openfusion.com.au>
Thanks to Robin Barker for a bug report and fix for glob problems with whitespace.
COPYRIGHT AND LICENSE
Copyright 2006-2007 by Gavin Carr.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl v5.10.1 2010-03-02 DirCompare(3pm)