map function in shell
The following defines an intuitive "map" function for use in shell scripts or on the command line:
$ alias map="xargs -n1"
This takes a command which will be invoked on every line from stdin. A useful example: finding all directories that contain a certain file:
$ find * -name models.py | map dirname
core
events
teams
vault
Written by Vincent Driessen
Related protips
3 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/49088/21144de45e080759e740c3b5332b98df.jpg)
Erm... use a wildcard in the path argument of find?
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/686/c5a7f21b46df698f3db31c37ed0cf55a.jpeg)
@avdleeuw In this case, I don't want the ./
prefixes on the output, so yes. I wouldn't use them in an uncontrolled environment :)
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/49355/default_profile_0_normal.png)
If you like map, have a look at GNU Parallel https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#