如何删除除了某个文件之外的所有文件?

发布于 2022-10-15 07:36:31 字数 1032 浏览 29 评论 0

如何删除除了某个文件之外的所有文件?

我在管理中经常想删除除了某个文件之外的所有文件?请问怎么实现啊?比如我的目录如下:
[webdb@database-8 old]$ ll
total 308
drwxrwxr-x  3 webdb webdb   4096 May  6 21:49 config
drwxr-xr-x 10 webdb webdb   4096 May  6 21:49 gbrowse
drwxr-xr-x 15 webdb webdb   4096 May  6 21:49 Generic-Genome-Browser-1.68
-rw-rw-r--  1 webdb webdb  10645 May  6 21:49 index.html
-rw-r--r--  1 webdb webdb  10690 May  6 21:49 index.html20090810
-rw-rw-r--  1 webdb webdb  12667 May  6 21:49 index.html.bak1
drwxrwxrwx  6 webdb webdb   4096 May  5 17:53 monkey
-rw-rw-r--  1 webdb webdb    551 May  6 21:49 robots.txt
-rw-rw-r--  1 webdb webdb 246692 May  6 21:49 silkdb_genome_browse.jpg
我要删除除了monkey之外的所有文件,rm 后面接什么样的正则呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我一向站在原地 2022-10-22 07:36:31

我测试过了,完全可以这么来如下:
ls |grep -v monkey |xargs rm -f       就可以了

而下面这样来操作,是不行:
ll |grep -v monkey |xargs rm -f  

我测试的环境如下:
[root@nagios 0507]# ls
a.txt  b.txt  c.txt
[root@nagios 0507]# ls |grep -v b.txt |xargs rm  -f
[root@nagios 0507]# ls
b.txt
[root@nagios 0507]# touch {a.txt,b.txt,c.txt}
[root@nagios 0507]# ls
a.txt  b.txt  c.txt
[root@nagios 0507]# ll |grep -v b.txt |xargs rm  -f
rm: invalid option -- w
Try `rm --help' for more information.
[root@nagios 0507]# ls
a.txt  b.txt  c.txt

金橙橙 2022-10-22 07:36:31

本帖最后由 听老歌 于 2011-05-08 15:15 编辑

跟踪命令
[root@nagios 0507]# strace ls |grep -v b.txt |xargs rm  -f
execve("/bin/ls", ["ls"], [/* 24 vars */]) = 0
brk(0)                                  = 0x8472000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=45337, ...}) = 0
mmap2(NULL, 45337, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f3a000
close(3)                                = 0
open("/lib/librt.so.1", O_RDONLY)       = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\200\350\271\0004\0\0\0"..., 512) = 512
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f39000
fstat64(3, {st_mode=S_IFREG|0755, st_size=48156, ...}) = 0
mmap2(0xb9d000, 33324, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb9d000
mmap2(0xba4000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x7) = 0xba4000
close(3)                                = 0
open("/lib/libacl.so.1", O_RDONLY)      = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220\222\272\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=25940, ...}) = 0
mmap2(0xba8000, 27288, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xba8000
mmap2(0xbae000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5) = 0xbae000
close(3)                                = 0
open("/lib/libselinux.so.1", O_RDONLY)  = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0000\5\274\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=93508, ...}) = 0
mmap2(0xbbd000, 97112, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xbbd000
mmap2(0xbd3000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15) = 0xbd3000
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\340\257\240\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1693796, ...}) = 0
mmap2(0x9f5000, 1410500, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x9f5000
mmap2(0xb48000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x153) = 0xb48000
mmap2(0xb4b000, 9668, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb4b000
close(3)                                = 0
open("/lib/libpthread.so.0", O_RDONLY)  = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0Ph\270\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=137908, ...}) = 0
mmap2(0xb82000, 98788, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb82000
mmap2(0xb97000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15) = 0xb97000
mmap2(0xb99000, 4580, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb99000
close(3)                                = 0
open("/lib/libattr.so.1", O_RDONLY)     = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0P, \0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=15972, ...}) = 0
mmap2(0x202000, 17248, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x202000
mmap2(0x206000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3) = 0x206000
close(3)                                = 0
open("/lib/libdl.so.2", O_RDONLY)       = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0p\272\267\0004\0\0\0"..., 512) = 512
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f38000
fstat64(3, {st_mode=S_IFREG|0755, st_size=20668, ...}) = 0
mmap2(0xb7b000, 16504, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7b000
mmap2(0xb7e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2) = 0xb7e000
close(3)                                = 0
open("/lib/libsepol.so.1", O_RDONLY)    = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0@\237\275\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=245376, ...}) = 0
mmap2(0xbd7000, 285024, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xbd7000
mmap2(0xc12000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3b) = 0xc12000
mmap2(0xc13000, 39264, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xc13000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f37000
set_thread_area({entry_number:-1 -> 6, base_addr:0xb7f378e0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present : 0, useable:1}) = 0
mprotect(0xba4000, 4096, PROT_READ )     = 0
mprotect(0xb48000, 8192, PROT_READ )     = 0
mprotect(0xb97000, 4096, PROT_READ )     = 0
mprotect(0x9f1000, 4096, PROT_READ )     = 0
mprotect(0xb7e000, 4096, PROT_READ )     = 0
munmap(0xb7f3a000, 45337)               = 0
set_tid_address(0xb7f37928 )             = 4473
set_robust_list(0xb7f37930, 0xc)        = 0
futex(0xbfb4aa04, FUTEX_WAKE_PRIVATE, 1 ) = 0
rt_sigaction(SIGRTMIN, {0xb863e0, [], SA_SIGINFO}, NULL, 8 ) = 0
rt_sigaction(SIGRT_1, {0xb862e0, [], SA_RESTART|SA_SIGINFO}, NULL, 8 ) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8 ) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=10240*1024, rlim_max=RLIM_INFINITY}) = 0
uname({sys="Linux", node="nagios", ...} ) = 0
access("/etc/selinux/", F_OK )           = 0
brk(0)                                  = 0x8472000
brk(0x8493000)                          = 0x8493000
open("/etc/selinux/config", O_RDONLY|O_LARGEFILE ) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=447, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f45000
read(3, "# This file controls the state o"..., 4096) = 447
read(3, "", 4096 )                       = 0
close(3)                                = 0
munmap(0xb7f45000, 4096)                = 0
open("/proc/mounts", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f45000
read(3, "rootfs / rootfs rw 0 0\n/dev/root"..., 4096) = 546
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0xb7f45000, 4096)                = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfb4a878 ) = -1 EINVAL (Invalid argument )
ioctl(1, TIOCGWINSZ, 0xbfb4a974)        = -1 EINVAL (Invalid argument)
open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY ) = 3
fcntl64(3, F_SETFD, FD_CLOEXEC)         = 0
getdents64(3, /* 3 entries */, 32768 )   = 80
getdents64(3, /* 0 entries */, 32768 )   = 0
close(3)                                = 0
fstat64(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f45000
write(1, "b.txt\n", 6)                  = 6
close(1)                                = 0
munmap(0xb7f45000, 4096)                = 0
exit_group(0)                           = ?
[root@nagios 0507]# ls
b.txt

[root@nagios 0507]# ll
total 0
-rw-r--r-- 1 root root 0 May  7 02:29 a.txt
-rw-r--r-- 1 root root 0 May  7 02:29 b.txt
-rw-r--r-- 1 root root 0 May  7 02:29 c.txt
[root@nagios 0507]# strace chattr +i b.txt ;rm -rf *
execve("/usr/bin/chattr", ["chattr", "+i", "b.txt"], [/* 24 vars */]) = 0
brk(0)                                  = 0x9c23000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=45337, ...}) = 0
mmap2(NULL, 45337, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f01000
close(3)                                = 0
open("/lib/libe2p.so.2", O_RDONLY)      = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\20\221\272\0004\0\0\0"..., 512) = 512
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f00000
fstat64(3, {st_mode=S_IFREG|0755, st_size=21640, ...}) = 0
mmap2(0xba8000, 23536, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xba8000
mmap2(0xbad000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x4) = 0xbad000
close(3)                                = 0
open("/lib/libcom_err.so.2", O_RDONLY)  = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\340\327;\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=7748, ...}) = 0
mmap2(0x3bd000, 9164, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3bd000
mmap2(0x3bf000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1) = 0x3bf000
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\340\257\240\0004\0\0\0"..., 512 ) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1693796, ...}) = 0
mmap2(0x9f5000, 1410500, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0 ) = 0x9f5000
mmap2(0xb48000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x153) = 0xb48000
mmap2(0xb4b000, 9668, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0 ) = 0xb4b000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7eff000
set_thread_area({entry_number:-1 -> 6, base_addr:0xb7eff6c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
mprotect(0xb48000, 8192, PROT_READ)     = 0
mprotect(0x9f1000, 4096, PROT_READ)     = 0
munmap(0xb7f01000, 45337)               = 0
brk(0)                                  = 0x9c23000
brk(0x9c44000)                          = 0x9c44000
lstat64("b.txt", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
stat64("b.txt", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
open("b.txt", O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 3
ioctl(3, EXT2_IOC_GETFLAGS, 0xbf864a78 ) = 0
close(3)                                = 0
stat64("b.txt", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
open("b.txt", O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 3
ioctl(3, EXT2_IOC_SETFLAGS, 0xbf864a78 ) = 0
close(3)                                = 0
exit_group(0)                           = ?
rm: cannot remove `b.txt': Operation not permitted
[root@nagios 0507]# ls
b.txt

梦亿 2022-10-22 07:36:31

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文