我有一个文件列表,其路径位于文本文件中。我想将列表中提到的这些文件复制到另一个目录及其父文件夹树。另外,用于复制这些文件的 shell 脚本与此文本文件位于同一目录中。
现在我已将此文本文件放入一个目录中,并且我想从文本文件当前目录的父文件夹中复制文本文件中列出的文件。
文件列表如下:
../app/code/community/Company/Module/Block/Adminhtml/Location.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Edit.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Grid.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Import.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Edit/Form.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Edit/Import.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Edit/Tabs.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Edit/Tab/Form.php
../app/code/community/Company/Module/Block/Adminhtml/Location/Edit/Tab/Import.php
../app/code/community/Company/Module/controllers/IndexController.php
../app/code/community/Company/Module/controllers/LocationController.php
../app/code/community/Company/Module/controllers/Adminhtml/LocationController.php
../app/code/community/Company/Module/etc/adminhtml.xml
../app/code/community/Company/Module/etc/config.xml
../app/code/community/Company/Module/etc/system.xml
../app/code/community/Company/Module/Helper/Data.php
../app/code/community/Company/Module/sql/pmodule_setup/mysql4-install-0.1.0.php
../app/code/community/Company/Module/Model/Defaultcountry.php
../app/code/community/Company/Module/Model/Location.php
../app/code/community/Company/Module/Model/Observer.php
../app/code/community/Company/Module/Model/System/Config/Source/Radiuses.php
../app/code/community/Company/Module/Model/System/Config/Source/RadiusUnits.php
../app/code/community/Company/Module/Model/Mysql4/Location.php
../app/code/community/Company/Module/Model/Mysql4/Location/Collection.php
../app/code/community/Company/Module/Model/Adminhtml/System/Config/Source/Radiuses.php
../app/code/community/Company/Module/Model/Adminhtml/System/Config/Source/RadiusUnits.php
../app/design/adminhtml/default/default/layout/pmodule.xml
../app/design/frontend/default/default/layout/pmodule.xml
../app/design/frontend/default/default/template/company/module/gmap.phtml
../app/etc/modules/Company_Module.xml
../app/locale/de_DE/Company_Module.csv
../app/locale/en_US/Company_Module.csv
../app/locale/fr_FR/Company_Module.csv
../skin/frontend/default/default/module/css/module.css
../skin/frontend/default/default/module/js/jquery-1.8.3.min.js
../skin/frontend/default/default/module/js/module.js
../skin/frontend/default/default/module/js/module_bak_20-05-2016.js
../skin/frontend/default/default/module/js/module_bak_21052016.js
现在我知道cp --parents
可以复制文件及其父目录,并在必要时重新创建结构,但是如何仅通过传递给命令../
来更改目录cp
,以便它从父文件夹中获取文件而不是在当前文件夹中查找它?
答案1
如何仅通过将 ../ 传递给 cp 命令来更改目录
我所见过的任何版本的都不能传递“更改目录”参数(例如,与-C
的选项类似)。之前,我建议在发出命令之前在 shell 脚本中简单地执行,但这也被认为没有帮助。tar
cp
cd
cp
答案2
这些是我的假设:
原位置的文件结构:
..
├── app
│ ├── code
│ ├── design
│ ├── etc
│ └── locale
│ └── ...
├── script.d
│ ├── file.list
│ ├── script.sh
└── skin
└── frontend
└── ...
文件列表是包含要复制的文件列表的文件的名称。脚本文件是下面的脚本。
#! /bin/bash
distDir="THE NEW DIRECTORY PATH" # <<<<<< Alter this line
for filePath in $(cat "file.list");
do
len=$(wc -c "$filePath")
newFilePath=$(cut -c4-$len "$filePath")
tempFileName=$(grep -Po '[^/]+$' "$filePath")
path=$(grep -Po '^.*/' "$newFilePath")
newPath=$distDir
for dir in $(echo "$path" | tr / '\n');
do
if [ $(ls "$newPath" | grep -P '^'$dir'$' | wc -l) = 0 ]
then
mkdir "$newPath"'/'"$dir"
newPath="$newPath"'/'"$dir"
fi
done
cp "$filePath" "$newPath"'/'
done
只需确保在第一行中给出正确的路径即可。