Skip to main content

Some shell magic

·177 words·1 min
Zsh Shell

I just ran:

find * -type f -exec echo "{}" \; -exec awk "NR==26" {} \;

Which I adapted from this StackOverflow post and, tldr, searches all files in my current directory (and subdirectories) and tells me what’s written on line 26.

Or, parsing it out:

  • find: “Find files or directories under the given directory tree, recursively.” ( - tldr)
  • *: Anything in the current directory.
  • -type f: A file (not a directory).
  • -exec: A flag in find which will run a shell command. If you include {} in that shell command, it will replace it with the “found” filename (or directory).
  • echo: Print to stdout.
  • "{}": The current filename.
  • \;: End of that command.
  • -exec: Yo, you can chain these guys!
  • awk: “A versatile programming language for working on files.” ( - tldr) AKA, magic.
  • "NR==26": Magic syntax. Found this via StackOverflow. Line number 26.
  • {}: The current filename (still coming from the find command).
  • \;: The end.

It outputs something like:

file1.txt
README.md
some_subdirectory/file2.txt
          some random text on line 26
some_subdirectory/file3.txt

.some_config_file
  enabled: true

Cool!

Related

Keeping it 100 in the terminal
·762 words·4 mins
Zsh Bash Tutorial
Boy, do I love my dotfiles.
Yet another dotfiles repo
·760 words·4 mins
Tutorial Bash Zsh Cool Tools
marcello
Such zsh merit
·834 words·4 mins
Learning Zsh
I think ‘{x} fu’ is ignorant, so I’ll say ‘{x} merit’ instead.