How to Find File with Name, Extension, Size and Date in directory on Windows/Mac
Super Fast Free command line Software to find files in directory and subdirectories - freeware fd
Free Download (Windows, Mac and Linux)
For 64-bit system, download fd-..-x86_64..zip
For 32-bit system, download fd-..-i686..zip
Unzip it and put the binary file fd (fd.exe on Windows) in a folder which in the PATH environment variable
Usage of Fast File Find fd
$ fd --help
fd 7.4.0
USAGE:
fd [FLAGS/OPTIONS] [<pattern>] [<path>...]
FLAGS:
-H, --hidden
Include hidden directories and files in the search results (default: hidden files and directories are
skipped). Files and directories are considered to be hidden if their name starts with a `.` sign (dot).
-I, --no-ignore
Show search results from files and directories that would otherwise be ignored by '.gitignore', '.ignore' or
'.fdignore' files
--no-ignore-vcs
Show search results from files and directories that would otherwise be ignored by '.gitignore' files
-s, --case-sensitive
Perform a case-sensitive search. By default, fd uses case-insensitive searches, unless the pattern contains
an uppercase character (smart case).
-i, --ignore-case
Perform a case-insensitive search. By default, fd uses case-insensitive searches, unless the pattern
contains an uppercase character (smart case).
-g, --glob
Perform a glob-based search instead of a regular expression search
--regex
Perform a regular-expression based search (default). This can be used to override --glob
-F, --fixed-strings
Treat the pattern as a literal string instead of a regular expression
-a, --absolute-path
Shows the full path starting from the root as opposed to relative paths
-L, --follow
By default, fd does not descend into symlinked directories. Using this flag, symbolic links are also
traversed
-p, --full-path
By default, the search pattern is only matched against the filename (or directory name). Using this flag,
the pattern is matched against the full path
-0, --print0
Separate search results by the null character (instead of newlines). Useful for piping results to 'xargs'.
--show-errors
Enable the display of filesystem errors for situations such as insufficient permissions or dead symlinks
-h, --help
Prints help information
-V, --version
Prints version information
OPTIONS:
-d, --max-depth <depth>
Limit the directory traversal to a given depth. By default, there is no limit on the search depth
-t, --type <filetype>...
Filter the search by type (multiple allowable filetypes can be specified):
'f' or 'file': regular files
'd' or 'directory': directories
'l' or 'symlink': symbolic links
'x' or 'executable': executable
'e' or 'empty': empty files or directories
-e, --extension <ext>...
(Additionally) filter search results by their file extension. Multiple allowable file extensions can be
specified
-x, --exec <cmd>
Execute a command for each search result
All arguments following --exec are taken to be arguments to the command until the argument ';' is
encountered
Each occurrence of the following placeholders is substituted by a path derived from the current search
result before the command is executed:
'{}': path
'{/}': basename
'{//}': parent directory
'{.}': path without file extension
'{/.}': basename without file extension
-X, --exec-batch <cmd>
Execute a command with all search results at once
All arguments following --exec-batch are taken to be arguments to the command until the argument ';' is
encountered
A single occurrence of the following placeholders is authorized and substituted by the paths derived from the
search results before the command is executed:
'{}': path
'{/}': basename
'{//}': parent directory
'{.}': path without file extension
'{/.}': basename without file extension
-E, --exclude <pattern>...
Exclude files/directories that match the given glob pattern. This overrides any other ignore logic. Multiple
exclude patterns can be specified
--ignore-file <path>...
Add a custom ignore-file in '.gitignore' format. These files have a low precedence
-c, --color <when>
Declare when to use color for the pattern match output:
'auto': show colors if the output goes to an interactive console (default)
'never': do not use colorized output
'always': always use colorized output
-j, --threads <num>
Set number of threads to use for searching & executing (default: number of available CPU cores)
-S, --size <size>...
Limit results based on the size of files using the format <+-><NUM><UNIT>.
'+': file size must be greater than or equal to this
'-': file size must be less than or equal to this
'NUM': The numeric size (e.g. 500)
'UNIT': The units for NUM. They are not case-sensitive
Allowed unit values:
'b': bytes
'k': kilobytes
'm': megabytes
'g': gigabytes
't': terabytes
'ki': kibibytes
'mi': mebibytes
'gi': gibibytes
'ti': tebibytes
--changed-within <date|dur>
Filter results based on the file modification time. The argument can be provided as a specific point in time
(YYYY-MM-DD HH:MM:SS) or as a duration (10h, 1d, 35min). '--change-newer-than' can be used as an alias
Examples:
--changed-within 2weeks
--change-newer-than '2018-10-27 10:00:00'
--changed-before <date|dur>
Filter results based on the file modification time. The argument can be provided as a specific point in time
(YYYY-MM-DD HH:MM:SS) or as a duration (10h, 1d, 35min). '--change-older-than' can be used as an alias
Examples:
--changed-before '2018-10-27 10:00:00'
--change-older-than 2weeks
--path-separator <separator>
Set the path separator to use when printing file paths. The default is the OS-specific separator ('/' on
Unix, '\' on Windows).
--search-path <search-path>...
Provide paths to search as an alternative to the positional <path> argument. Changes the usage to `fd
[FLAGS/OPTIONS] --search-path <path> --search-path <path2> [<pattern>]`
ARGS:
<pattern>
the search pattern: a regular expression unless '--glob' is used (optional)
<path>...
The directory where the filesystem search is rooted (optional). If omitted, search the current working
directory
Note: `fd -h` prints a short and concise overview while `fd --help` gives all details
How to find files with Name on Windows/Mac/Linux
Start a Command line terminal and enter command fd part_of_file_name
$ fd free
src\win\a-free-media-splitter
src\win\a-free-media-splitter\img\easiest-free-video-splitter.png
src\win\a-free-steganography-tool
src\win\a-free-video-end-cropper
src\win\a-free-video-end-cropper\img\easiest-free-crop-end-of-video-audio.png
src\z\eula-freeware.md
fd recursive search path starts from the current directory or the specified directory. If the last part contains string free
, it will capture the path as a record. Here the folder src\win\a-free-media-splitter
is a record because the last part contains the string free
, the file src\win\a-free-media-splitter\img\easiest-free-video-splitter.png
is a record too
Filter out directories from file search results, and now when the last part of the path(file name) contains the string free, then the paths will be captured:
$ fd --type file free
src\win\a-free-media-splitter\img\easiest-free-video-splitter.png
src\win\a-free-video-end-cropper\img\easiest-free-crop-end-of-video-audio.png
src\z\eula-freeware.md
How to command line find files with specified file extensions
Find files with the md
extension and the last part of path contains free
:
$ fd --type file --extension md free
src\z\eula-freeware.md
How to command line find files with size on Windows/Mac/Linux
Syntax:
-S, --size <size>...
Limit results based on the size of files using the format <+-><NUM><UNIT>.
'+': file size must be greater than or equal to this
'-': file size must be less than or equal to this
'NUM': The numeric size (e.g. 500)
'UNIT': The units for NUM. They are not case-sensitive
Allowed unit values:
'b': bytes
'k': kilobytes
'm': megabytes
'g': gigabytes
't': terabytes
'ki': kibibytes
'mi': mebibytes
'gi': gibibytes
'ti': tebibytes
Find files whose file size must be less than or equal to 3 MB and greater than or equal to 1 MB:
$ fd --size -3m --size +1m
src\win\a-life-is-rotation-timer\dl\setup-life-is-rotation-timer.exe
How to command line search files with date
Syntax:
--changed-within <date|dur>
Filter results based on the file modification time. The argument can be provided as a specific point in time
(YYYY-MM-DD HH:MM:SS) or as a duration (10h, 1d, 35min). '--change-newer-than' can be used as an alias
Examples:
--changed-within 2weeks
--change-newer-than '2018-10-27 10:00:00'
--changed-before <date|dur>
Filter results based on the file modification time. The argument can be provided as a specific point in time
(YYYY-MM-DD HH:MM:SS) or as a duration (10h, 1d, 35min). '--change-older-than' can be used as an alias
Examples:
--changed-before '2018-10-27 10:00:00'
--change-older-than 2weeks
Find files with extension md and changed within 1 hour:
$ fd --type file --extension md --changed-within 1h
src\web\command-line-find-file-with-name-extension-size-in-directory\index.md
Command line find exclude file extensions/types
Syntax:
-E, --exclude <pattern>...
Exclude files/directories that match the given glob pattern. This overrides any other ignore logic. Multiple
exclude patterns can be specified
glob pattern is also called wildcard, and its syntax is similar to .gitignore file, it can match any part of the path, not just the last part
Command line to find files that have changed within 1 day and exclude file extension png:
$ fd --type file --changed-within 1d --exclude '*.png'
src\web\command-line-find-file-with-name-extension-size-in-directory\index.md
src\win\software-to-run-linux-commands-on-windows\index.md
if the name of the directory ends with .png, it is also excluded, because *.png is a glob pattern, not a file extension, and we can verify it by:
$ mkdir test.png && touch test.png/me.md && fd --changed-within 1min --exclude '*png'
src\web\command-line-find-file-with-name-extension-size-in-directory\index.md
I only found the file that is currently be edited (the source of this article)
If I only find the files that have changed within 5 minutes, the result is:
$ fd --type file --changed-within 5min
src\web\command-line-find-file-with-name-extension-size-in-directory\index.md
test.png\me.md
Find files from Command line and compress them to .xz format using tar
The following is an example of find files that I (King Eca) use in real scenarios:
cd ~/mybook/easiestsoft.com
tar --create --xz --file=src.xz $(fd --changed-within 10h --exclude 'how-to/' -E 'zz_*' -E '*.xz' --type file --path-separator '//')
- Find files that changed within 10 hours
- Exclude files whose file path contains the folder name
how-to
- Exclude files whose file path contains the (folder or file) name begin with
zz_
- Exclude files whose file path contains the (folder or file) name end with
.xz
- Pass all found files to tar to create .xz compressed file
I used this code with MSYS2, the tar
program does not recognize the Windows path separator \
, so I used --path-separator '//'
, but why it should be double /
here? I don't know
2019-12-20 King Eca