`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除外項目のすべての項目を削除するにはwpPOSIXly:

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

答え3

検索、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'

関連情報