(개발)자잘한 팁/Linux (터미널)

Linux - 특정파일을 제외한 후 명령어 실행

yuiee 2022. 11. 28. 09:34

이 코드를 작성하기 위해 찾아봤습니다.

find *.c *.h ! -name main.c -exec norminette {} \;

코드에 대한 설명

  • .c .h라는 파일에 대해서 main.c를 제외하고
  • find : 파일, 디렉토리 등을 찾는 명령어.
  • -name : 뒤에 오는 이름 형식에 맞으면 true 반환 *.c와 같은 shell 패턴이 적용될 수 있음. 특정 이름 형식을 제외하고 싶으면 -name 앞에 !붙이면 됩니다.
  • -exec : -exec 앞의 명령어가 true값이면, 뒤에오는 argument (여기서는 norminette {}) 뒤에 오는 ,{}은 각각의 파일 경로 이름입니다. (현재 디렉토리에 속하면 단순히 파일이름만 들어가게 됩니다.)
  • norminette은 제가 속해 있는 곳에서 쓰는 명령어이기 때문에 신경쓰지 않으셔도 됩니다.

man find로 옵션에 대해서 자세히 알아보기

  • 저는 linux 특정파일 제외 이런식으로 검색 후, 자세한 옵션은 터미널에 man find를 입력해서 옵션을 알아봤습니다.
  • 제가 알아본 옵션들은 밑에 첨부해 두었습니다.
     -name pattern
             True if the last component of the pathname being examined matches
             pattern.  Special shell pattern matching characters (``['',
             ``]'', ``*'', and ``?'') may be used as part of pattern.
     -type t
             True if the file is of the specified type.  Possible file types
             are as follows:

             b       block special
             c       character special
             d       directory
             f       regular file
             l       symbolic link
             p       FIFO
             s       socket
      -exec utility [argument ...] ;
             True if the program named utility returns a zero value as its
             exit status.  Optional arguments may be passed to the utility.
             The expression must be terminated by a semicolon (``;'').  If you
             invoke find from a shell you may need to quote the semicolon if
             the shell would otherwise treat it as a control operator.  If the
             string ``{}'' appears anywhere in the utility name or the argu-
             ments it is replaced by the pathname of the current file.
             Utility will be executed from the directory from which find was
             executed.  Utility and arguments are not subject to the further
             expansion of shell patterns and constructs.

     -exec utility [argument ...] {} +
             Same as -exec, except that ``{}'' is replaced with as many path-
             names as possible for each invocation of utility.  This behaviour
             is similar to that of xargs(1).
  • 메뉴얼에는 예시도 나와있는데, 이를 통해 더 빠르게 습득할 수도 있습니다.

    EXAMPLES
       The following examples are shown as given to the shell:
    
       find / \! -name "*.c" -print
               Print out a list of all the files whose names do not end in .c.
    
       find / -newer ttt -user wnj -print
               Print out a list of all the files owned by user ``wnj'' that are
               newer than the file ttt.
    
       find / \! \( -newer ttt -user wnj \) -print
               Print out a list of all the files which are not both newer than
               ttt and owned by ``wnj''.
    
       find / \( -newer ttt -or -user wnj \) -print
               Print out a list of all the files that are either owned by
               ``wnj'' or that are newer than ttt.
    
       find / -newerct '1 minute ago' -print
               Print out a list of all the files whose inode change time is more
               recent than the current time minus one minute.
    
       find / -type f -exec echo {} \;
               Use the echo(1) command to print out a list of all the files.
    
       find -L /usr/ports/packages -type l -exec rm -- {} +
               Delete all broken symbolic links in /usr/ports/packages.
    
       find /usr/src -name CVS -prune -o -depth +6 -print
               Find files and directories that are at least seven levels deep in
               the working directory /usr/src.
    
       find /usr/src -name CVS -prune -o -mindepth 7 -print
               Is not equivalent to the previous example, since -prune is not
               evaluated below level seven.