Skip to content

Instantly share code, notes, and snippets.

@santisbon
Last active August 4, 2024 06:45

Revisions

  1. santisbon revised this gist Mar 16, 2023. No changes.
  2. santisbon revised this gist Sep 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Find files.md
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --versio
    /usr/local/anaconda3/pkgs/python-3.9.12-hdfd78df_0/bin/python3.9 Python 3.9.12
    ```
    Specifying optional patterns (Requires EXTENDED_GLOB to be set. See below)
    For each file in the Homebrew installation (recursively) named "python", followed by an optional pattern consisting of anything ending in a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links):
    For each file in the Homebrew installation (recursively) named "python", followed by an **optional** pattern consisting of anything ending in a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links):
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
    for file in /opt/homebrew/**/python(*[[:digit:]])#(*); do echo -n $file '\t'; $file --version; done
  3. santisbon revised this gist Sep 6, 2022. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Find files.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,8 @@ Find the broadcast address of your network by searching for the line with "broad
    $ ifconfig | grep broadcast
    ```

    In zsh you can use glob expansion to do something like this:
    ## zsh glob expansion

    Let's say you want to list some python executables and check their version.
    For each file in /usr/ (recursively) named "python", followed by anything, followed by a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    @@ -34,7 +35,7 @@ for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --versio
    /usr/local/anaconda3/pkgs/python-3.10.4-hdfd78df_0/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/pkgs/python-3.9.12-hdfd78df_0/bin/python3.9 Python 3.9.12
    ```
    Specifying optional patterns
    Specifying optional patterns (Requires EXTENDED_GLOB to be set. See below)
    For each file in the Homebrew installation (recursively) named "python", followed by an optional pattern consisting of anything ending in a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links):
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
  4. santisbon revised this gist Sep 6, 2022. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion Find files.md
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,21 @@ for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --versio
    /usr/local/anaconda3/pkgs/python-3.10.4-hdfd78df_0/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/pkgs/python-3.9.12-hdfd78df_0/bin/python3.9 Python 3.9.12
    ```
    Specifying optional patterns
    For each file in the Homebrew installation (recursively) named "python", followed by an optional pattern consisting of anything ending in a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links):
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
    for file in /opt/homebrew/**/python(*[[:digit:]])#(*); do echo -n $file '\t'; $file --version; done

    /opt/homebrew/Caskroom/miniconda/base/bin/python3.9 Python 3.9.12
    /opt/homebrew/Caskroom/miniconda/base/envs/ldm/bin/python3.9 Python 3.9.13
    /opt/homebrew/Caskroom/miniconda/base/pkgs/python-3.9.12-hbdb9e5c_0/bin/python3.9 Python 3.9.12
    /opt/homebrew/Caskroom/miniconda/base/pkgs/python-3.9.13-hc596b02_0_cpython/bin/python3.9 Python 3.9.13
    /opt/homebrew/Caskroom/miniconda/base/python.app/Contents/MacOS/python Python 3.9.12
    /opt/homebrew/Cellar/[email protected]/3.10.6_2/Frameworks/Python.framework/Versions/3.10/bin/python3.10 Python 3.10.6
    /opt/homebrew/Cellar/[email protected]/3.9.13_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 Python 3.9.13
    ```
    Another example.
    For each file in the Hombrew installation (recursively) named "zsh", followed by anything, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
  5. santisbon revised this gist Sep 6, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Find files.md
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ For each file in /usr/ (recursively) named "python", followed by anything, follo
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
    for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --version; done

    /usr/bin/python3 Python 3.8.9
    /usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/bin/python3.9 Python 3.9.13
    /usr/local/anaconda3/bin/python3.9 Python 3.9.12
  6. santisbon revised this gist Sep 6, 2022. 1 changed file with 14 additions and 3 deletions.
    17 changes: 14 additions & 3 deletions Find files.md
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,10 @@ Find the broadcast address of your network by searching for the line with "broad
    $ ifconfig | grep broadcast
    ```

    In zsh you can use glob expansion to do something like this: Let's say you want to list some python executables and check their version.
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    Print the file name (no line breaks), a tab, and then execute the file with the --version option.
    In zsh you can use glob expansion to do something like this:
    Let's say you want to list some python executables and check their version.
    For each file in /usr/ (recursively) named "python", followed by anything, followed by a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
    for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --version; done
    /usr/bin/python3 Python 3.8.9
    @@ -33,6 +34,16 @@ for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --versio
    /usr/local/anaconda3/pkgs/python-3.9.12-hdfd78df_0/bin/python3.9 Python 3.9.12
    ```

    Another example.
    For each file in the Hombrew installation (recursively) named "zsh", followed by anything, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    Print (without line breaks) the file name, a tab, and then execute the file with the --version option.
    ```zsh
    for file in /opt/homebrew/**/zsh*(*); do echo -n $file '\t'; $file --version; done

    /opt/homebrew/Cellar/zsh/5.9/bin/zsh zsh 5.9 (arm-apple-darwin21.3.0)
    /opt/homebrew/Cellar/zsh/5.9/bin/zsh-5.9 zsh 5.9 (arm-apple-darwin21.3.0)
    ```

    Some szh [expansion](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation) options require ```EXTENDED_GLOB``` to be set like this:
    ```Shell
    setopt extendedglob
  7. santisbon revised this gist Sep 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Find files.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ Linux and Cygwin
    ```bash
    find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;
    ```
    macOS.
    macOS
    In [sed](https://www.gnu.org/software/sed/manual/sed.html) the -i option edits files in-place instead of printing to standard output and requires a file extension.
    Use "" to overwrite the file in place.
    ```bash
  8. santisbon revised this gist Sep 6, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Find files.md
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,15 @@ Linux and Cygwin
    ```bash
    find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;
    ```
    OS X.
    macOS.
    In [sed](https://www.gnu.org/software/sed/manual/sed.html) the -i option edits files in-place instead of printing to standard output and requires a file extension.
    Use "" to overwrite the file in place.
    ```bash
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;
    ```
    Find a string in current directory and subdirectories, print line number, exclude some directories.
    ```bash
    grep -rn --exclude-dir={node_modules,amplify} mystring .
    Find a string in current directory and subdirectories, print line number, exclude some directories and files. Redirect output to a file.
    ```Shell
    grep -rn --exclude-dir={node_modules,__pycache__} --exclude={"*.md","LICENSE"} mystring . > ~/results.txt
    ```
    Find the broadcast address of your network by searching for the line with "broadcast" in it:
    ```Shell
  9. santisbon revised this gist Jul 27, 2022. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Find files.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,10 @@ Find a string in current directory and subdirectories, print line number, exclud
    ```bash
    grep -rn --exclude-dir={node_modules,amplify} mystring .
    ```

    Find the broadcast address of your network by searching for the line with "broadcast" in it:
    ```Shell
    $ ifconfig | grep broadcast
    ```

    In zsh you can use glob expansion to do something like this: Let's say you want to list some python executables and check their version.
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
  10. santisbon revised this gist Jul 27, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Find files.md
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,9 @@ grep -rn --exclude-dir={node_modules,amplify} mystring .
    ```


    In zsh you can use glob expansion to do something like this: You want to list the python executables and check their version.
    In zsh you can use glob expansion to do something like this: Let's say you want to list some python executables and check their version.
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    print the file name (no line breaks), a tab, and then execute the file with the --version option.
    Print the file name (no line breaks), a tab, and then execute the file with the --version option.
    ```zsh
    for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --version; done
    /usr/bin/python3 Python 3.8.9
  11. santisbon revised this gist Jul 27, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Find files.md
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ grep -rn --exclude-dir={node_modules,amplify} mystring .


    In zsh you can use glob expansion to do something like this: You want to list the python executables and check their version.
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, expanding only executable simple files (not directories or links) do the following:
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, [expanding](https://thevaluable.dev/zsh-expansion-guide-example/) only executable simple files (not directories or links) do the following:
    print the file name (no line breaks), a tab, and then execute the file with the --version option.
    ```zsh
    for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --version; done
  12. santisbon revised this gist Jul 27, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions Find files.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ Find a string in current directory and subdirectories, print line number, exclud
    grep -rn --exclude-dir={node_modules,amplify} mystring .
    ```


    In zsh you can use glob expansion to do something like this: You want to list the python executables and check their version.
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, expanding only executable simple files (not directories or links) do the following:
    print the file name (no line breaks), a tab, and then execute the file with the --version option.
    @@ -27,4 +28,9 @@ for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --versio
    /usr/local/anaconda3/envs/snowflakes/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/pkgs/python-3.10.4-hdfd78df_0/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/pkgs/python-3.9.12-hdfd78df_0/bin/python3.9 Python 3.9.12
    ```

    Some szh [expansion](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation) options require ```EXTENDED_GLOB``` to be set like this:
    ```Shell
    setopt extendedglob
    ```
  13. santisbon renamed this gist Jul 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. santisbon revised this gist Jul 27, 2022. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions Find and replace.md
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,18 @@ find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;
    Find a string in current directory and subdirectories, print line number, exclude some directories.
    ```bash
    grep -rn --exclude-dir={node_modules,amplify} mystring .
    ```

    In zsh you can use glob expansion to do something like this: You want to list the python executables and check their version.
    For each file in /usr/ (recursively) named "python" followed by anything, followed by a digit, expanding only executable simple files (not directories or links) do the following:
    print the file name (no line breaks), a tab, and then execute the file with the --version option.
    ```zsh
    for file in /usr/**/python*[[:digit:]](*); do echo -n $file '\t'; $file --version; done
    /usr/bin/python3 Python 3.8.9
    /usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/bin/python3.9 Python 3.9.13
    /usr/local/anaconda3/bin/python3.9 Python 3.9.12
    /usr/local/anaconda3/envs/snakes/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/envs/snowflakes/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/pkgs/python-3.10.4-hdfd78df_0/bin/python3.10 Python 3.10.4
    /usr/local/anaconda3/pkgs/python-3.9.12-hdfd78df_0/bin/python3.9 Python 3.9.12
    ```
  15. santisbon revised this gist Jul 27, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Find and replace.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Assuming there's a /test folder with .txt files in it.Note how the * character is escaped to prevent expansion.
    Assuming there's a /test folder with .txt files in it. Note how the * character is escaped to prevent expansion.

    Linux and Cygwin
    ```bash
  16. santisbon renamed this gist Jul 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. santisbon revised this gist Jul 27, 2022. 2 changed files with 16 additions and 9 deletions.
    16 changes: 16 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    Assuming there's a /test folder with .txt files in it.Note how the * character is escaped to prevent expansion.

    Linux and Cygwin
    ```bash
    find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;
    ```
    OS X.
    In [sed](https://www.gnu.org/software/sed/manual/sed.html) the -i option edits files in-place instead of printing to standard output and requires a file extension.
    Use "" to overwrite the file in place.
    ```bash
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;
    ```
    Find a string in current directory and subdirectories, print line number, exclude some directories.
    ```bash
    grep -rn --exclude-dir={node_modules,amplify} mystring .
    ```
    9 changes: 0 additions & 9 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    # Linux and Cygwin
    find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;

    # OS X.
    # The -i option requires a file extension. Use "" to overwrite the file in place.
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;

    # Find a string in current directory and subdirectories, print line number, exclude some directories.
    grep -rn --exclude-dir={node_modules,amplify} mystring .
  18. santisbon revised this gist Nov 18, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -5,5 +5,5 @@ find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;
    # The -i option requires a file extension. Use "" to overwrite the file in place.
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;

    # Find a string in current directory and subdirectories, excluding some directories.
    grep -r --exclude-dir={node_modules,amplify} mystring .
    # Find a string in current directory and subdirectories, print line number, exclude some directories.
    grep -rn --exclude-dir={node_modules,amplify} mystring .
  19. santisbon revised this gist Nov 18, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,7 @@ find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;

    # OS X.
    # The -i option requires a file extension. Use "" to overwrite the file in place.
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;

    # Find a string in current directory and subdirectories, excluding some directories.
    grep -r --exclude-dir={node_modules,amplify} mystring .
  20. asantisbon created this gist Apr 1, 2013.
    6 changes: 6 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # Linux and Cygwin
    find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;

    # OS X.
    # The -i option requires a file extension. Use "" to overwrite the file in place.
    find /test -name \*.txt -exec sed -i "" 's/oldValue/newValue/g' {} \;