Writing a tcsh script to copy files specified in first argument to a specific directory

0

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!

David

Posted 2013-05-13T17:24:26.577

Reputation: 47

Answers

0

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:

#!/bin/tcsh

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.

Sudo Bash

Posted 2013-05-13T17:24:26.577

Reputation: 93

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 – 2013-05-13T18:13:25.843

also...sorry about the horrible formatting – David – 2013-05-13T18:19:30.540