ls에 대한 쉘 스크립트 및 ls 결과에 대해 명령 실행

ls에 대한 쉘 스크립트 및 ls 결과에 대해 명령 실행

우분투의 폴더 안에 폴더 목록이 있습니다. 즉. /mnt/source/customcode/files/brands/에서

root@a919794ec280:/mnt/source/customcode/files/brands# ls -la
total 236
drwxrwxrwx 19 1002 root   4096 Jan  3 09:26 .
drwxrwxrwx  8 1002 1003   4096 Dec 27 22:45 ..
drwxrwxr-x  3 1002 1003   4096 Jan  3 09:22 aem
drwxrwxr-x  3 1002 1003   4096 Jan  3 09:22 apple
drwxrwxr-x  3 1002 1003   4096 Jan  3 09:22 cool
drwxr-xr-x  3 1002 root   4096 Jan  3 09:22 doll
drwxr-xr-x  3 1002 root   4096 Jan  3 09:22 ent
drwxr-xr-x  3 1002 root   4096 Jan  3 09:22 f1
drwxr-xr-x  3 1002 root   4096 Jan  3 09:22 google
drwxrwxrwx  3 1002 root   4096 Jan  3 09:22 h1
drwxrwxr-x  3 1002 1003   4096 Jan  3 09:22 iq
drwxr-xr-x  3 1002 root   4096 Jan  3 09:22 king
drwxrwxrwx  3 root root   4096 Jan  3 09:22 link
drwxrwxrwx  3 1002 root 155648 Jan  3 09:22 nobrand
drwxrwxr-x  3 1002 1003  12288 Jan  3 09:22 pepperfry
drwxrwxrwx  2 1002 root   4096 Jan  3 09:22 pepsi
drwxrwxr-x  2 1002 1003   4096 Jan  3 09:22 rock
drwxrwxrwx  2 1002 root   4096 Jan  3 09:22 star9
drwxrwxr-x  2 1002 1003   4096 Jan  3 09:22 vicks
----- 100+ -----

위에서 언급한 것처럼 "a1", "cola", "link" 등과 같은 폴더가 있습니다. (100개 이상의 폴더)

각 폴더 옆에는 "data_aug"라는 폴더가 있고 "data_aug" 폴더에는 파일이 포함되어 있습니다.

root@a919794ec280:/mnt/source/customcode/files/brands/apple# ls -la
total 1988
drwxrwxr-x  3 1002 1003   4096 Jan  3 09:22 .
drwxrwxrwx 19 1002 root   4096 Jan  3 09:26 ..
-rw-r--r--  1 1002 1003  17465 Dec 29 23:28 0DPA0C3.jpg
-rw-r--r--  1 1002 1003   3836 Dec 31 00:55 130418131111-apple-store-santa-monica.jpg
-rw-r--r--  1 1002 1003  48616 Dec 31 00:52 15146774227434507677123475441338.jpg
-rw-r--r--  1 1002 1003   9107 Dec 31 00:52 15146774615326659565665852728294.jpg
-rw-r--r--  1 1002 1003  28005 Dec 29 23:28 3D-Apple-Logo-Wallpapers.jpg
-rw-r--r--  1 1002 1003  75855 Dec 29 23:28 3D-Apple-Logo.jpg
-rw-r--r--  1 1002 1003  27403 Dec 29 23:28 3d_apple_logo___wallpaper_by_techflashdesigns-d4fbc7p.jpg
-rw-r--r--  1 1002 1003  38420 Dec 29 23:28 AX62GjL.jpg
-rw-r--r--  1 1002 1003   6025 Dec 29 23:28 Apple-Logo-2016 (1).jpg
-rw-r--r--  1 1002 1003 929419 Dec 29 23:28 Apple-Logo-Png-Download.png
-rw-r--r--  1 1002 1003  35742 Dec 29 23:28 apple-logo-2016.jpg
-rw-r--r--  1 1002 1003  15301 Dec 29 23:28 apple-logo-3d-model-max-obj-fbx-3dm-ige-igs-iges.jpg
-rw-r--r--  1 1002 1003  42514 Dec 29 23:28 apple-logo-clipart-2.jpeg
-rw-r--r--  1 1002 1003 152183 Dec 29 23:28 apple-logo-clouds.jpg
-rw-r--r--  1 1002 1003   6117 Dec 31 00:55 apple1.jpg
-rw-r--r--  1 1002 1003 349787 Dec 29 23:28 apple_ipad_logo.png
-rw-r--r--  1 1002 1003  54167 Dec 29 23:28 b5c4d6c9-3.jpg
drwxr-xr-x  2 root root   4096 Jan  3 09:22 data_aug
-rw-r--r--  1 1002 1003  32918 Dec 29 23:28 large.JPG
-rw-r--r--  1 1002 1003  48972 Dec 29 23:28 medium.jpg
-rw-r--r--  1 1002 1003   3609 Dec 29 23:28 th (1).jpeg
-rw-r--r--  1 1002 1003  30392 Dec 29 23:28 th (2).jpeg
-rw-r--r--  1 1002 1003   3222 Dec 29 23:28 th.jpeg
-rw-r--r--  1 1002 1003  22544 Dec 29 23:28 u5TAcBk.jpg

root@a919794ec280:/mnt/source/customcode/files/brands/apple/data_aug# ls -la
total 4356
drwxr-xr-x 2 root root  12288 Jan  3 09:41 .
drwxrwxr-x 3 1002 1003   4096 Jan  3 09:22 ..
-rw-r--r-- 1 root root  10450 Jan  3 09:40 apple_0_1.jpeg
-rw-r--r-- 1 root root   5599 Jan  3 09:40 apple_0_1019.jpeg
-rw-r--r-- 1 root root   3225 Jan  3 09:40 apple_0_1075.jpeg
-rw-r--r-- 1 root root  10283 Jan  3 09:40 apple_0_1100.jpeg
-rw-r--r-- 1 root root  13923 Jan  3 09:41 apple_0_1119.jpeg

지금...

"/mnt/source/customcode/"에 스크립트를 배치해야 합니다.

내 스크립트 관련 파일은 다음 목적으로 "/mnt/source/customcode/"에서만 실행할 수 있으므로 "/mnt/source/customcode/"에서 스크립트를 실행하고 싶습니다.

  • 100개 폴더 모두에서 "data_aug" 폴더와 "data_aug" 내의 데이터를 반복적으로 삭제합니다.


  • 그런 다음 이 100개 폴더를 참조하는 "/mnt/source/customcode/" 위치에서 특수 스크립트를 실행해야 합니다 .
    아래 예 와 같습니다 .

암호...

python -m scripts.augmentation_keras --image_dir=files/brands/apple --target_dir=files/brands/apple/data_aug --save_prefix=apple

python -m scripts.augmentation_keras --image_dir=files/brands/a1 --target_dir=files/brands/a1/data_aug --save_prefix=a1

위에 표시된 대로 모든 100개 폴더에 대해. 향후 폴더 수는 100개로 늘어날 예정입니다.

수백 줄의 간단한 스크립트(정적 삭제 및 실행)를 작성하는 대신 어떻게 이를 동적으로 수행할 수 있습니까?

감사해요.

답변1

질문이 있으신가요 rm -rf */data_aug?

답변2

이 같은. 나는 에코에게 유용한 일을 하도록 했습니다. 원하는 작업이 성공하면 디렉터리 이름을 바꾸고 삭제하는 것이 더 안전하다는 점에 유의하세요.

테스트해야합니다.

!/bin/bash
set -e

for da in files/brands/*/data_aug; do
  dir=$(dirname "${da}")
  brand=$(basename "${dir}")

  tmpd=$(mktemp -du "${dir}/data_aug.XXXXXX")
  echo mv "${da}" "${tmpd}"
  if echo python -m scripts.augmentation_keras \
     --image_dir="${dir}" \
     --target_dir="${da}" \
     --save_prefix="${brand}"
  then
    echo rm -fr "${tmpd}"
  fi
done

답변3

이 코드는 이러한 폴더를 재귀적으로 삭제하는 데 도움이 됩니다.

directoryPath="/mnt/source/customcode/"


for dir in $(find ${directoryPath} -maxdepth 100 -type d)
do
      if [ $(basename ${dir}) == "data_aug" ]; then
          rm -fr ${dir}
      fi
done

관련 정보