...
fd (최신식 find)
fd 는 file system 에서 특정 엔트리를 찾는 명령어로 기존 find 명령어 보다 더 사용자 친화적이며 속도도 거의 8배로 빠르다.
기본적으로 검색한 파일들에 대한 색상화 출력을 지원하여 가독성을 높여준다.
또한 검색할 때 .gitignore 에 지정한 디렉토리는 자동으로 무시하는 기능도 붙어있다. (무시하지 않도록 하는 설정도 가능)
fd 기본적으로 대소 문자를 구분하지 않는다. 검색 패턴에 대문자가 포함 된 경우 fd 대소 문자 구분 모드로 작동되게 된다.
이처럼 대부분의 경우 사용자 편의성을 선호하는 방식으로 작동되는 최신 명령어이다.
GitHub - sharkdp/fd: A simple, fast and user-friendly alternative to 'find'
A simple, fast and user-friendly alternative to 'find' - GitHub - sharkdp/fd: A simple, fast and user-friendly alternative to 'find'
github.com
fd 설치
$ apt install fd-find
$ ln -s $(which fdfind) ~/.local/bin/fd
우분투로 위의 apt 패키지 인스톨러로 설치하게 되면, 다른 명령어의 충돌때문에 fd 대신 fdfind 으로 명령어를 사용해야 한다.
만일 fd으로 명령어를 사용하고 싶다면, ln -s $(which fdfind) ~/.local/bin/fd 명령을 통해 fd으로 사용자설정이 가능하다.
fd 사용법
$ fd --help # 옵션 전체 보기
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 seach (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': executables
'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 occurence 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.
fd는 기본적으로 대소문자 구분을 하지 않지만 PATTERN 에 대문자가 있으면 대소문자를 구분한다.
파일 리스트 나열
$ fd # 기본적으로 fd만 쓰면 ls 같이 파일들을 나열
파일 검색
$ fd <파일명> # 현재 디렉토리에서 해당 파일명을 찾는다.
$ fd <파일명> <경로> # 해당 경로 디렉토리에서 해당 파일명을 찾는다.
파일 확장자 필터링
$ fd -e png # 파일 확장명 필터링
$ fd -e png 2022-04 # 파일 확장명 필터링하는데 2022-04 문자가 들어간 파일명만 색인
$ fd -e sh test / # 루트 디렉토리에서 쉘스크립트 파일을 검색하는데 파일명 이름에 test가 들어간 파일만
검색 제외 (-E)
$ fd -E <검색에 제외할 디렉토리명> # 디렉토리명을 제외한 파일 검색
정규식 검색
fd는 Rust언어로 개발된 명령어 프로그램이기 때문에, 정규식 문법은 Rust 의 문법을 따른다.
$ fd '^n.*conf$'
글로벌 패턴 검색
-g, --glob 옵션을 사용하면 경로에서 패턴과 일치하는 파일을 알수 있다.
$ fd -g '*.png' # 현재 디렉토리에서 png 파일 검색
사이즈 검색
-S, --size 를 사용하면 크기를 지정해서 검색할 수 있다.
$ fd -S +1M # 1메가바이트 이상의 파일만 검색
검색 파일 추가 명령 실행 (exec)
기존 find 명령어에는 -exec 옵션과 {}, \ 문자를 쓰면, 검색할 파일에 대한 추가 명령실행을 할수 있었다.
당연히 fd에도 -x 나 -exec 옵션을 통해 병렬 명령을 실행 할 수 있다.
$ fd -e jpg -x chmod 644 {} # 확장자가 jpg 인 모든 파일을 찾아 chmod 644 을 실행
$ fd -e zip -x unzip {} # 현재디렉토리에서 zip파일을 찾아 unzip함.
fd는 파일 검색 결과가 들어가는 중괄호 {} 에 대한 추가 옵션들을 제공한다.
- {}: 발견 된 파일의 전체 파일 경로 및 이름
- {/}: 찾은 파일의 파일 이름
- {//}: 찾은 파일이 포함 된 디렉토리
- {/.}: 찾은 파일의 확장자없이 파일 이름
ignore 파일로 검색 제외
fd 는 검색하지 않을 패턴들을 $HOME/.fdignore 파일에 등록해 두면 이 파일들은 검색시 자동으로 제외된다.
마찬가지로 .gitignore 에 등록된 패턴들은 찾지 않게 된다.
따라서 다음과 같이 .fdignore 파일에 등록되어 있다면 검색시 .class 와 .xml 이 아닌 파일만 출력되게 된다.
*.class
*.xml
-I, --no-ignore 옵션를 사용하면 .gitignore 나 .fdignore 에 등록된 파일 패턴도 검색한다.
변경 시간으로 검색
--change-newer-than <기간>를 사용하면 변경일이 특정일 이후인 파일을 찾을 수 있다.
기존 find 명령어의 mtime/mmin 옵션 역할이라고 생각하면 된다.
기간에는 1weeks 나 3months 같이 적어주면 된다.
$ fd --change-newer-than 7days # 현재일 기준 변경일이 7일 이후에 변경된 파일을 검색
$ fd . --change-newer-than '2021-08-23 10:00:00' /tmp # /tmp 디렉토리에서 2021-08-23일 10시 이후에 변경된 파일을 검색
반대로 변경일이 특정일 이전인 파일을 찾을 경우 --change-older-than <기간> 을 사용하면 된다.
$ fd --change-older-than 7days -l # 현재일 기준 변경일이 7일 이전인 파일들을 검색
2가지 옵션을 같이 쓰면 BETWEEN 처럼 사용할 수 있다.
$ fd . --change-newer-than '2021-08-24 04:30:00' --change-older-than '2021-08-25 00:00:00' /tmp # /tmp 아래에서 변경일이 특정 기간 사이인 파일들을 전체 검색
이외의 모던 리눅스 명령어 모음
[Modern Linux] 🐧 모던 리눅스 - 최신식 리눅스 명령어 모음
모던 리눅스/유닉스 명령어 대부분의 리눅스 강의나 수업에서는 초창기부터 있던 전통적인 CLI 명령어(ls, cd, pwd, cat, cp, mv, rm, mkdir, ...등) 위주로 알려준다. 그러나 이 오래된 명령어들은 작성된
inpa.tistory.com
이 글이 좋으셨다면 구독 & 좋아요
여러분의 구독과 좋아요는
저자에게 큰 힘이 됩니다.