Por que 'rm -f! (/ var / www / wp)' deixa arquivos em / var / www?

5

Por que o comando rm -f !(/var/www/wp) não tem efeito? Desejo remover todos os arquivos em /var/www , exceto o diretório /var/www/wp , que deve permanecer.

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
    
por it_is_a_literature 20.03.2015 / 05:24

4 respostas

9

Você pode ler a resposta de Michael Homer para saber o motivo.

Para remover todas as coisas em /var/www excluir wp , POSIXly:

find /var/www -path /var/www/wp -prune -o ! -path /var/www -exec rm -rf {} +
    
por 20.03.2015 / 05:39
37

Se você estiver executando o bash ≥4.3, se você tiver backups, agora seria um bom momento para encontrá-los .

Eu suponho que você esteja usando o Bash. O !(...) padrão de expansão de nome de arquivo se expande para todos os caminhos existentes que não corresponde ao padrão no ponto em que é usado . Isso é:

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

expande para cada nome de arquivo no diretório atual que não seja "/ var / www / wp". Isso é todo arquivo no diretório atual. Em essência, você executou rm -f * em ~ . Não execute o comando rm acima .

Para obter o efeito desejado, use o padrão somente para a parte do caminho que você deseja que (não) corresponda, assim como faria para * , {a,b,c} ou qualquer outro padrão. O comando:

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

imprimirá o comando que você deseja executar.

Eu não, para ser sincero, sugiro fazer as coisas dessa maneira - é propenso a exatamente o tipo de problema que você teve aqui e outros. Algo com find é mais fácil de seguir. No mínimo, echo o comando antes de executá-lo e você verá o que está acontecendo.

    
por 20.03.2015 / 05:35
0

Encontrar, combinação grep e xargs

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

E pode ser convertido em arquivo bash, para propósitos mais generalizados.

#!bash
# $1 -- directory 
# $2 -- Exception filename or directory name
find "$1" -maxdepth 1|grep -v "$2"|xargs rm -rf
    
por 21.03.2015 / 00:56
-2
 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'

Idea for a workaround:

:~$

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 Referência:

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'

usuário @ host: ~ $ chown Referência:

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'
    
por 21.03.2015 / 06:40

Tags