删除除指定文件夹及其内容以外的文件夹内容 [英] delete folder contents except a specified folder and its contents

查看:97
本文介绍了删除除指定文件夹及其内容以外的文件夹内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于其他问题,我有一个脚本可以快速,轻松地更新drupal核心.

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

鉴于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.

我在互联网上尝试了其他一些建议,其中一些建议是使用find进行的,但其中一些建议也删除了sites文件夹中的文件和文件夹.

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)

为清晰起见,这是完整的脚本:

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

这篇关于删除除指定文件夹及其内容以外的文件夹内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆