![두 번째 명령에서 "grep" 및 "grep" 출력을 변수로 사용하여 폴더의 사용자 ID를 가져옵니다.](https://linux55.com/image/137068/%EB%91%90%20%EB%B2%88%EC%A7%B8%20%EB%AA%85%EB%A0%B9%EC%97%90%EC%84%9C%20%22grep%22%20%EB%B0%8F%20%22grep%22%20%EC%B6%9C%EB%A0%A5%EC%9D%84%20%EB%B3%80%EC%88%98%EB%A1%9C%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%ED%8F%B4%EB%8D%94%EC%9D%98%20%EC%82%AC%EC%9A%A9%EC%9E%90%20ID%EB%A5%BC%20%EA%B0%80%EC%A0%B8%EC%98%B5%EB%8B%88%EB%8B%A4..png)
다음 명령을 사용하여 사용된 디스크의 백분율을 가져왔습니다 df -h| grep workfld
. 가장 많은 디스크 공간을 백분율로 사용하여 최상위 폴더를 가져오는 대신 마지막 부분은 du -h /saswork | sort -rh | head -20
최종 명령의 출력/결과를 사용하여 폴더인 사용자를 grep
가져오는 것입니다 . USERID
여기에서 코드를 읽을 수 있도록 하기 위해 여러 단계로 나뉩니다.
df -h| grep saswork
du -h /saswork | sort -rh | head -20
ls -la| grep %OUTPUT_FROM_COMMAND_2%
처음 5개 폴더에 대해 반복하고 해당 폴더에 대한 사용자 ID를 얻을 수 있도록 이 작업을 수행하는 정확하고 가장 쉬운 방법은 무엇입니까?
따라서 두 번째 명령의 결과는 다음과 같습니다. 569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03 569G/saswork/SAS_work438800007078_prdsasgridn03
그러나 상위 20개의 결과가 있습니다. 세 번째 명령에서 입력으로 사용하려는 부분은 위에 굵게 표시된 폴더 이름입니다. 이것이 세 번째 명령에 들어가야 하는 내용이므로 세 번째 명령은 다음과 같습니다.
ls -la| grep SAS_work438800007078_prdsasgridn03
이 명령 출력은 사용자 ID를 제공합니다. 실행한 두 번째 명령의 출력에서 생성된 각 폴더 이름에 대해 이 명령을 실행하고 싶습니다.
더 정확한 예시 추가
Input: df -h| grep saswork
Output: /dev/mapper/vg_saswork--prd-lv_sas--saswork 3.1T 2.2T 861G 73%
/saswork
Input: du -h /saswork | sort -rh | head -20
Output:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03
569G /saswork/SAS_work438800007078_prdsasgridn03 526G/saswork/SAS_work445F00002079_prdsasgridn01/SAS_work189900002079_prdsasgridn01 526G/saswork/SAS_work445F00002079_prdsasgridn01 165G/saswork/SAS_workBD3300007B7 E_prdsasgrid n04/SAS_work154B00007B7E_prdsasgridn04 165G/saswork/SAS_workBD3300007B7E_prdsasgridn04 134G/saswork/SAS_work36E800005097_prdsasgridn04/SAS_workA860000050 97_prdsasgridn04 134G /saswork/SAS_work36E800005097_prdsasgridn04 110G /saswork/SAS_workB87B00002026_prdsasgridn01/SAS_workD37900002026_prdsasgridn01 110G /saswork/ SAS_ workB87B00002026_prdsasgridn01 105G/saswork/SAS_work55C800001BDA_prdsasgridn01/SAS_work849500001BDA_prdsasgridn01 105G/saswork/SAS_work55C800001BDA_prdsasgridn01 7G/saswork/SAS_work3FB700003AAF_prds asgridn03/SAS_work8268 00003AAF_prdsasgridn03 57G /saswork/SAS_work3FB700003AAF_prdsasgridn03 55G/saswork/SAS_work8744000068D9_prdsasgri dn01/SAS_work8CA9000068D9_prdsasgrid n01 55G /saswork/SAS_work87440000 68D9_prdsasgridn01 46G/saswork/SAS_work400B00002BFF_prdsasgridn02/SAS_work668100002BFF_prdsasgridn 02 46G /saswork/SAS_work400B00002BFF_ prdsasgridn02 40G/saswork/ SAS_work67780000280E_prdsasgridn02/SAS_work91E90000280E_prdsasgridn02
Input: ls -la| grep **SAS_work438800007078_prdsasgridn03** NOTE: The
foldername SAS_work438800007078_prdsasgridn03 came from one of the results
from the second command output. That's where I need to pull it from for each
one.
Output: drwx------. 3 **g6753** ereapp 3864 Jul 12 12:25
AS_work438800007078_prdsasgridn03
Note - bold in this line is the ID of the developer that I need.
답변1
명령 결과를 변수에 저장하려면 `` 또는 $() 사이에 명령을 넣으면 됩니다.
yourvar=`date +%Y`
또는
yourvar=$(date +%Y)
yourvar는 현재 연도(2018)의 값을 갖습니다. 명령에서 직접 결과를 실행할 수도 있습니다.
ls -la| grep `date +%Y`
또는
ls -la| grep $(date +%Y)
원하는 것을 달성하기 위해 for 루프를 사용합니다.
for i in `du -h |sort -rh|awk '{print $2}'|sed "s/.\///g"`;do ls -la |grep $i|awk '{print $3 " " $9}';done
이 명령은 대략적인 예이므로 중복을 피하기 위해 조정해야 할 수도 있습니다. 디렉터리에 하위 디렉터리가 있는 경우 동일한 상위 디렉터리의 출력이 다르기 때문입니다.
답변2
귀하의 질문을 올바르게 이해했다면 가장 큰 디렉토리의 소유자 목록(내림차순)을 보고 싶을 것입니다. 다시 말해서:
- 크기에 따라 디렉토리를 정렬하고 싶습니다.
- 가장 큰 상위 20개 디렉토리 각각에 대한 사용자 ID를 찾으십시오.
형식화된 출력을 보려면 ls -l
다음을 시도하십시오.
(du -sh ./*; ls -lh --color=no) | awk '{ if($1 == "total") {X = 1} else if (!X) {SIZES[$2] = $1} else { sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0} }' | sort --key=5,5hr
예제 출력:
drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
-rwxrwxrwx 1 root root 124M May 22 10:41 archive_web.html.7z
-rwxrwxrwx 1 root root 113M Jan 15 14:51 example1.tif
-rwxrwxrwx 1 root root 113M Apr 15 13:27 example3.pdf
-rwxrwxrwx 1 root root 1.0M Apr 15 09:17 sample_info.xlsx
-rwxrwxrwx 1 root root 1.0M Apr 27 09:20 sample_info2.xlsx
-rwxrwxrwx 1 root root 1.0M Jun 12 09:18 sample_run.R
보시다시피 이것은 모든 디렉토리의 정렬된 목록을 제공합니다(크기 기준).그리고문서. 그러나 당신은 디렉토리/폴더, 크기 및 사용자 ID에만 관심이 있습니다. 따라서 이들 모두(위 명령)에 간단한 파이프를 추가하고 해당 grep
명령을 사용하면(즉, | grep "^d" --color=never"
위 명령에 추가) 디렉터리만 나열되고 파일은 더 이상 나열되지 않습니다.
drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
따라서 이 경우 전체 명령은 다음과 같습니다.
(du -sh ./*; ls -lh --color=no) | awk '{ if($1 == "total") {X = 1} else if (!X) {SIZES[$2] = $1} else { sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0} }' | sort --key=5,5hr | grep "^d" --color=never"
다음으로 해야 할 일은 head -20
위의 명령을 추가하여 가장 큰 상위 20개 디렉터리만 보는 것입니다.
모든 신용스택 오버플로 사용자로 이동세비. 자세한 내용은 SO에 대한 이 스레드를 참조하세요.ls를 사용하여 디렉토리와 전체 크기를 나열하십시오.