[英]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.