|
Home > Archive > Unix Shell > February 2006 > Stripping off path names: "find" and variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Stripping off path names: "find" and variables
|
|
| kk_oop@yahoo.com 2006-02-22, 8:51 pm |
| Hi. I've got two related questions:
1. I would like to use the find command to return a list of files
without the paths attached, i.e., it would just return the file names.
2. Given a variable that contains a list of path/file names, I'd like
to be able to save the string in another variable but with the paths
stripped off.
Thanks for any input!
Ken
| |
| Stachu 'Dozzie' K. 2006-02-22, 8:51 pm |
| On 23.02.2006, kk_oop@yahoo.com <kk_oop@yahoo.com> wrote:
> Hi. I've got two related questions:
>
> 1. I would like to use the find command to return a list of files
> without the paths attached, i.e., it would just return the file names.
If you use GNU find, then look closer at -printf option. Otherwise you
could use sed for removing directories.
> 2. Given a variable that contains a list of path/file names, I'd like
> to be able to save the string in another variable but with the paths
> stripped off.
Use sed.
--
Feel free to correct my English
Stanislaw Klekot
| |
| Xicheng 2006-02-22, 8:51 pm |
| kk_oop@yahoo.com wrote:
> Hi. I've got two related questions:
>
> 1. I would like to use the find command to return a list of files
> without the paths attached, i.e., it would just return the file names.
find -type f -printf "%f\n"
>
> 2. Given a variable that contains a list of path/file names, I'd like
> to be able to save the string in another variable but with the paths
> stripped off.
var1=$(find -type f)
echo "$var1"
var2=$(echo "$var1" | PERL -F/ -alne 'print $F[-1]')
echo "$var2"
#or
var3=$(echo "$var1" | awk -F/ '{print $NF}')
echo "$var3"
#(under GNU bash)
Xicheng
| |
| Stephane CHAZELAS 2006-02-23, 2:54 am |
| 2006-02-22, 17:06(-08), kk_oop@yahoo.com:
> Hi. I've got two related questions:
>
> 1. I would like to use the find command to return a list of files
> without the paths attached, i.e., it would just return the file names.
find . -exec basename {} \;
Or, probably more efficient but less readable:
find .//. -print | awk '
/\/\// && NR > 1 {
sub(/.*\//, "", file)
print file
file = $0
next
}
{file = file "\n" $0}
END {
if (NR > 0) {
sub(/.*\//, "", file)
print file
}
}'
> 2. Given a variable that contains a list of path/file names, I'd like
> to be able to save the string in another variable but with the paths
> stripped off.
[...]
How can a variable contain a list of paths? What would you use
as a separator given that every character accepted in a shell
variable is also allowed in a file path (except for zsh that
accepts the NUL character in its variables), you'd need a way to
escape the separator.
Maybe you're thinking of an array variable. But beware that
arrays are a non standard feature.
What can be done is to have the variable written in the xargs
input format (where backslash can be used to escape the
separators), such as:
var='a/file1 b/file2
c/file\ 3
d/file\
4'
Then, you can do:
printf '%s\n' "$var" | xargs -n1 basename
--
Stéphane
|
|
|
|
|