Stripping off path names: "find" and variables

Stripping off path names: "find" and variables

Post by kk_.. » Fri, 24 Feb 2006 10:06:50



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

 
 
 

Stripping off path names: "find" and variables

Post by Stachu 'Dozzie' K » Fri, 24 Feb 2006 10:22:43



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.

If you use GNU find, then look closer at -printf option. Otherwise you
could use sed for removing directories.

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.

Use sed.

--
Feel free to correct my English
Stanislaw Klekot

 
 
 

Stripping off path names: "find" and variables

Post by Xichen » Fri, 24 Feb 2006 10:42:58



> 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"

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.

    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

 
 
 

Stripping off path names: "find" and variables

Post by Stephane CHAZELA » Fri, 24 Feb 2006 17:47:39



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

 
 
 

1. CygWin - PATH finds "myscript.sh" but not "myscript"

Hi, I'm new to CygWin,  I've written a script called "myscript.sh" and
placed it in a directory in the PATH.  I am able to run the script from
anywhere using the name "myscript.sh"......but I would also like to be able
to run it by just typing "myscript" without the ".sh."

I'm pretty sure this is possible because when working with MKS Toolkit,
which uses a Korn Shell, it worked that way out of the box.  CygWin seems to
have some of this functionality because ".exe" files will work like this.
But I can't figure out what I have to change to get my ".sh" script to work.

Any suggestions?

/\/\ark

P.S. - I'm running CygWin 1.3.12-2 under Windows XP.

2. construct ip packet problems ?

3. named: "found it", ping: "unknown host"

4. deadlocks on slow machine (486)

5. GETSERVBYNAME()????????????????????"""""""""""""

6. msync() bug

7. How to track "Inode", "name" and "buffer" cache rates

8. stdout & stderr redirection

9. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

10. Mac "find" performs far better than Unix O(n^2) "find"

11. stripping "" enclosing text in a variable

12. STARTX: "No Screens Found"/"Can't find Display"

13. set path = "$path" hoses path in tcsh -- why???