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:


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 ?

  • 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


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.

  • 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

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");
    len=$(wc -c "$filePath")
    newFilePath=$(cut -c4-$len "$filePath")

    tempFileName=$(grep -Po '[^/]+$' "$filePath")

    path=$(grep -Po '^.*/' "$newFilePath")


    for dir in $(echo "$path" | tr / '\n');

        if [ $(ls "$newPath" | grep -P '^'$dir'$' | wc -l) = 0 ]
            mkdir "$newPath"'/'"$dir"


    cp "$filePath" "$newPath"'/'

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

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