0

I want my script to copy the files in addresses.list (which contains the absolute path of those files) in my folder3 adding to the name of files the variable k (name of the lower folders they came from).

The script makes sense to me but it does not work, it states:"cp: cannot create regular file"; any suggestions?

#!/bin/bash
cd /folder1/

for k in $( cat lowerfolders_list ); do
    for f in $( cat addresses.lst ); do 
        cp $f "/folder1/folder2/folder3/${k}_$f"
        cd /folder1/
    done
done

The exact error I get is this " cp: cannot create regular file `/A/B/C/D/E/folder1/folder2/folder3/name of my $k/path_to_my_file/myfile.sdf': No such file or directory "

EDIT

It would be ok even if I could copy those files naming them only after the $k but when I tried to do this my output is the last file in my addresses.lst multiple times with every name on my lowerfolders_list. This is the script I used:

#!/bin/bash
cd /folder1/

for k in $( cat lowerfolders_list ); do
    for f in $( cat addresses.lst ); do 
        cp "$f" "/folder1/folder2/folder3/${k}"
    done
    cd -
done

Any suggestions?

EDIT Resolved

#!/bin/bash
cd /folder1/

for k in $( cat lowerfolders_list ); do
    for f in $( cat addresses.lst ); do 
    myfile=$( basename "$f" )   
    cp "$f" "/folder1/folder2/folder3/${k}_${myfile}"
    done
    cd -
done

Thanks to all the people that contributed.

alikino
  • 23
  • 4
  • 1
    Does your user have permission to create files in that directory? Also why are you using `cd` in the loop? `cp` doesn't change directories. – Etan Reisner Dec 01 '14 at 16:10
  • I have the permission to do it, the cd in the end of script worked for me once in a similiar script so I kept it in this one but anyway shouldn't affect in a bad way the script... I suppose. – alikino Dec 01 '14 at 16:12
  • Does using `touch "/folder1/folder2/folder3/${k}_$f"` in that loop instead of `cp` work correctly? – Etan Reisner Dec 01 '14 at 16:14
  • Nope, same kind of error: touch: cannot touch – alikino Dec 01 '14 at 16:15
  • Then you need to double check your permissions. What does `ls -l /folder1/{,folder2/{,folder3/}}` say? – Etan Reisner Dec 01 '14 at 16:18
  • Are there whitespaces in the file-/dirnames? – r_3 Dec 01 '14 at 16:25
  • no whitespaces in an name/directory @r_3 – alikino Dec 03 '14 at 08:56
  • Are you trying to copy your files to a file whose name has slashes in it? You can't have a slash in a filename! use `sed -i 's@/@_@g' lowerfolders_list` to substitute the slashes with something that is allowed in a filename. – r_3 Dec 03 '14 at 11:24
  • No I have no / in the lowerfolders_list, i only have some _ but it's allowed in file names. @r_3 – alikino Dec 03 '14 at 12:47
  • If `$k` is a file name, the `[ -d "/folder1/folder2/folder3/" ] || mkdir -p "/folder1/folder2/folder3/"`, then immediately `[ -d "/folder1/folder2/folder3/" ] || printf "error: unable to create /folder1/folder2/folder3/\n"`. It sounds like the directory doesn't exist. Those tests attempt to create the folder, then if it fails, it throws an error. – David C. Rankin Dec 03 '14 at 16:00
  • I'm not sure I understood your comment @DavidC.Rankin , but the path /folder1/folder2/folder3/ already exist and the $k is a list of the names of the folders (without any /) that contains the files in addresses list: the first one in $k correlates to the first one in $f and so on. – alikino Dec 04 '14 at 08:44
  • If you are wanting to write a new file `${k}_$f` in `/folder1/folder2/folder3/`, and `/folder1/folder2/folder3/` exists, then the only reason it would fail is if you do not have `write` permission in `folder3`. That includes you being either **the owner** of `folder3` or a member of **the group** for `folder3` with `folder3` having a minimum octal permission of `0770` listed as `drwxrwx---`. The e`x`ecute permission is required as it controls **descend into** directory permission. Show me `ls -al /folder1/folder2/folder3` – David C. Rankin Dec 04 '14 at 08:53
  • drwxr-xr-x myname sudo 4096 drwxr-xr-x myname sudo 4096 drwxr-xr-x myname sudo 4096 @DavidC.Rankin – alikino Dec 04 '14 at 10:17
  • Do you have to enter a password to executed a command with `sudo`? per your /etc/sudoers file? – David C. Rankin Dec 04 '14 at 17:53
  • Yes I have to write a password, but I resolved the problem with the last script I posted. @DavidC.Rankin – alikino Dec 05 '14 at 09:13
  • Or `myfile="${f##*/}"` or `myfile="${f//*\//}"` both of which prevent spawning a separate subshell. Always good to post a bit of `address.list` which would have disclosed the issue to start with. Glad you got it sorted. – David C. Rankin Dec 05 '14 at 16:15

1 Answers1

1

Sorry but keeping commands that worked for another purpose without understanding what do they do is always going to put you into trouble. In marketing it may appear to work but in computer science, doing something you don't understand breaks very fast. Just stop that practice early. Try to first understand what is the directory structure. Read man cd and man cp and man mkdir.

So the cd command is screwing your script because it changes directory and the list of files and directories you read initially become invalid paths. But maybe I have an idea what were you trying to do.

Second thing is that it's unsafe to read list of dirs and files like that. If they have spaces in the name, it will break bad. But lets leave that for the time being.

Then you're not creating the directory structure "/folder1/folder2/folder3". If that doesn't exist prior running your script, it will also break. Use mkdir -p.

So my best guess for what you're trying to do will be something like that:

for k in $( cat lowerfolders_list ); do
    cd "${k}" 
    for f in $( cat addresses.lst ); do 
        cp "$f" "/folder1/folder2/folder3/${k}_$f"
    done
    cd -
done
akostadinov
  • 17,364
  • 6
  • 77
  • 85
  • Still not working, no folder contains spaces in the name and I already have the destination folder. The error I get is this cp: cannot create regular file `/A/B/C/D/E/folder1/folder2/folder3/name of my $k/path_to_my_file/myfile.sdf': No such file or directory – alikino Dec 03 '14 at 08:30