I'm trying to write a script in tcsh that will copy select files that I specify at the command line from one directory to another without having to write out the entire path of each file.

Also, to help clarify, say for example I have 30 of these files in a single directory, all with nearly identical names but I only want 4 of the files to be copied (i.e. I don't think a wildcard could be used here).

As one last note, I'm very new to Unix so any "dumbed down" suggestions on how to write this script are greatly appreciated!

1 Answer 1


The cp command will already do this for you. Try this:

> cd /path/to/files
> cp file1 file2 file3 file4 /path/to/destination

Since you are in the directory where the files reside, you don't need to type out the full path of each file, but you do need to type the full path to the destination directory.

If you really want to write your own script, try this:


cd /path/to/files
cp $* /path/to/destination

put it in a file like move.tcsh and run the command chmod +x move.tcsh to make it executable. This script does the exact same thing as above. In a tcsh script $* represents all command line arguments.

In both cases, make sure you put in your correct paths for /path/to/files and /path/to/destination In this case where the task is so simple, the more correct way to do this is directly from the command line.

  • Thanks for your quick response. I tried what you said and the first file was copied just fine but the second gave this error cp: cannot stat testing_2': No such file or directory" (but I know the file exists). This is what I'm working with... #!/bin/tcsh set raw_files="/infinite1/incoming/$*" foreach files ($raw_files) echo "Copying "basename $raw_files"..." cp -r "$files" "/infinite1/infinite/analyses_Kresub/" echo ""basename $raw_files`" copied successfully" end
    – David
    May 13, 2013 at 18:13
  • also...sorry about the horrible formatting
    – David
    May 13, 2013 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.