не может запускать виртуальный бокс после перезагрузки

Вот несколько способов сделать то, что вы хотите:

1. find

find . -iname '*html' -type f -exec grep -q election "{}" \; -and -exec mv {} politics/ \; 

Объяснение

Здесь мы используем опцию find -exec:

-exec command ;
      Execute  command;  true  if 0 status is returned.  All following
      arguments to find are taken to be arguments to the command until
      an  argument  consisting of `;' is encountered.  The string `{}'
      is replaced by the current file name being processed

Итак, первые -exec поисковые запросы файл (здесь, представленный {}) для election, а второй - заготовку. [F11] гарантирует, что второй -exec будет запущен только в том случае, если первый был успешным, если файл соответствует шаблону.

2. find & amp; shell

Это тот же базовый подход, что и в ответе Cos64, но с некоторыми улучшениями.

find . -iname '*html' -type f -print0 | 
    while IFS= read -r -d '' file; do 
        grep -q election "$file" && mv "$file" politics/
    done

Объяснение

Команда find найдет все файлы (-type f), чье имя заканчивается на .html (или .HTML, -iname нечувствительно к регистру) и распечатайте их, разделенные символом NULL. Это необходимо, потому что имена файлов в системах * nix могут содержать любой символ, кроме / и \0 (NULL). Таким образом, вы можете иметь файлы с пробелами, символами новой строки и любым другим странным символом. Их нужно лечить специально. while IFS= read -r -d '' file; do ... done: он выполняет итерацию по выходу find, сохраняя каждый файл как $file. Параметр IFS= устанавливает разделитель полей ввода в нуль, что означает, что мы можем правильно обрабатывать пробелы в именах файлов. [F25] позволяет прочитать \0 -сепаратированные строки, а -r позволяет использовать имена файлов, содержащие \. grep -q election "$file": поиск файла для шаблона. [F30] подавляет нормальный выход и делает grep тихим. && echo mv "$file" politics/: && гарантирует, что эта команда будет выполняться только в том случае, если предыдущая (grep) была успешной.

3. Bash.

Этот сценарий очень похож на тот, что есть в очень хорошем ответе @ WilhelmErasmus с той разницей, что i) он может взять набор шаблонов и замен из командной строки и ii) он также находит файлы в подкаталоги.

#!/usr/bin/env bash

## Exit if no arguments were given
[ -z "$1" ] && echo "At least two arguments are needed." >&2 && exit 1
## Collect the arguments
args=("$@")
## Declare the $dirs associative array
declare -A dirs

## Save the arguments given in the $dirs array.
## $# is the number of arguments given, so this
## will iterate over of them, reading two by two.
for ((i=0;i<$#;i+=2)); 
do
    ## The arguments are pairs of patterns and target directories.
    ## Set the value of this pattern to the value of the next argument,
    ## its target directory. 
    dirs[${args[$i]}]="${args[i+1]}"
done

## Ignore globs that match no files
shopt -s nullglob
## This enables ** to match subdirectories
shopt -s globstar
## Find all .html files
for file in **/*{html,htm,HTM,HTML}
do
    matched=0;
    for pat in "${!dirs[@]}"
    do
        ## Does this file match the pattern?
        ## The `-q` suppresses grep's output.
        grep -q "$pat" "$file" && 
        ## Set matched to 1 if the file matches.
        matched=1 &&
        ## If the grep succeeded, move the file
        ## to the corresponding directory
        mv "$file" "${dirs[$pat]}" && 
        ## If the move succeeded, break the loop
        ## and move to the next pattern.
        break 
    done
    ## Report files that didn't match
    [[ "$matched" -eq 0 ]] && printf "No matches for '%s'\n" "$file" >&2
done

Запустите сценарий, указав ему имена шаблонов и их целей. Например, с вашими вопросами:

bash move_files.sh "election" "politics" "stock market" "business" "open source" "computers" 
0
задан 17 January 2018 в 17:44

0 ответов

Другие вопросы по тегам:

Похожие вопросы: