I'm using Kali linux which is based on Debian and wrote an script to copy some files from where the script is based to a specified directory. But when I execute my script (called CopyFiles.sh), cp command works on the root, not current directory. Here is my script:


cp -f * /var/www/

Also I tried ./* but same error but if I change directory to my scripts (cd command) it works fine. I can use following script without problem but I don't want to use any additical commands and want to know why my script doesn't work:


SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
cp -f "$SCRIPT_DIR/"* /var/www/
  • Which one of these is correct? That you "wrote a script to copy some files from the current directory" or that you "wrote a script to copy some files from the directory where the script is"? They are not necessarily the same thing and I think that's where the confusion lies. Apr 17, 2015 at 21:14
  • What happens when you try to run the original script? Do you get an error?
    – Barmar
    Apr 17, 2015 at 21:16
  • Do you have any filenames in the current directory that begin with -?
    – Barmar
    Apr 17, 2015 at 21:17
  • @roaima I want to copy files from "Where script is" not "where terminal is and not current directory". I think you can solve my problem :D
    – Shouman
    Apr 17, 2015 at 21:17
  • If you want to run from where the script is, you have to do the second way. Relative pathnames are always interpreted relative to your working directory, the directory containing the script is irrelevant.
    – Barmar
    Apr 17, 2015 at 21:18

1 Answer 1


The command cp -f * /var/www/ copies files matching * in the caller's current directory, i.e. your current directory. It is irrelevant where the script is located.

  • So there is no method to do this without using second script? also is that script stable and won't make any problem later?
    – Shouman
    Apr 17, 2015 at 21:25
  • 1
    The script looks perfectly reasonable to me. It's not how I'd write it - personally I'd require the source directory as a command line argument rather than basing that on the location of the script. But your use case is presumably different to mine. Apr 17, 2015 at 21:49
  • No you don't need a second script. Just put an command line argument in there like so cd $1; cp -f * /var/www/ and when you execute the script just put the directory location in there CopyFiles.sh path-of-direcory
    – Jeight
    Apr 17, 2015 at 23:07
  • 1
    @Jeight in that case why not cp -fp "$1"/* /var/www Apr 18, 2015 at 0:09

You must log in to answer this question.

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