Basic File Finding & Opening
To find all files that end in .js
in a folder and its subfolders, and then open them in Visual Studio Code, use the following command:
find . -type f -name "*.js" -exec code {} +
Explanation:
find .
- start the search in the current directory (replace.
with the path to your target directory if needed)-type f
- search for files-name "*.js"
- look for files that end with.js
-exec code {} +
- for each found file, execute the code command to open it in Visual Studio Code. The{}
is a placeholder for each found file, and+
at the end optimizes the command by passing all found files to a single code command
Adding Files to Git
To git add
all files that match the find
command for .js
files, use the following command:
git add $(find . -type f -name "*.js")
Explanation:
find . -type f -name "*.js"
- finds all JavaScript files in the current directory and subdirectories$(...)
- this is command substitution, which takes the output of thefind
command and passes it as arguments togit add
Excluding Directories
To find all .js
files while ignoring the node_modules
and .next
directories, and then open them in Visual Studio Code, modify the command above as follows:
find . -type f -name "*.js" -not -path "./node_modules/*" -not -path "./.next/*" -exec code {} +
Explanation:
find .
- starts the search in the current directory-type f
- limits the search to files-name "*.js"
- matches files with the.js
extension-not -path "./node_modules/*"
- excludes any files in thenode_modules
directory-not -path "./.next/*"
- excludes any files in the.next
directory-exec code {} +
- opens all the found files in Visual Studio Code. The{}
is replaced by each file found, and+
ensures all files are passed to a single code command
To exclude multiple common directories at once, chain multiple -not -path
flags:
find . -type f -name "*.js" \
-not -path "./node_modules/*" \
-not -path "./.next/*" \
-not -path "./dist/*" \
-not -path "./build/*" \
-exec code {} +
Finding Files by Multiple Extensions
To find and open files with multiple extensions (for example, both JavaScript and TypeScript files), use the following command:
find . -type f \( -name "*.js" -o -name "*.jsx" -o -name "*.ts" -o -name "*.tsx" \) -exec code {} +
Explanation:
\( ... \)
- groups multiple conditions together, and the backslashes escape the parentheses-o
- means "or", allowing to match any of the specified patterns-name "*.js" -o -name "*.jsx" -o -name "*.ts" -o -name "*.tsx"
- matches files ending with.js
,.jsx
,.ts
, or.tsx
Case-Insensitive Search
To match files regardless of case (for example, .jpg
, .JPG
, or .Jpg
), use -iname
instead of -name
:
find . -type f -iname "*.jpg" -exec code {} +
Explanation:
-iname "*.jpg"
- performs a case-insensitive search, matching any variation of the.jpg
extension
Limiting Search Depth
To only search a specific number of levels deep from the current directory, use the -maxdepth
flag:
find . -maxdepth 2 -type f -name "*.json" -exec code {} +
Explanation:
-maxdepth 2
- only searches 2 levels deep from the current directory, ignoring any subdirectories beyond that depth
Preview Before Taking Action
Before opening or committing files, it's a good idea to preview what the find command will match.
To list all matching files without taking any action:
find . -type f -name "*.py" -not -path "./venv/*"
To count how many files match your search:
find . -type f -name "*.css" | wc -l
Explanation:
| wc -l
- pipes the output of the find command towc -l
, which counts the number of lines (i.e., files)
Using xargs
for Better File Handling
For better handling of filenames with spaces or special characters, use xargs
instead of command substitution:
find . -type f -name "*.md" -print0 | xargs -0 code
Explanation:
-print0
- separates found files with null characters instead of newlines, which safely handles filenames with spaces or special characters| xargs -0
- reads the null-separated input and passes it to the code command
To git add files using xargs
:
find . -type f -name "*.css" -print0 | xargs -0 git add
Troubleshooting
-
"Argument list too long" error
If you have thousands of files and encounter this error, use
xargs
instead of command substitution:find . -type f -name "*.log" | xargs git add
-
Files not opening in the same VS Code window
To reuse an existing VS Code editor instead of opening a new one, add the
-r
flag, which opens files in the most recently used window:find . -type f -name "*.json" -exec code -r {} +