Quote:> 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
}
}'
Quote:> 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
--
Stphane