파일 이름과 함께 파일 크기를 인쇄하는 find 명령을 어떻게 얻습니까?
다음과 같이 find 명령을 실행하면 :
$ find . -name *.ear
다음과 같이 출력됩니다.
./dir1/dir2/earFile1.ear
./dir1/dir2/earFile2.ear
./dir1/dir3/earFile1.ear
명령 줄에 '인쇄'하고 싶은 것은 이름과 크기입니다.
./dir1/dir2/earFile1.ear 5000 KB
./dir1/dir2/earFile2.ear 5400 KB
./dir1/dir3/earFile1.ear 5400 KB
find . -name '*.ear' -exec ls -lh {} \;
jer.drab.org의 답장에서 추가 된 h. 정신적으로 MB로 변환하는 시간을 절약합니다.)
-exec 또는 -printf를 사용해야합니다. Printf는 다음과 같이 작동합니다.
find . -name *.ear -printf "%p %k KB\n"
-exec는 더 강력하고 임의의 명령을 실행할 수 있도록합니다. 따라서 'ls'또는 'wc'버전을 사용하여 다른 정보와 함께 파일 이름을 인쇄 할 수 있습니다. 'man find'는 printf에 사용할 수있는 인수를 보여 주며, 파일 크기 이상을 수행 할 수 있습니다.
-printf는 공식 POSIX 표준이 아니므로 해당 버전에서 지원되는지 확인하십시오. 그러나 대부분의 최신 시스템은 GNU 찾기 또는 유사한 확장 버전을 사용하므로 구현 될 가능성이 높습니다.
간단한 해결책은 find에서 -ls 옵션을 사용하는 것입니다.
find . -name \*.ear -ls
그러면 일반 "ls -l"형식으로 각 항목이 제공됩니다. 또는 찾고있는 특정 출력을 얻으려면 다음을 수행하십시오.
find . -name \*.ear -printf "%p\t%k KB\n"
파일 이름과 KB 단위의 크기를 제공합니다.
gnu find를 사용하면 이것이 당신이 원하는 것이라고 생각합니다. 디렉터리 (-type f)가 아닌 모든 실제 파일을 찾고 각 파일에 대해 파일 이름 (% p), 탭 (\ t), 킬로바이트 (% k) 크기, 접미사 "KB"를 인쇄 한 다음 개행 (\ n).
find . -type f -printf '%p\t%k KB\n'
printf 명령이 원하는 방식으로 형식을 지정하지 않으면 exec를 사용하고 각 파일에서 실행할 명령을 사용할 수 있습니다. 파일 이름으로 {}를 사용하고 세미콜론 (;)으로 명령을 종료합니다. 대부분의 셸에서 이러한 세 문자는 모두 백 슬래시로 이스케이프되어야합니다.
다음은 "ls -lh"를 사용하여 파일을 찾아 인쇄하는 간단한 솔루션입니다. 이는 사람이 읽을 수있는 형식으로 크기를 표시합니다 (KB는 k, 메가 바이트는 M).
find . -type f -exec ls -lh \{\} \;
또 다른 대안으로 "wc -c"는 파일에있는 문자 수 (바이트)를 인쇄합니다.
find . -type f -exec wc -c \{\} \;
find . -name '*.ear' -exec du -h {} \;
이것은 불필요한 모든 것 대신 파일 크기 만 제공합니다.
Awk는 질문자가 요청한 것을 제공하기 위해 출력을 수정할 수 있습니다. 내 Solaris 10 시스템에서 find -ls는 두 번째 필드로 크기 (KB)를 인쇄합니다.
% find . -name '*.ear' -ls | awk '{print $2, $11}'
5400 ./dir1/dir2/earFile2.ear
5400 ./dir1/dir2/earFile3.ear
5400 ./dir1/dir2/earFile1.ear
Otherwise, use -exec ls -lh and pick out the size field from the output. Again on Solaris 10:
% find . -name '*.ear' -exec ls -lh {} \; | awk '{print $5, $9}'
5.3M ./dir1/dir2/earFile2.ear
5.3M ./dir1/dir2/earFile3.ear
5.3M ./dir1/dir2/earFile1.ear
Why not use du -a ? E.g.
find . -name "*.ear" -exec du -a {} \;
Works on a Mac
I struggled with this on Mac OS X where the find command doesn't support -printf
.
A solution that I found, that admittedly relies on the 'group' for all files being 'staff' was...
ls -l -R | sed 's/\(.*\)staff *\([0-9]*\)..............\(.*\)/\2 \3/'
This splits the ls long output into three tokens
- the stuff before the text 'staff'
- the file size
- the file name
And then outputs tokens 2 and 3, i.e. output is number of bytes and then filename
8071 sections.php
54681 services.php
37961 style.css
13260 thumb.php
70951 workshops.php
This should get you what you're looking for, formatting included (i.e. file name first and size afterward):
find . -type f -iname "*.ear" -exec du -ah {} \; | awk '{print $2"\t", $1}'
sample output (where I used -iname "*.php"
to get some result):
./plugins/bat/class.bat.inc.php 20K
./plugins/quotas/class.quotas.inc.php 8.0K
./plugins/dmraid/class.dmraid.inc.php 8.0K
./plugins/updatenotifier/class.updatenotifier.inc.php 4.0K
./index.php 4.0K
./config.php 12K
./includes/mb/class.hwsensors.inc.php 8.0K
You could try this:
find. -name *.ear -exec du {} \;
This will give you the size in bytes. But the du command also accepts the parameters -k for KB and -m for MB. It will give you an output like
5000 ./dir1/dir2/earFile1.ear
5400 ./dir1/dir2/earFile2.ear
5400 ./dir1/dir3/earFile1.ear
find . -name "*.ear" | xargs ls -sh
$ find . -name "test*" -exec du -sh {} \; 4.0K ./test1 0 ./test2 0 ./test3 0 ./test4 $
Try the following commands:
GNU stat
:
find . -type f -name *.ear -exec stat -c "%n %s" {} ';'
BSD stat
:
find . -type f -name *.ear -exec stat -f "%N %z" {} ';'
however stat
isn't standard, so du
or wc
could be a better approach:
find . -type f -name *.ear -exec sh -c 'echo "{} $(wc -c < {})"' ';'
find . -name "*.ear" -exec ls -l {} \;
'Programing' 카테고리의 다른 글
helm list :“kube-system”네임 스페이스에있는 configmap을 나열 할 수 없습니다. (0) | 2020.09.11 |
---|---|
ipython 노트북에서 matplotlib 그림 기본 크기를 설정하는 방법은 무엇입니까? (0) | 2020.09.11 |
비밀번호가 현재 정책 요구 사항을 충족하지 않습니다. (0) | 2020.09.11 |
명령 줄에서 Gradle을 통해 장치에 배포 할 수 있습니까? (0) | 2020.09.11 |
C # 구문-문자열을 쉼표로 배열로 분할, 일반 목록으로 변환 및 역순 (0) | 2020.09.11 |