为什么 `rm -f !(/var/www/wp)` 会将文件留在 /var/www 中?

为什么 `rm -f !(/var/www/wp)` 会将文件留在 /var/www 中?

为什么rm -f !(/var/www/wp)命令没有效果?我想删除 中的所有文件/var/www,但目录除外/var/www/wp,该目录应该保留。

root@born:~# ls  /var/www
authorize.php  index.html          INSTALL.txt      README.txt  UPGRADE.txt
CHANGELOG.txt  index.php           LICENSE.txt      robots.txt  web.config
COPYRIGHT.txt  INSTALL.mysql.txt   MAINTAINERS.txt  scripts wp
cron.php       INSTALL.pgsql.txt   misc             sites       xmlrpc.php
drupal         install.php         modules          themes
includes       INSTALL.sqlite.txt  profiles         update.php
root@born:~# rm  -f  !(/var/www/wp)
root@born:~# ls  /var/www
authorize.php  index.html          INSTALL.txt      README.txt  UPGRADE.txt
CHANGELOG.txt  index.php           LICENSE.txt      robots.txt  web.config
COPYRIGHT.txt  INSTALL.mysql.txt   MAINTAINERS.txt  scripts wp
cron.php       INSTALL.pgsql.txt   misc             sites       xmlrpc.php
drupal         install.php         modules          themes
includes       INSTALL.sqlite.txt  profiles         update.php

答案1

如果您运行的是 bash ≥4.3,那么如果您有备份,现在是找到它们的好时机

我假设您正在使用 Bash。这!(...)文件名扩展模式扩展到与模式不匹配的每个现有路径在使用时。那是:

echo rm  -f  !(/var/www/wp)

扩展到当前目录中除“/var/www/wp”之外的每个文件名。这是当前目录中的每个文件。本质上,你跑rm -f *进来了~不要运行rm上面的命令

要获得您想要的效果,请仅将模式用于您希望(不)匹配的路径部分,就像使用*{a,b,c}或任何其他模式一样。命令:

echo rm -f /var/www/!(wp)

将打印出您想要运行的命令。

老实说,我不建议这样做——它很容易出现你在这里和其他地方遇到的那种问题。带有 的东西find更容易遵循。至少,echo在运行命令之前,您会看到发生了什么。

答案2

你可以阅读迈克尔·霍默的回答知道原因。

要删除/var/www排除中的所有内容wp,POSIXly:

find /var/www -path /var/www/wp -prune -o ! -path /var/www -exec rm -rf {} +

答案3

Find、grep 和 xargs 组合

find /var/www/ -maxdepth 1|grep -v wp|xargs rm -rf

并且可以转换为 bash 文件,以实现更通用的目的。

#!bash
# $1 -- directory 
# $2 -- Exception filename or directory name
find "$1" -maxdepth 1|grep -v "$2"|xargs rm -rf

答案4

 user@host:~$ rm --help
Usage: rm [OPTION]... FILE...
Remove (unlink) the FILE(s).

  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively.  Less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i).  Without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially
      --preserve-root   do not remove '/' (default)
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
      --help     display this help and exit
      --version  output version information and exit

By default, rm does not remove directories.  Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
  rm -- -foo

  rm ./-foo

Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time.  For greater
assurance that the contents are truly unrecoverable, consider using shred.

Report rm bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'rm invocation'

解决方法的想法:

:~$

cd /var/www
    mkdir /opt/move
    mv wp /opt/move/wp
    rm -r *
    mv /opt/wp /var/www/wp
[optional ;)] chown -R www-data:www-data /var/www

user@host:~$ mv 参考:

mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

Report mv bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'mv invocation'

user@host:~$ chown 参考:

chown --help                                                                                                                              
    Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...                                                                                                           
      or:  chown [OPTION]... --reference=RFILE FILE...                                                                                                           
    Change the owner and/or group of each FILE to OWNER and/or GROUP.                                                                                            
    With --reference, change the owner and group of each FILE to those of RFILE.                                                                                 

      -c, --changes          like verbose but report only when a change is made                                                                                  
      -f, --silent, --quiet  suppress most error messages                                                                                                        
      -v, --verbose          output a diagnostic for every file processed                                                                                        
          --dereference      affect the referent of each symbolic link (this is                                                                                  
                             the default), rather than the symbolic link itself                                                                                  
      -h, --no-dereference   affect symbolic links instead of any referenced file                                                                                
                             (useful only on systems that can change the                                                                                         
                             ownership of a symlink)                                                                                                             
          --from=CURRENT_OWNER:CURRENT_GROUP                                                                                                                     
                             change the owner and/or group of each file only if                                                                                  
                             its current owner and/or group match those specified                                                                                
                             here.  Either may be omitted, in which case a match                                                                                 
                             is not required for the omitted attribute                                                                                           
          --no-preserve-root  do not treat '/' specially (the default)                                                                                           
          --preserve-root    fail to operate recursively on '/'                                                                                                  
          --reference=RFILE  use RFILE's owner and group rather than                                                                                             
                             specifying OWNER:GROUP values
      -R, --recursive        operate on files and directories recursively

    The following options modify how a hierarchy is traversed when the -R
    option is also specified.  If more than one is specified, only the final
    one takes effect.

      -H                     if a command line argument is a symbolic link
                             to a directory, traverse it
      -L                     traverse every symbolic link to a directory
                             encountered
      -P                     do not traverse any symbolic links (default)

          --help     display this help and exit
          --version  output version information and exit

    Owner is unchanged if missing.  Group is unchanged if missing, but changed
    to login group if implied by a ':' following a symbolic OWNER.
    OWNER and GROUP may be numeric as well as symbolic.

    Examples:
      chown root /u        Change the owner of /u to "root".
      chown root:staff /u  Likewise, but also change its group to "staff".
      chown -hR root /u    Change the owner of /u and subfiles to "root".

    Report chown bugs to [email protected]
    GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
    General help using GNU software: <http://www.gnu.org/gethelp/>
    For complete documentation, run: info coreutils 'chown invocation'

相关内容