Скрипт, который переименовывает папку, если папка пуста

Это не работает для меня.

#!/bin/bash
# script to find empty folder in current folder and rename it.

#find empty folder and write it into fef.txt
find . -type d -empty -maxdepth 1 -mindepth 1 | cut -c2- > fef.txt;

#number line of fef.txt
nl_fef=$(cat fef.txt | wc -l);

#rename loop
for i in {1..$nl_fef};
do

# folder path
f=$(pwd);
d=$(cat fef.txt | head -$i | tail -1)
folder_path=$f$d

#rename folder_name to folder_name.empty 
mv "$folder_path" "$folder_path".empty;

done;

Это вывод:

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

head: invalid option -- '{'
Try 'head --help' for more information.
-1
задан 21 August 2019 в 13:39

1 ответ

Это не работает, потому что расширение фигурной скобки не принимает переменные .

Кроме этого существует много странных или "плохих" вещей в Вашем сценарии, например:

  • Для лучшей производительности, помещенной -maxdepth 1 и -mindepth 1 как первый аргумент find.
  • , Почему Вы убегаете . и добавляете $pwd вместо того, чтобы просто оставить это относительным путем?
  • Использование wc -l < fef.txt вместо этого бесполезного использования cat.
  • Использование sed для получения строки .

, Но почему Вы просто не используете find -exec:

find . -maxdepth 1 -mindepth 1 -type d -not -name  -empty -exec mv {} {}.empty \;

, Чтобы возвращаться, если не пустой больше:

find . -maxdepth 1 -mindepth 1 -type d -name "*.empty" -not -empty -exec rename 's/.empty$//' {} +

или объединенный:

find . -maxdepth 1 -mindepth 1 -type d \
  \( -empty -not -name "*.empty" -exec rename 's/$/.empty/' {} + \) \
  -or \( -not -empty -name "*.empty" -exec rename 's/.empty$//' {} + \)

Это переименует к [1 112], если пустой и удалит .empty если не пустой.

3
ответ дан 23 October 2019 в 05:05

Другие вопросы по тегам:

Похожие вопросы: