I was looking to get some help with copying files in one directory to another using a for-in loop. My script file is called copyfile and here is what I have:
for file in $(ls -a $1)
do
cp $file ~/dir-2
done
When I run copyfile dir-1 this is what I get
cp: omitting directory `.'
cp: omitting directory `..'
cp: cannot stat `abc': No such file or directory
cp: cannot stat `def': No such file or directory
cp: cannot stat `file1': No such file or directory
cp: cannot stat `file2': No such file or directory
cp: cannot stat `file3': No such file or directory
cp: cannot stat `file4': No such file or directory
cp: cannot stat `file5': No such file or directory
cp: cannot stat `file6': No such file or directory
cp: cannot stat `ghi': No such file or directory
Thanks in advance!
---------- Post updated at 02:32 AM ---------- Previous update was at 02:13 AM ----------
I think I may have actually solved the issue myself. It seems to work when I put
for file in $(ls -a $1)
do
cp $1/$file dir-2
done
I would appreciate it if someone could tell me how I'd be able to copy directories and files both.