How to delete an entity after creating it using jhipster? - jhipster

I have created 3 entities (Author, Book, Library) using "yo jhipster:entity" command but in one entity (Library) I had a ManyToMany relationship (to Book) but that caused "mappedBy reference an unknown target entity property: com.tst.testdomain.domain.Book.librarys in com.tst.testdomain.docmain.Library.books" so what is the clean way of deleted the Library entity. Would a command like "yo jhipster:entitydelete" be useful?

I use git scm for this. It's not just the generated files that need to be deleted. jHipster also modifies files so with those you need to keep the file but back out the change.
Each time I create an entity I do a separate commit so I can track what jHipster did for each entity.
To clear all changes since the last commit I do
git reset --hard
git clean -fd
If I've done that but I also need to back out the previous commit then I do
git reset --hard HEAD~1
Commits are local with git so it's not until you push these commits that they will be shared.

I do a pull request to add this feature:
https://github.com/jhipster/generator-jhipster/pull/4369
To use it, it's quite simple:
yo jhipster:entity Foo --remove
It's remove the liquibase script but after you need to deal with the table/columns family already created and not yet dropped.
Please vote or comment the issue if you are interested by this :
https://github.com/jhipster/generator-jhipster/issues/4372

You should delete the following:
rm -rf src/main/resources/config/liquibase/changelog/XXX_added_entity_YourEntity.xml
rm -rf src/main/java/com/radsphere/jeces/domain/YourEntity.java
rm -rf src/main/java/com/radsphere/jeces/repository/YourEntityRepository.java
rm -rf src/main/java/com/radsphere/jeces/web/rest/YourEntityResource.java
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntitys.html
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity-detail.html
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity.js
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity.controller.js
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity-detail.controller.js
rm -rf src/main/webapp/scripts/components/entities/YourEntity/YourEntity.service.js
rm -rf src/test/java/com/radsphere/jeces/web/rest/YourEntityResourceTest.java
rm -rf src/main/webapp/i18n/en/YourEntity.json
rm -rf src/main/webapp/i18n/fr/YourEntity.json
And remove the reference from config/liquibase/master.xml:
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="classpath:config/liquibase/changelog/00000000000000_initial_schema.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307175923_added_entity_Company.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180142_added_entity_UserTypeRecruiter.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180338_added_entity_UserTypeCandidate.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180448_added_entity_QuestionType.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180612_added_entity_Question.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180849_added_entity_Answer.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307181033_added_entity_CandidateAnswer.xml" relativeToChangelogFile="false"/>
<!--<include file="classpath:config/liquibase/changelog/20150307181249_added_entity_your_removed_entity.xml" relativeToChangelogFile="false"/>-->
<include file="classpath:config/liquibase/changelog/20150307182736_added_entity_ExamTemplate.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307182950_added_entity_Exam.xml" relativeToChangelogFile="false"/>
<!-- JHipster will add liquibase changelogs here -->

You're right a delete command would be useful. But this command does not exist today. Perhaps it will be added. If you want to delete entity, you have to do it manually. So delete all generated files for your entity: domain, repository, rest, test, Angularjs controller & services, HTML view, link in menu, HTML view

it's simple you just need to delete the entity on the directory .jhipster/entityName.json and thats it.
You can run the "yo:jhipster entity" again and override everything else.
Meme.-

EDITED: added script for newer version of jhipster 3.9.0
My delete-entity.sh script for jhipster Release 2.26.2, based on #shacharsol 's answer.
Usage: ./delete-entity GROUP_ID ENTITY_NAME
GROUP_ID: com/subpackage/and/so/on/ (end with '/')
ENTITY_NAME: firstLowercaseNameOfEntity
Example: ./delete-entity uz/javlon/ transInfo
#!/usr/bin/env bash
echo "################";
if [ -z "$1" ];
then
printf "Required argument GROUP_ID is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
exit 1;
else
GROUP_ID=$1;
echo "GROUP_ID is set to '$1'.";
fi
if [ -z "$2" ];
then
printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
exit 1;
else
ENTITY_NAME=$2;
echo "ENTITY_NAME is set to '$2'.";
fi
echo;
rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${ENTITY_NAME^}.xml
rm -rf src/main/java/${GROUP_ID}domain/${ENTITY_NAME^}.java
rm -rf src/main/java/${GROUP_ID}repository/${ENTITY_NAME^}Repository.java
rm -rf src/main/java/${GROUP_ID}repository/search/${ENTITY_NAME^}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${ENTITY_NAME^}Resource.java
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}s.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.search.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}
rm -rf src/test/java/${GROUP_ID}web/rest/${ENTITY_NAME^}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${ENTITY_NAME^}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.spec.js
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}
rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json
rm -rf ./.jhipster/${ENTITY_NAME^}.json
echo;
echo "Deleting ${ENTITY_NAME^} is completed.";
echo "################";
My another delete-entity.sh script for jhipster Release 3.9.0, based on #Tevfik Kiziloren's answer.
#!/usr/bin/env bash
echo;
if [ -z "$1" ];
then
printf "Required argument GROUP_ID is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
exit 1;
else
GROUP_ID=$1;
echo "GROUP_ID is set to '$1'.";
fi
if [ -z "$2" ];
then
printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
exit 1;
else
ENTITY_NAME=$2;
JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr '[a-z]' '[A-Z]'`${ENTITY_NAME:1}
echo "ENTITY_NAME is set to '$2'."
echo "Java entity name inferred as: '${JAVA_ENTITY_NAME}'.";
fi
JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr '[a-z]' '[A-Z]'`${ENTITY_NAME:1}
UNDERSCORED_FOLDER_NAME=`echo ${ENTITY_NAME} | sed -r 's/([a-z0-9])([A-Z])/\1-\L\2/g'`
echo ${UNDERSCORED_FOLDER_NAME};
QUESTION=$'You may want to keep definition file(.jhipster/'${JAVA_ENTITY_NAME}'.json) in case you want to regenerate entity in the future.\nDo you want to delete entity definition file also?'
while true; do
read -p "${QUESTION}" yn
case $yn in
[Yy]* ) rm -rf ./.jhipster/${ENTITY_NAME}.json; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
echo;
echo "Starting to delete files...";
rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${JAVA_ENTITY_NAME}.xml
rm -rf src/main/java/${GROUP_ID}domain/${JAVA_ENTITY_NAME}.java
rm -rf src/main/java/${GROUP_ID}repository/${JAVA_ENTITY_NAME}Repository.java
rm -rf src/main/java/${GROUP_ID}service/${JAVA_ENTITY_NAME}Service.java
rm -rf src/main/java/${GROUP_ID}service/impl/${JAVA_ENTITY_NAME}ServiceImpl.java
rm -rf src/main/java/${GROUP_ID}repository/search/${JAVA_ENTITY_NAME}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}Resource.java
rm -rf src/main/java/${GROUP_ID}web/rest/dto/${JAVA_ENTITY_NAME}DTO.java
rm -rf src/main/java/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}Mapper.java
rm -rf target/generated-sources/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}MapperImpl.java
rm -rf src/main/webapp/app/entities/${UNDERSCORED_FOLDER_NAME}/*
rm -rf src/test/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${JAVA_ENTITY_NAME}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${UNDERSCORED_FOLDER_NAME}/*
rm -rf src/test/javascript/spec/app/entities/${UNDERSCORED_FOLDER_NAME}
rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/fr/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json
echo "Deleting entity '${ENTITY_NAME}' is completed.";
echo;
echo "-----------------------------------------------------";
echo "Do not forget to manually correct these files also: ";
echo "-----------------------------------------------------";
echo " --> src/main/webapp/index.html"
echo " --> src/main/webapp/scripts/compopnents/navbar.html"
echo " --> src/main/webapp/i18n/**/global.json"
echo " --> src/main/resources/config/liquibase/master.xml (if you use liquibase)"
echo " --> src/main/resources/config/mongeez/master.xml (if you use mongodb)"

In jHipster 7.0.1 you should delete the following files:
jhipster/[EntityName].json
src/main/java/[base package]/domain/[EntityName].java
src/main/java/[base package]/repository/[EntityName]Repository.java
src/main/java/[base package]/repository/search/[EntityName]SearchRepository.java
src/main/java/[base package]/service/[EntityName]Service.java
src/main/java/[base package]/service/dto/[EntityName]DTO.java
src/main/java/[base package]/service/impl/[EntityName]ServiceImpl.java
src/main/java/[base package]/service/mapper/[EntityName]Mapper.java
src/main/java/[base package]/web/rest/[EntityName]Resource.java
src/main/resources/config/liquibase/changelog/*_added_entity_[EntityName].xml
src/main/resources/config/liquibase/changelog/*_added_entity_constraints_[EntityName].xml
src/main/resources/config/liquibase/fake-data/[entity_name].csv
src/test/java/[base package]/domain/[EntityName]Test.java
src/test/java/[base package]/repository/search/[EntityName]SearchRepositoryMockConfiguration.java
src/test/java/[base package]/service/dto/[EntityName]DTOTest.java
src/test/java/[base package]/service/mapper/[EntityName]MapperTest.java
src/test/java/[base package]/web/rest/[EntityName]ResourceIT.java
Delete the whole directory:
src/main/webapp/app/entities/[entity-name]
Remove the references from the following files:
.yo-rc.json
src/main/java/[base package]/config/CacheConfiguration.java
src/main/resources/config/liquibase/master.xml
src/main/webapp/app/entities/entity-routing.module.ts
src/main/webapp/app/layouts/navbar/navbar.component.html
src/main/webapp/i18n/en/global.json
src/main/webapp/i18n/pt-br/global.json
Drop the database and let the app generate it again when starting up.
If you can't delete the database, instead of deleting the liquibase files you will need to create a changelog to delete the obsolete table.
Obviusly depending on how you generated your project you might have less files (or maybe more files).

I've made some additions to SparX's and Meme's answers.
You need to delete DTO file generated for the entity, if you had chosen to "use DTO's" when generating your entity,
JHipster uses Mapstruct to map Entity and DTO. If you use DTO, then you need to delete mapper classes that JHipster generated for you
You need to remove javascript file references of deleted entities from "webapp/index.html"
You need to remove i18n definitions of deleted entity from "webapp/i18n/*/global.json"
You need to remove menu links from "webapp/scripts/components/navbar.html"
If you want to update update entity definition and regenerate entity in the future, you may keep ./.jhipster/${ENTITY_NAME}.json
I updated the SparX's bash script(delete-entity.sh) to ask for whether to delete entity definition file or not. Also the "${ENTITY_NAME^}" syntax (which is used for converting the first letter of the entity name to uppercase) is not run in older bash versions(such as 3.2).
The updated bash script is below. Just put this script in the folder which your pom.xml is located.
Example use-case: If your project's groupId "com.example" and if you want to delete entity named as "city", an example command is given below:
./delete-entity.sh com/example/ city
#!/usr/bin/env bash
echo;
if [ -z "$1" ];
then
printf "Required argument GROUP_ID is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
exit 1;
else
GROUP_ID=$1;
echo "GROUP_ID is set to '$1'.";
fi
if [ -z "$2" ];
then
printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
exit 1;
else
ENTITY_NAME=$2;
JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr '[a-z]' '[A-Z]'`${ENTITY_NAME:1}
echo "ENTITY_NAME is set to '$2'."
echo "Java entity name inferred as: '${JAVA_ENTITY_NAME}'.";
fi
JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr '[a-z]' '[A-Z]'`${ENTITY_NAME:1}
QUESTION=$'You may want to keep definition file(.jhipster/${JAVA_ENTITY_NAME}.json) in case you want to regenerate entity in the future.\nDo you want to delete entity definition file also?'
while true; do
read -p "${QUESTION}" yn
case $yn in
[Yy]* ) rm -rf ./.jhipster/${ENTITY_NAME}.json; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
echo;
echo "Starting to delete files...";
rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${JAVA_ENTITY_NAME}.xml
rm -rf src/main/java/${GROUP_ID}domain/${JAVA_ENTITY_NAME}.java
rm -rf src/main/java/${GROUP_ID}repository/${JAVA_ENTITY_NAME}Repository.java
rm -rf src/main/java/${GROUP_ID}service/${JAVA_ENTITY_NAME}Service.java
rm -rf src/main/java/${GROUP_ID}service/impl/${JAVA_ENTITY_NAME}ServiceImpl.java
rm -rf src/main/java/${GROUP_ID}repository/search/${JAVA_ENTITY_NAME}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}Resource.java
rm -rf src/main/java/${GROUP_ID}web/rest/dto/${JAVA_ENTITY_NAME}DTO.java
rm -rf src/main/java/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}Mapper.java
rm -rf target/generated-sources/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}MapperImpl.java
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}s.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.search.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}
rm -rf src/test/java/${GROUP_ID}web/rest/${ENTITY_NAME}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${ENTITY_NAME}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.spec.js
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}
rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/fr/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json
echo "Deleting entity '${ENTITY_NAME}' is completed.";
echo;
echo "-----------------------------------------------------";
echo "Do not forget to manually correct these files also: ";
echo "-----------------------------------------------------";
echo " --> src/main/webapp/index.html"
echo " --> src/main/webapp/scripts/compopnents/navbar.html"
echo " --> src/main/webapp/i18n/**/global.json"
echo " --> src/main/resources/config/liquibase/master.xml (if you use liquibase)"
echo " --> src/main/resources/config/mongeez/master.xml (if you use mongodb)"

Related

Shell: Move file and folder listed in a list

I'm using Alpine Linux and would like to move specified files and folders from a list. My final goal being to reset a folder by making a backup of some files and folders inside it.
Here is my current script:
cd /mnt/server
mkdir /tmp/backup
[ -d folderOne ] && mv folderOne /tmp/backup
[ -f fileOne.txt ] && mv fileOne.txt /tmp/backup
rm -fr *
rm -fr .*
[ -d /tmp/backup/folderOne ] && mv /tmp/backup/folderOne /mnt/server
[ -f /tmp/backup/fileOne.txt ] && mv /tmp/backup/fileOne.txt /mnt/server
rm -fr /tmp/backup
I would like to use a list to avoid script redundancy and use as few external software as possible. So the idea would be to have a list containing the path to the folders and files to backup. I would use a for loop to go through the list and, if the file/folder exists, then move it by recreating the hierarchy if necessary.
folder/folder2/file.txt would recreate the missing folders and place the file there. And if it is a folder (folder/folderTwo) then it recreates the missing hierarchy and places the folder (and its contents) in it.
#! /bin/bash
set -eu
workdir=/mnt/server
cd "$workdir"
mkdir /tmp/backup
movables=(folderOne fileOne.txt folder/folder2/file.txt)
for move in "${movables[#]}" ; do
if [[ -d $move || -f $move ]] ; then
if [[ "$move" = */* ]] ; then
mkdir -p /tmp/backup/"${move%/*}"
fi
mv "$move" /tmp/backup/"${move%/*}"
fi
done
shopt -s extglob
rm -fr !(.|..)
find /tmp/backup
for move in "${movables[#]}" ; do
if [[ -d /tmp/backup/$move || -f /tmp/backup/$move ]] ; then
if [[ "$move" = */* ]] ; then
mkdir -p "$workdir/${move%/*}"
fi
mv /tmp/backup/"$move" "$workdir"
fi
done
rm -fr /tmp/backup

How can I find the temp file?

I was going to get it to work by creating a temporary file, print the name to the screen, using the brace expansion to make it quickly by creating a backup file with the .BAK suffix in the same directory, using the ls for both files, and delete both files:
#!/bin/bash
fileone=$(mktemp)
echo cp "$fileone"{,.bak}
ls "$fileone"*
rm -rf "$fileone"*
I got an error that tells me that it did not find "/tmp/tmp.UQxlOPQXri.BAK" in my output.
Here's what I got when I run it
++ mktemp
+ fileone=/tmp/tmp.CYQY3BP693
+ echo cp /tmp/tmp.CYQY3BP693 /tmp/tmp.CYQY3BP693.bak
cp /tmp/tmp.CYQY3BP693 /tmp/tmp.CYQY3BP693.bak
+ ls /tmp/tmp.CYQY3BP693
/tmp/tmp.CYQY3BP693
+ rm -rf /tmp/tmp.CYQY3BP693
#!/bin/bash
fileone=$(mktemp)
echo cp "$fileone"{,.bak}
ls "$fileone"*
rm -rf "$fileone"*
You generate a temp file, echo what a cp would do, but you never do it.
That's all.

Linux/Unix Delete files from a remote list

I want to perform:
rm -Rf file.txt file2.txt file3.txt
but from a remote list, eg: domain.com/list.txt with list.txt containing the file names (file.txt, file2.txt etc etc). How can this be done?
You can put the content of domain into variable and then call that variable with rm -rf command to delete the files in the desired location
content=$(wget test.com/1.txt -q -O -); rm -rf $content

delete folder contents except a specified folder and its contents

Per my other question, I have a script to update drupal core quickly and easily.
Presently, it moves the sites folder out of the directory, deletes the site folder's contents, then moves the sites folder back in.
mv ./sites ../sites
rm -rf *
cp -R /sources/drupal-7/* ./
mv ../sites ./sites
Given the fact that a sites folder can get fairly large, I would like to avoid moving it if at all possible, and delete only the other folders, leaving only the sites folder behind.
I have tried some other suggestions around the internet, a few from here, one of which used find, but that deleted the files and folders WITHIN the sites folder too.
I would also like to keep the source folder intact, i.e.: keep a sites folder within it for new sites, and move copy only other files/folders to the site to update like:
rm -rf * !sites/*
cp -R /sources/drupal-7/* ./ !sites/*
I have tried numerous methods, most of which simply don't work or give a syntax error (or delete sites or its contents)
EDIT: here is the script in its entirity, for clarity:
#/bin/bash
CWD=$(pwd)
cd $CWD
echo $CWD
if [[ $CWD = "/var/www/vhosts/"* ]]; then
echo "Updating drupal core files"
read -r -p "do you need to keep the .htaccess file? [y/N]" response
if [ $response = y ]
then
/usr/local/bin/drush vset maintenance_mode 1
mv ./.htaccess ../.htaccess
mv ./sites ../sites
rm -rf *
cp -R /sources/drupal-7/* ./
cp -R /sources/drupal-7/.* ./
mv ../sites ./sites
mv ../.htaccess ./.htaccess
chown -R httpd:httpd *
/usr/local/bin/drush up && /usr/local/bin/drush updb
/usr/local/bin/drush cc all && /usr/local/bin/drush cron
/usr/local/bin/drush vset maintenance_mode 0
elif [ $response = n ]
then
/usr/local/bin/drush vset maintenance_mode 1
mv ./sites ../sites
rm -rf *
cp -R /sources/drupal-7/* ./
cp -R /sources/drupal-7/.* ./
mv ../sites ./sites
chown -R httpd:httpd *
/usr/local/bin/drush up && /usr/local/bin/drush updb
/usr/local/bin/drush cc all && /usr/local/bin/drush cron
/usr/local/bin/drush vset maintenance_mode 0
else
echo "Response must be either y or n"
fi
else
echo "not in a web directory, exiting"
fi
Updated as per suggestions in comments:
find /path/to/dir -maxdepth 1 -mindepth 1 ! -name "sites" -type d -print0 | xargs -0 rm -rf
In bash you can enable extended globs (shopt -s extglob) and then use !(sites):
bash-3.2$ shopt -s extglob
bash-3.2$ rm -r !(sites)
bash-3.2$ cp -R /sources/drupal-7/!(sites) .

Syntax error when I try to use bash from sh `echo 'rm -rf !(cookbooks)' | bash`

When I use /bin/sh I can issue commands through bash simply by echoing to it
vagrant#vagrant:~$ sh
$ echo 'ls' | bash
some.sh
But when I try to use this command rm -rf !(cookbooks) I get this
$ echo 'rm -rf !(cookbooks)' | bash
bash: line 1: syntax error near unexpected token `('
bash: line 1: `rm -rf !(cookbooks)'
And I need issue this command from /bin/sh.
#anubhava from a packer http://www.packer.io/ provision script
if [ -d "/opt/chef/chef-solo/" ]; then
cd /opt/chef/chef-solo
rm -rf !(cookbooks)
fi
!(cookbooks) is an extglob. They're not enabled by default; you need to run shopt -s extglob in a prior line of bash (because the parser operates line-by-line) to make it valid.
So:
printf '%s\n' 'shopt -s extglob' 'rm -rf !(cookbooks)' | bash
...or you can enable extglobs via the command line (thanks to chepner for the addendum):
echo 'rm -rf !(cookbooks)' | bash -O extglob
By the way, it is possible to do this in POSIX sh, without use of extglobs. For instance:
# wrap in a function to have a separate $# rather than overriding global
delete_all_but_cookbooks() {
set --
for f in *; do
[ "$f" = "cookbooks" ] || set -- "$#" "$f"
done
rm -rf "$#"
}
delete_all_but_cookbooks
...or, much simpler, just using find:
find . -mindepth 1 -maxdepth 1 -name cookbooks -prune -o -exec rm -rf '{}' +

Resources