-1

This don't work for me.

#!/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;

This is output:

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
  • 1
    Don't just dump code and tell us it doesn't work! You need to explain what it is supposed to do and how it "doesn't work".
    – terdon
    Aug 21, 2019 at 10:37

1 Answer 1

2

It does not work because brace expansion does not accept variables.

Apart from that there are a lot of strange or "bad" things in your script, e.g.:

  • For better performance, put -maxdepth 1 and -mindepth 1 as first argument to find.
  • Why do you cut off the . and add $pwd instead of just leaving it a relative path ?
  • Use wc -l < fef.txt instead of this useless use of cat.
  • Use sed to get line.

But why you don't simply use find -exec:

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

To revert if not empty anymore:

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

or combined:

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

This will rename to .empty if empty and will remove .empty if not empty.

0

You must log in to answer this question.