`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'

관련 정보