Copying files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying files
# 1  
Old 07-20-2015
Copying files

I'm trying to do this exact same thing, so far I have created this to move files

i've named my script CP.sh

Code:
#!/bin/bash

cd /root/my-documents/NewDir/
for f in *.doc
do cp -v $f root/my-documents/NewDir $f{%.doc}
done

When i go to run this in the console i type, bin/sh/ CP.sh
but it comes up
Code:
bash: /bin/sh/: Not a directory

Moderator's Comments:
Mod Comment This thread was split out from a similar thread started by another user.
Please do not hijack existing threads with a new question.

Last edited by Don Cragun; 07-20-2015 at 06:27 PM.. Reason: Add CODE & ICODE tags. Moved to new thread.
# 2  
Old 07-20-2015
Code:
When i go to run this in the console i type, bin/sh/ CP.sh
but it comes up

Code:

bash: /bin/sh/: Not a directory

The proper command should be /bin/sh CP.sh
Notice the removal of last /
Hint: If you're testing your script that has the potential to do damage, test using echo first to see the result of what it would have been the result.

Last edited by Aia; 07-20-2015 at 07:06 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 07-20-2015
you are missing a '/' in front of your target directory and I don't see what you are using $f{%.doc} for, is that meant to be part of the target file spec
This User Gave Thanks to blackrageous For This Post:
# 4  
Old 07-20-2015
Quote:
Originally Posted by MKTM_93_SIMP
I'm trying to do this exact same thing, so far I have created this to move files

i've named my script CP.sh

Code:
#!/bin/bash

cd /root/my-documents/NewDir/
for f in *.doc
do cp -v $f root/my-documents/NewDir $f{%.doc}
done

When i go to run this in the console i type, bin/sh/ CP.sh
but it comes up
Code:
bash: /bin/sh/: Not a directory

Moderator's Comments:
Mod Comment This thread was split out from a similar thread started by another user.
Please do not hijack existing threads with a new question.
First: Is this a homework assignment? Homework assignments must be posted in the Homework and Coursework Forum and threads started there must include a fully completed homework template.

There are a few problems here:
  1. If you want to execute a bash run it with bash, not sh.
  2. When specifying an absolute pathname for a file; there is a / at the start of the pathname. Putting a / at the end of a pathname declares that the file named by that pathname is a directory (and if there is a file with that name that is not of type directory; it should be ignored for the purposes of resolving that pathname).
  3. Assuming that there is a file with the pathname /root/my-documents/NewDir/x.doc, one of the cp commands run by your loop would try to move the two files /root/my-documents/NewDir/x.doc and /root/my-documents/NewDir/root/my-documents/NewDir to the directory named /root/my-documents/NewDir/x.
Assuming this is not a homework assignment, your immediate problem can be solved by running the script with bash CP.sh (assuming that you have your PATH environment variable set properly) or /bin/bash CP.sh.

But, then you'll run into problems with the cp commands. If you describe what you are trying to do in English, we may be able to help you fix your code.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-20-2015
So I want to copy files from one dir to another but rename the file names in the source dir to the dir name, i was attempting to create one script to copy them and another to rename them? Or is it better to do it in one? Just trying to learn Linux - its harder than I anticipated!

When I run it without the / it comes up with

Code:
cp: cannot stat "*.doc"; No such file or directory

I have files within the source dir with the extensions doc and png

What I have at the minute, I'm not too sure its accurate;

To match with only doc and png file extensions
Code:
match ="(png|.doc)"

Chose working dir
Code:
src = '${1:-}"
dest = "{2:- /root/my-documents/NewDir/}

I'm not sure how to proceed from here, maybe with a for loop?.. and I can't really implement this either, i've got some ideas but it's just putting it together and actually getting the script to run which I find difficult.

Thanks

Last edited by MKTM_93_SIMP; 07-20-2015 at 07:26 PM..
# 6  
Old 07-20-2015
Quote:
but rename the file names in the source dir to the dir name
This is the part I am missing and I quite do not understand.
Could you post an actual real example of just two files and the names you would like them to be?

Side note:
Code:
match ="(png|.doc)"

There's syntax errors on that
No spaces around the `=' , no alternate regex `|'

I could be
Code:
match=( *.png *.doc )

Here's a taste of the result of it:
Code:
[aia@ludus temp]$ match=( *.png *.doc )
[aia@ludus temp]$ echo ${#match[@]}
6
[aia@ludus temp]$ for ((i=0; i<${#match[@]}; i++)); do echo ${match[i]}; done
file1.png
file2.png
file3.png
file1.doc
file2.doc
file3.doc

By the way, you need neither of it to do what you are trying to do.

Code:
src = '${1:-}"
dest = "{2:- /root/my-documents/NewDir/}

There are syntax errors as well. Assignment requires no spaces. Quotes must be the same, mixing ' and " are not valid. Missing one is neither valid.

Last edited by Aia; 07-20-2015 at 08:19 PM..
This User Gave Thanks to Aia For This Post:
# 7  
Old 07-22-2015
So i've managed to get the files moved and renamed however i'm finding errors when I try to put sequential numbers after the file names, my aim would be to split these up i.e sequential numbers after each file name for each file individual file type. This is my attempt;
Code:
num = 1
for f in *.png *.doc
do
cp -v $f $d "$(printf "%u" $num) . png"
let num=num+1
done

Any tips and advice are much appreciated!

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files

All, I need to grab and rename common files from several unique directory structures. For example, the directory structures looks like: /unique_dir/common/common/common/person_name_dir/common_file.txt There are over 90,000 of these text files that I'd like to put in a single directory as... (5 Replies)
Discussion started by: hburnswell
5 Replies

2. Shell Programming and Scripting

Copying files excluding some files

Hi, I have a folder which contains files in this format. abc-bin.000001 abc-bin.000002 abc-bin.000003 abc-bin.000004 abc-bin.000005 abc-bin.000006 abc-bin.000007 abc-bin.000008 abc-bin.000009 abc-bin.000010 abc-bin.index I want to copy all the files between abc-bin.000004... (6 Replies)
Discussion started by: arijitsaha
6 Replies

3. Shell Programming and Scripting

Copying files after result

Hi, I have a shell script #!/bin/sh date echo 'HI PROD' echo $Please ENTER THE INPUT 1 for old files 2 for new file read i if ; then cd /apps/acetp3_logs/prod3/O* pwd echo $PLEASE ENTER THE STRING TO SEARCH (PLEASE ENTER THE STRING INSIDE QUOTES ' ') read j echo... (6 Replies)
Discussion started by: thelakbe
6 Replies

4. Shell Programming and Scripting

Files copying - [ Listed files alone. ] - Shell script

Hi All, I am doing this for svn patch making. I got the list of files to make the patch. I have the list in a file with path of all the files. To Do From Directory : /myproject/MainDir To Directory : /myproject/data List of files need to copy is in the file: /myproject/filesList.txt ... (4 Replies)
Discussion started by: linuxadmin
4 Replies

5. Shell Programming and Scripting

Copying Files

Hi All, I'm trying to list some files from my log directory and files are like this log.20110302_20.gz log.20110302_21.gz log.20110302_22.gz log.20110302_23.gz log.20110303_00.gz log.20110303_01.gz log.20110303_02.gz ............ log.20110311_22.gz log.20110311_23.gz... (2 Replies)
Discussion started by: thelakbe
2 Replies

6. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

7. Solaris

Copying Files

Hi, I understand that to copy files across server, the feasible way will be using scp command. Am I right? What if the two servers are not connected to a network? If by using a cross cable to link up both the server, what will be the best (fastest) way to copy files across? scp as well? ... (3 Replies)
Discussion started by: user50210
3 Replies

8. Shell Programming and Scripting

copying files

hi I want to copy all files from the current directory and move to .archive file. Moreover,I want to add .bak to each file name, that will be copied. How can I do that? (4 Replies)
Discussion started by: tjay83
4 Replies

9. Solaris

Copying Files and

I am new user to solaris and installed solaris operating system on full Harddisk 120Gb. I am unable to copy music files to desktop and /home directory. One thing happened while registering is- i entered login-root and its password. The message prompted your system is crashed. Is it because of... (1 Reply)
Discussion started by: patilmukundraj
1 Replies

10. UNIX for Dummies Questions & Answers

Copying files

I like to know the command structure of copying files/directories from a unix box using telnet session to a windows box. (4 Replies)
Discussion started by: alpheusm
4 Replies
Login or Register to Ask a Question