未删除文件夹但删除了文件

未删除文件夹但删除了文件

删除工作项目中的所有文件,但不删除工作目录,使用带有-r的交互选项(查看rm命令手册来执行此操作)

答案1

保留目录结构但仅删除常规运行的文件。
find /tmp/work -type f -delete

要以交互方式删除目录和文件,请运行。'i
rm /tmp/work/* -irv
' 标志将使 rm 在每次删除时请求权限。'r' 标志将告诉 rm 以递归方式删除文件。'v' 标志使 rm 告诉您它正在做什么。

# use brace expansion to create three levels of directories. The -p flag tells mkdir to create parent directories as needed.
mkdir -p /tmp/work/dir_{A..D}/dir_{a,b}
# tree will show the directory structure.
tree /tmp/work
/tmp/work
├── dir_A
│   ├── dir_a
│   └── dir_b
├── dir_B
│   ├── dir_a
│   └── dir_b
├── dir_C
│   ├── dir_a
│   └── dir_b
└── dir_D
    ├── dir_a
    └── dir_b
# For testing create empty files in each dir_a
touch /tmp/work/dir_{A..D}/dir_a/test.txt
# to Find what files will be deleted
find /tmp/work -type f
/tmp/work/dir_B/dir_a/test.txt
/tmp/work/dir_D/dir_a/test.txt
/tmp/work/dir_C/dir_a/test.txt
/tmp/work/dir_A/dir_a/test.txt  

# if the correct files were found now run the command but include the delete flag
find /tmp/work -type f -delete
# if however you would prefer to remove both files and the directories interactively. The 'i' flag will make rm ask for permission for each removal. The 'r' flag will tell rm to recursively remove files. The 'v' flag makes rm tell you what it is doing.
rm /tmp/work/* -irv

相关内容