当前位置:首页 > 技术文章 > 正文内容

Linux 如何删除某路径下属主为root的所有文件和文件夹

方法1

删除当前路径下属主为root的所有文件和文件夹

find ./* -user root -print0 | xargs -0 rm -rf

删除当前路径下属组为root的所有文件和文件夹

find ./* -group root -print0 | xargs -0 rm -rf

为了防止删错,开始时最好分步执行命令,当确定该删时才合并执行:

# 第一步
ls -l

# 第二步:
find ./* -user root

# 第三步:
find ./* -user root -print0

# 第四步:合并命令正式执行删除操作
find ./* -user root -print0 | xargs -0 rm -rf

方法2: 不要轻易执行rm -rf删除操作,若删除就后悔莫及了

find ./* -user root -exec ls -l {} +
find ./* -user root -exec rm -rf {} +

方法3:此方法不可靠,容易删错文件

step 1:

root@tdar-srv:/home/decisionmaker/.vscode# ls -al
total 172
drwxrwxr-x 17 decisionmaker decisionmaker  4096 9月  18 21:20  .
drwxr-xr-x 29 decisionmaker decisionmaker  4096 9月  18 21:37  ..
-rw-rw-r--  1 decisionmaker decisionmaker   984 9月  18 20:50  argv.json
drwxr-xr-x  2 root          root           4096 9月  18 21:17  Backups
drwx------  3 root          root           4096 9月  18 21:17  blob_storage
drwx------  3 root          root           4096 9月  18 21:18  Cache
drwxr-xr-x  3 root          root           4096 9月  18 21:17  CachedData
drwxr-xr-x  2 root          root           4096 9月  18 21:17  CachedExtensions
drwx------  4 root          root           4096 9月  18 21:17 'Code Cache'
-rw-------  1 root          root          20480 9月  18 21:18  Cookies
-rw-------  1 root          root              0 9月  18 21:18  Cookies-journal
drwx------  2 root          root           4096 9月  18 21:17 'Crash Reports'
drwx------  2 root          root           4096 9月  18 21:17  Dictionaries
drwxrwxr-x  3 decisionmaker decisionmaker  4096 9月  18 20:50  extensions
drwx------  2 root          root           4096 9月  18 21:17 'exthost Crash Reports'
drwx------  2 root          root           4096 9月  18 21:17  GPUCache
-rw-r--r--  1 root          root              2 9月  18 21:17  languagepacks.json
drwx------  3 root          root           4096 9月  18 21:17 'Local Storage'
drwxr-xr-x  3 root          root           4096 9月  18 21:17  logs
-rw-r--r--  1 root          root             36 9月  18 21:17  machineid
-rw-------  1 root          root            365 9月  18 21:18 'Network Persistent State'
-rw-r--r--  1 root          root            446 9月  18 21:17  rapid_render.json
drwx------  2 root          root           4096 9月  18 21:20 'Session Storage'
-rw-r--r--  1 root          root          59724 9月  18 21:17  storage.json
-rw-------  1 root          root            406 9月  18 21:18  TransportSecurity
drwxr-xr-x  4 root          root           4096 9月  18 21:17  User

step 2:

root@tdar-srv:/home/decisionmaker/.vscode# ls -al | grep root
drwxr-xr-x  2 root          root           4096 9月  18 21:17 Backups
drwx------  3 root          root           4096 9月  18 21:17 blob_storage
drwx------  3 root          root           4096 9月  18 21:18 Cache
drwxr-xr-x  3 root          root           4096 9月  18 21:17 CachedData
drwxr-xr-x  2 root          root           4096 9月  18 21:17 CachedExtensions
drwx------  4 root          root           4096 9月  18 21:17 Code Cache
-rw-------  1 root          root          20480 9月  18 21:18 Cookies
-rw-------  1 root          root              0 9月  18 21:18 Cookies-journal
drwx------  2 root          root           4096 9月  18 21:17 Crash Reports
drwx------  2 root          root           4096 9月  18 21:17 Dictionaries
drwx------  2 root          root           4096 9月  18 21:17 exthost Crash Reports
drwx------  2 root          root           4096 9月  18 21:17 GPUCache
-rw-r--r--  1 root          root              2 9月  18 21:17 languagepacks.json
drwx------  3 root          root           4096 9月  18 21:17 Local Storage
drwxr-xr-x  3 root          root           4096 9月  18 21:17 logs
-rw-r--r--  1 root          root             36 9月  18 21:17 machineid
-rw-------  1 root          root            365 9月  18 21:18 Network Persistent State
-rw-r--r--  1 root          root            446 9月  18 21:17 rapid_render.json
drwx------  2 root          root           4096 9月  18 21:20 Session Storage
-rw-r--r--  1 root          root          59724 9月  18 21:17 storage.json
-rw-------  1 root          root            406 9月  18 21:18 TransportSecurity
drwxr-xr-x  4 root          root           4096 9月  18 21:17 User

step 3:

root@tdar-srv:/home/decisionmaker/.vscode# ls -al | grep root | awk '{print $9}'
Backups
blob_storage
Cache
CachedData
CachedExtensions
Code
Cookies
Cookies-journal
Crash
Dictionaries
exthost
GPUCache
languagepacks.json
Local
logs
machineid
Network
rapid_render.json
Session
storage.json
TransportSecurity
User

step 4:

root@tdar-srv:/home/decisionmaker/.vscode# ls -al | grep root | awk '{print $9}' | xargs 
Backups blob_storage Cache CachedData CachedExtensions Code Cookies Cookies-journal Crash Dictionaries exthost GPUCache languagepacks.json Local logs machineid Network rapid_render.json Session storage.json TransportSecurity User

step 5:

root@tdar-srv:/home/decisionmaker/.vscode# ls -al | grep root | awk '{print $9}' | xargs rm -rf

相关文章

Linux 防火墙怎么开?3 分钟学会放行 80 端口,再也不怕远程连接被拒

刚部署完网站却打不开?远程连接突然被拒?90% 的原因是防火墙把端口 “封锁” 了!别慌!今天教你用最简单的命令,3 分钟搞定 Linux 防火墙端口放行,新手也能秒变 “防火墙管理高手”!一、为什么...

linux学习:查看linux服务器的IP(查看linux服务器的ip地址)

我有一云主机,平台提供的信息如下:公网IP:106.52.48.103内网IP:172.16.0.11我想知道我服务器的IP,通过AI查询:linux 查看服务器ip在 Linux 系统中,你可以使用...

Linux基础知识之查看io(查看linux的io使用情况)

1.查看占用IO高的磁盘:iostat -X -d -k 1 102.查看占IO的pid:pidstat -d3.查看pid进程:ps -aux | grep pid4.查看占用IO高的pid:iot...

如何在Linux中查询 DNS 记录,这三个命令可谓是最常用、最经典的

在进入正题之前,先聊聊为什么我们需要检查 DNS 记录。DNS 是互联网的基石,它负责把域名解析成 IP 地址,确保你的浏览器能找到目标服务器。如果 DNS 出问题,可能导致网站打不开、邮件发不出去,...

linux入门系列15--文件传输之vsftp服务

前面的系列文章基本讲完了linux管理相关的基础知识,从本篇开始讲解centos7中服务程序的部署和配置,以便为外部提供各种服务。日常工作和娱乐中,我们所需的各种资源都离不开网络以及各种服务,我们通过...

filezilla,非常实用的ftp工具,远程Linux上传、下载和文件管理

今天给小伙伴们推荐一款非常好用的Linux远程工具,filezilla。在实际的开发过程中,时间就是项目的生命线,欲善其事,先利其器!如果你使用命令ftp上传、下载,加上远程登录、文件授权,烦不甚烦,...