Assuming there's a /test folder with .txt files in it. Note how the * character is escaped to prevent expansion.
Linux and Cygwin
find /test -name \*.txt -exec sed -i 's/oldValue/newValue/g' {} \;
macOS
In sed 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.
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 and files. Redirect output to a file.
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:
$ ifconfig | grep broadcast
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 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.
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
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 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.
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
For each file in the Hombrew installation (recursively) named "zsh", followed by anything, expanding 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.
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 options require EXTENDED_GLOB
to be set like this:
setopt extendedglob