Issue with copy command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with copy command
# 1  
Old 11-12-2018
Issue with copy command

Hi,
I have a shell script which is having copy command. My need is to copy all the files in a directory into its subdirectory.

My directory structure is like this.

Dir1
file1.txt
file2.txt
file3.txt
subDir1
subDir1 is a subdirectory in Dir1.
Now I am trying to copy all the txt files in Dir1 into subDir1 by excluding subDir1. I used the below command.
cp -r !(subDir1) subDir1
When run the above command in terminal, it is working fine but when I used the same command in shell script, it is giving error as below.

syntax error near unexpected token `('

Please suggest.

Last edited by yuvi; 11-12-2018 at 01:16 PM..
# 2  
Old 11-12-2018
Stupid question number 1: What shell are you using interactively?


Stupid question number 2: How are you invoking your shell-script?


You are probably using bash interactively; most Linux users are. You may be using sh or /bin/sh to run your script. They are different shells, and have different features. Make sure your script is invoked thus:
Code:
bash script

and the first line looks like:
Code:
#!/bin/bash

Now, finally, you are using extended globbing in your copy. Extended globbing is an option in bash, and it has to be turned on. Add this line to the top of your script:
Code:
shopt -s extglob

and let us know if you are still having problems.


Andrew



Andrew
# 3  
Old 11-12-2018
Thank you for the solution. Answers to your questions..
I am using bash and I will run script as sh. I have already #!/bin/bash in my script file.
cp command worked after I placed 'shopt -s extglob' command on top but can you please tell me is there any impact of this code at any other places? I am new to shell scripting, so trying to understand...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy command

Hi , I am trying to take a backup of file before overwriting it with cp command, I am using the command cp -b. -rw-rw-r-- 1 autoengine murex 0 Jan 22 07:08 a -rw-rw-r-- 1 autoengine murex 0 Jan 22 07:08 b cp -b a b -rw-rw-r-- 1 autoengine murex 0 Jan 22 07:08 a -rw-rw-r-- 1... (1 Reply)
Discussion started by: Raj999
1 Replies

2. Solaris

File copy issue in Solaris

Dears, I have one strange problem. When I copy file from source to destination using scp, file size decreased in destination side. I can see that copy progress is successful in source side. But when I check file size using du -sh command file size is 10 time smaller. First I looked into... (6 Replies)
Discussion started by: sembii
6 Replies

3. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

4. UNIX for Dummies Questions & Answers

Issue with Reflection - text copy beyond screen.

Hi, Gretings, While viewing the contents of a file using more/cat in Reflection, I am neither able to see the text beyond the width of screen, nor able to type it as well. I have tried changing windows settings also, but of no vail. Even if I cat/more a file and try to copy its contents, it... (1 Reply)
Discussion started by: dipanchandra
1 Replies

5. Shell Programming and Scripting

Help with copy command

Hello, I have a directory in which I have files as follows CRDT.csv CRDT.csv.1 CRDT.csv.2 .... CRDT.csv.n I would like to copy it over to another directory as crdt_lon.csv crdt_lon.csv.1 crdt_lon.csv.2 .... crdt_lon.csv.n I am looking for a one line command but I am... (5 Replies)
Discussion started by: srattani
5 Replies

6. Shell Programming and Scripting

Help with using grep command with copy command

Hi, im taking an entry Unix class, and as part of my lab assignment I have to copy all files in the /home/david/lab3 directory that have the file extension .save to your lab3/temp directory. I'm having trouble getting the grep to do anything worth while I've been trying to do: cp... (6 Replies)
Discussion started by: Critical jeff
6 Replies

7. UNIX for Dummies Questions & Answers

Issue with the copy command

Hi, I was copying a bunch of files from one folder to another.. while copy command was running i pressed 'ctrl C'...now these files are not present in the parent directory nor in the target folder... :eek: Where can find these files... Is there any way to get this files back.. :confused:... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

8. UNIX for Dummies Questions & Answers

Copy a command string from the command line

How can we copy a command string from a previous command line and paste it into the cursor position on the current command line? I know that ^c will not work as the shell will interpret as an interrupt signal. Thanks, (1 Reply)
Discussion started by: Pouchie1
1 Replies

9. Linux

copy command

sir can any body tell me how i can copy files like copy c:\abc\pqr\mr.txt c:\windows\my documents\this.txt i need command in linux like this really i am a new in linux that is why simple questions alson can any body explain me how i get current directory tree or path in windows... (2 Replies)
Discussion started by: sadiquep
2 Replies

10. Shell Programming and Scripting

copy command

what command would i wrote to copy files from $folder1 to $folder2 ???? (1 Reply)
Discussion started by: perleo
1 Replies
Login or Register to Ask a Question