OS/Linux

๐Ÿง fd ๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ• (find ๋Œ€์‹  ์ด๊ฑฐ ์“ฐ์ž)

์ธํŒŒ_ 2022. 5. 18. 13:27

fd๋ช…๋ น์–ด-fdfind

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๋ช…๋ น์–ด-fdfind

 

ํŒŒ์ผ ๊ฒ€์ƒ‰

$ fd <ํŒŒ์ผ๋ช…> # ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ํ•ด๋‹น ํŒŒ์ผ๋ช…์„ ์ฐพ๋Š”๋‹ค.

$ fd <ํŒŒ์ผ๋ช…> <๊ฒฝ๋กœ> # ํ•ด๋‹น ๊ฒฝ๋กœ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ํ•ด๋‹น ํŒŒ์ผ๋ช…์„ ์ฐพ๋Š”๋‹ค.

 

ํŒŒ์ผ ํ™•์žฅ์ž ํ•„ํ„ฐ๋ง

$ fd -e png # ํŒŒ์ผ ํ™•์žฅ๋ช… ํ•„ํ„ฐ๋ง
$ fd -e png 2022-04 # ํŒŒ์ผ ํ™•์žฅ๋ช… ํ•„ํ„ฐ๋งํ•˜๋Š”๋ฐ 2022-04 ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ„ ํŒŒ์ผ๋ช…๋งŒ ์ƒ‰์ธ

$ fd -e sh test / # ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ์‰˜์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๊ฒ€์ƒ‰ํ•˜๋Š”๋ฐ ํŒŒ์ผ๋ช… ์ด๋ฆ„์— test๊ฐ€ ๋“ค์–ด๊ฐ„ ํŒŒ์ผ๋งŒ

fd๋ช…๋ น์–ด-fdfind
fd๋ช…๋ น์–ด-fdfind

 

๊ฒ€์ƒ‰ ์ œ์™ธ (-E)

$ fd -E <๊ฒ€์ƒ‰์— ์ œ์™ธํ•  ๋””๋ ‰ํ† ๋ฆฌ๋ช…> # ๋””๋ ‰ํ† ๋ฆฌ๋ช…์„ ์ œ์™ธํ•œ ํŒŒ์ผ ๊ฒ€์ƒ‰

 

์ •๊ทœ์‹ ๊ฒ€์ƒ‰

fd๋Š” Rust์–ธ์–ด๋กœ ๊ฐœ๋ฐœ๋œ ๋ช…๋ น์–ด ํ”„๋กœ๊ทธ๋žจ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ •๊ทœ์‹ ๋ฌธ๋ฒ•์€ Rust ์˜ ๋ฌธ๋ฒ•์„ ๋”ฐ๋ฅธ๋‹ค.

$ fd '^n.*conf$'

fd๋ช…๋ น์–ด-fdfind

 

๊ธ€๋กœ๋ฒŒ ํŒจํ„ด ๊ฒ€์ƒ‰

-g, --glob ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฒฝ๋กœ์—์„œ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ํŒŒ์ผ์„ ์•Œ์ˆ˜ ์žˆ๋‹ค.

$ fd -g '*.png' # ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ png ํŒŒ์ผ ๊ฒ€์ƒ‰

fd๋ช…๋ น์–ด-fdfind

 

์‚ฌ์ด์ฆˆ ๊ฒ€์ƒ‰

-S, --size ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด์„œ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

$ fd -S +1M # 1๋ฉ”๊ฐ€๋ฐ”์ดํŠธ ์ด์ƒ์˜ ํŒŒ์ผ๋งŒ ๊ฒ€์ƒ‰

fd๋ช…๋ น์–ด-fdfind

 

๊ฒ€์ƒ‰ ํŒŒ์ผ ์ถ”๊ฐ€ ๋ช…๋ น ์‹คํ–‰ (exec)

๊ธฐ์กด find ๋ช…๋ น์–ด์—๋Š” -exec ์˜ต์…˜๊ณผ {}, \ ๋ฌธ์ž๋ฅผ ์“ฐ๋ฉด, ๊ฒ€์ƒ‰ํ•  ํŒŒ์ผ์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ๋ช…๋ น์‹คํ–‰์„ ํ• ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

fd๋ช…๋ น์–ด-fdfind

 

๋‹น์—ฐํžˆ 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 ์•„๋ž˜์—์„œ ๋ณ€๊ฒฝ์ผ์ด ํŠน์ • ๊ธฐ๊ฐ„ ์‚ฌ์ด์ธ ํŒŒ์ผ๋“ค์„ ์ „์ฒด ๊ฒ€์ƒ‰

fd๋ช…๋ น์–ด-fdfind


์ด์™ธ์˜ ๋ชจ๋˜ ๋ฆฌ๋ˆ…์Šค ๋ช…๋ น์–ด ๋ชจ์Œ

 

[Modern Linux] ๐Ÿง ๋ชจ๋˜ ๋ฆฌ๋ˆ…์Šค - ์ตœ์‹ ์‹ ๋ฆฌ๋ˆ…์Šค ๋ช…๋ น์–ด ๋ชจ์Œ

๋ชจ๋˜ ๋ฆฌ๋ˆ…์Šค/์œ ๋‹‰์Šค ๋ช…๋ น์–ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ˆ…์Šค ๊ฐ•์˜๋‚˜ ์ˆ˜์—…์—์„œ๋Š” ์ดˆ์ฐฝ๊ธฐ๋ถ€ํ„ฐ ์žˆ๋˜ ์ „ํ†ต์ ์ธ CLI ๋ช…๋ น์–ด(ls, cd, pwd, cat, cp, mv, rm, mkdir, ...๋“ฑ) ์œ„์ฃผ๋กœ ์•Œ๋ ค์ค€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ์˜ค๋ž˜๋œ ๋ช…๋ น์–ด๋“ค์€ ์ž‘์„ฑ๋œ

inpa.tistory.com