简体   繁体   English

删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容

[英]delete all folders and files within a linux directory except one folder and all contents inside that folder

I have a directory structure as :- 我有一个目录结构为:-

/usr/testing/member/
   ---> public--->folder1--->file1
           \----> file2
   ---> folder3:- contains files and folders
   ---> folder4:- contains files and folders
   ---> several files

I want to keep the public folder and all its contents (further folders and files within it) but want to delete everything else under the directory /usr/testing/member/. 我想保留公用文件夹及其所有内容(其中的其他文件夹和文件),但要删除目录/ usr / testing / member /下的所有其他内容。 But that also means member folder is not deleted. 但这也意味着不删除成员文件夹。 Is there any shell script or command that can be used to achieve this exactly as i stated. 是否有任何外壳脚本或命令可用于完全按照我的说明实现此目的。

Here's one way to do it: 这是一种实现方法:

(cd /usr/testing/member; find . -maxdepth 1 \( ! -name . -a ! -name public \) -exec echo rm -fr {} +)

That is: cd into /usr/testing/member , find all files and directories there, without going further below, and exclude the current directory (".") and any file or directory named "public", and execute a command for the found files. 即: cd进入/usr/testing/member ,在此查找所有文件和目录,而无需在下面进行进一步介绍,并排除当前目录(“。”)和任何名为“ public”的文件或目录,并为该目录执行命令找到文件。

This will print what would be deleted. 这将打印将要删除的内容。 Verify it looks good, and then drop the echo . 验证它看起来不错,然后删除echo

I think below will do the work, 我认为下面会做的工作,

$ cd  /usr/testing/member/
$ rm -rf $(ls | grep -v "public")

explanation: 说明:

we are passing everything inside /usr/testing/member/ but public to rm by making use of -v(exclude) option of grep 我们通过使用grep-v(exclude)选项将/usr/testing/member/内部的所有内容都传递给rm ,将其public传递给rm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除目录中除一个文件夹和一个文件以外的所有文件? - how to delete all files in directory except one folder and one file? Linux命令删除除.git文件夹以外的所有文件? - Linux command to delete all files except .git folder? 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 在Linux终端中,如何删除目录中除一个或两个以外的所有文件 - In Linux terminal, how to delete all files in a directory except one or two 递归删除所有文件和文件夹,但不包括一个文件夹 - Delete all files and folders recursively but excluding one folder 除了某些文件夹中的文件外,我如何将其所有文件包含在文件夹或目录中? - How do I tar a folder or directory with all of it's files except leave out files from certain folders? Linux - 授予所有用户访问文件夹(以及下面的所有文件夹和文件)的权限 - Linux - Giving all users access to a folder (and all folders and files below) Linux:如何删除文件夹中除具有特定名称的文件之外的所有文件? - Linux: how to remove all the files in a folder except ones with specific names? 更改除一个文件夹外的所有文件夹的所有者 - Changing owner of all folder except one folder Linux:查找所有具有特定名称的文件夹,将其删除,然后将一个文件夹复制到这些文件夹的父目录中 - Linux: Find all folders with a particular name, remove them and have a folder copied into the parent directory of those folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM