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.
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
andman cp
andman 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: