1

I have a list of folders inside a folder in ubuntu. ie. Under /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+ -----

Like above, I have folders like "a1", "cola", "link" and etc. (more then 100 folders)

In side those each folders there will be a folder named "data_aug" and "data_aug" folder do contain files inside.

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

Now...

I need a script to be placed in "/mnt/source/customcode/"

Want to run the script from "/mnt/source/customcode/" as my script dependent files are can only be executed from "/mnt/source/customcode/"to achieve bellow.

  • Delete the folder "data_aug" and the data inside "data_aug" recursively from all the 100 folders.

  • Then from the "/mnt/source/customcode/" location I should execute a
    special script which, it will refer those 100 folders. like example
    below.

Code...

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

like above for all the 100 folders. The number of folders 100 will grow more in future.

How can I do it dynamically instead of writing simple script with hundreds of lines (static deletion and execution)!

Thanks.

migrated from serverfault.com Jan 3 '18 at 13:04

This question came from our site for system and network administrators.

0

Something like this. I've made it echo over do anything useful. Note its safer to rename the directory and remove it if the work you wanted to do was successful.

You need to test.

!/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
  • No. The script is not working. – Sreekanth Sagar Jan 3 '18 at 12:10
  • 2
    This is a rather short description of the problem. – Cyrus Jan 6 '18 at 11:55
0

With this code will help you to delete those folder recursively

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.