优秀的编程知识分享平台

网站首页 > 技术文章 正文

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

nanyue 2024-08-31 23:59:20 技术文章 4 ℃

方法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
最近发表
标签列表