Если вы работаете в оболочке bash, введите help set
.
В конце напечатанной справки прочитайте:
Если не задано никаких ARG, печатаются все переменные оболочки.
Таким образом, команда без аргументов set
печатает все переменные, известные оболочке.
Интуитивным подходом к этой проблеме является:
Я включил основные реализации этого простого алгоритма как в Python, так и в Bash.
Вот скрипт Python, который должен делать то, что вы просили:
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""move_pairs.py
Move pairs of matching *.mp3 and *.flac files from one directory tree to another.
"""
from glob import glob
import os
import shutil
import sys
# Get source and target directories as command-line arguments
source_dir = sys.argv[1]
target_dir = sys.argv[2]
# Recursivley iterate over all files in the source directory with a ".mp3" filename-extension
for mp3_file in glob("{}/**/*.mp3".format(source_dir), recursive=True):
# Create the corresponding ".flac" filename
flac_file = mp3_file[:-3] + "flac"
# Check to see if the ".flac" file exists - if so, then proceed
if os.path.exists(flac_file):
# Create the pair of target paths
new_mp3_path = target_dir + "/" + mp3_file.partition("/")[2]
new_flac_path = target_dir + "/" + flac_file.partition("/")[2]
# Ensure that the target subdirectory exists
os.makedirs(os.path.dirname(new_mp3_path), exist_ok=True)
# Move the files
shutil.move(mp3_file, new_mp3_path)
shutil.move(flac_file, new_flac_path)
Вы можете вызвать его следующим образом:
python move_pairs.py source-directory target-directory
Для проверки я создал следующую файловую иерархию:
.
├── source_dir
│ ├── dir1
│ │ ├── file1.flac
│ │ ├── file1.mp3
│ │ └── file2.mp3
│ └── dir2
│ ├── file3.flac
│ ├── file3.mp3
│ └── file4.mp3
└── target_dir
После запуска скрипта я получил следующий результат:
.
├── source_dir
│ ├── dir1
│ │ └── file2.mp3
│ └── dir2
│ └── file4.mp3
└── target_dir
├── dir1
│ ├── file1.flac
│ └── file1.mp3
└── dir2
├── file3.flac
└── file3.mp3
Вот почти идентичная реализация в Bash:
#!//usr/bin/env bash
# Set globstar shell option to enable recursive globbing
shopt -s globstar
# Get source and target directories as command-line arguments
source_dir="$1"
target_dir="$2"
# Recursively iterate over all files in the source directory with a ".mp3" filename-extension
for mp3_file in "${source_dir}"/**/*.mp3; do
# Create the corresponding ".flac" filename
flac_file="${mp3_file%.mp3}.flac"
# Check to see if the ".flac" file exists - if so, then proceed
if [[ -f "${flac_file}" ]]; then
# Create the pair of target paths
new_mp3_path="${mp3_file/#${source_dir}/${target_dir}}"
new_flac_path="${flac_file/#${source_dir}/${target_dir}}"
# Ensure that the target subdirectory exists
mkdir -p "$(dirname ${new_mp3_path})"
# Move the files
mv -i "${mp3_file}" "${new_mp3_path}"
mv -i "${flac_file}" "${new_flac_path}"
fi
done
Я запустил этот скрипт следующим образом:
bash move_pairs.sh source_dir target_dir
Это дало тот же результат, что и запуск скрипта Python.
Проблема несколько упрощается, если вы рассматриваете эквивалент :для каждого файла FLAC, переместите аналогичный -с именем MP3:
shopt -s globstar
targetroot='/path/to/target'
for f in **/*.flac
do
dir=$(dirname "$f")
mp3=$(basename "$f".flac).mp3
[[ -e "$dir/$mp3" ]] || continue
mkdir -p "$targetroot/$dir"
mv -t "$targetroot/$dir" "$dir/$mp3"
done