2

I have a list of files with their paths in a text file. And I want to copy these files, mentioned in list, to another directory with their parent folders tree. Also my shell-script to copy these files is in the same directory as this text-file.

Now I have put this text file inside a directory and I want to copy files as listed in the text file from the parent folder of the current directory of text file.

List of files is like below:

../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

Now I know cp --parents can copy the file along with it's parent directories and re-create structure if necessary, but how can I change directory just by passing ../ to the cp command, so that it picks up file from parent folder and not look for it in current folder ?

6
  • Is the directory structure same in your destination? Or the script should make it? May 27, 2016 at 10:53
  • Script should make it, so no extra effort needed.
    – Vicky Dev
    May 27, 2016 at 10:54
  • 1
    I don't understand what you're trying to do here. What arguments do you want to pass to the script, and where should it copy the file? And please give your question a meaningful title: why on earth would you want to change directory without using the command whose job is precisely to do that? May 28, 2016 at 18:00
  • I think I have explained my question pretty clear, I just need to copy specific files from my website root directory (Magento website) to another directory. These specific files I have listed out in a text file. So I want to copy those files along with the directory structure they are residing in, What's so complicated or not clear in that ?
    – Vicky Dev
    May 29, 2016 at 11:03
  • @VickyDev Well, that's not what the original post says. Maybe, tar, cpio or rsync might be a better choice for what you are trying to do. May 30, 2016 at 8:59

2 Answers 2

-1

how can I change directory just by passing ../ to the cp command

You can not pass a "change directory" argument (comparable to the -C option of tar, for example) to any version of cp I have ever seen. Earlier, I suggested simply doing a cd in the shell script, before issuing the cp command, but that was deemed unhelpful, too.

1
  • But that sounds like a not-so-smart work, I want to know if this could be done with cp itself, then how ?
    – Vicky Dev
    May 27, 2016 at 10:14
-1

These are my assumption:

File structure in the original place:

..
├── app
│   ├── code
│   ├── design
│   ├── etc
│   └── locale
│   └── ...
├── script.d
│   ├── file.list
│   ├── script.sh
└── skin
    └── frontend
    └── ...

file.list is the name of the file containing the list of files to be copied. script.sh is the following script.

#! /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

Just make sure you give the correct path in the first line.

6
  • so no need to change file.list, keep it as in my question, right ?
    – Vicky Dev
    May 27, 2016 at 11:19
  • But my destDir is outside the folder of the script.
    – Vicky Dev
    May 27, 2016 at 12:12
  • Keep file.list as it is. But add the path (you can give relative or absolute path like ~/blah/destination for destDir) May 27, 2016 at 14:00
  • I will check this on Monday, as I have Ubuntu in my office system.
    – Vicky Dev
    May 28, 2016 at 6:04
  • 1
    All your uses of unquoted variable expansions, echo | …, etc. cause your script to be needlessly complicated, break on file names containing spaces and wildcard characters, and be slow. As minimum hygiene, use double quotes around variable substitutions, and use shell constructs (e.g. ${#filePath} for the length of the value of a variable) instead of external tools. May 28, 2016 at 17:59

Not the answer you're looking for? Browse other questions tagged .