copy files from one path to another path in linux
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:
file-1.txt
file-2.pdf
file-3.ppt
....
I created a .sh
file with the following code:
#!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done
The above code is copying only the last file name from the text instead of all to the destination path.
bash shell-script file-copy
add a comment |
up vote
1
down vote
favorite
I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:
file-1.txt
file-2.pdf
file-3.ppt
....
I created a .sh
file with the following code:
#!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done
The above code is copying only the last file name from the text instead of all to the destination path.
bash shell-script file-copy
protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:
file-1.txt
file-2.pdf
file-3.ppt
....
I created a .sh
file with the following code:
#!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done
The above code is copying only the last file name from the text instead of all to the destination path.
bash shell-script file-copy
I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:
file-1.txt
file-2.pdf
file-3.ppt
....
I created a .sh
file with the following code:
#!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done
The above code is copying only the last file name from the text instead of all to the destination path.
bash shell-script file-copy
bash shell-script file-copy
edited Nov 24 at 20:33
Rui F Ribeiro
38.3k1476127
38.3k1476127
asked Apr 8 '14 at 10:08
Mr_Green
108115
108115
protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51
add a comment |
protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51
protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51
protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/
cd "$fromPath" && xargs mv -t "$toPath" < "$file"
The problem was that each line in the text file was terminating differently instead of justn
.. may be likenr
.. I runsed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
– Mr_Green
Apr 8 '14 at 11:24
1
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
add a comment |
up vote
1
down vote
You might take a look at rsync
, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.
Take a look here, or use your Google foo.
The rsync
option you need is probably --files-from
.
The rsync
incantation will be something like:
rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/
cd "$fromPath" && xargs mv -t "$toPath" < "$file"
The problem was that each line in the text file was terminating differently instead of justn
.. may be likenr
.. I runsed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
– Mr_Green
Apr 8 '14 at 11:24
1
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
add a comment |
up vote
3
down vote
accepted
file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/
cd "$fromPath" && xargs mv -t "$toPath" < "$file"
The problem was that each line in the text file was terminating differently instead of justn
.. may be likenr
.. I runsed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
– Mr_Green
Apr 8 '14 at 11:24
1
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/
cd "$fromPath" && xargs mv -t "$toPath" < "$file"
file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/
cd "$fromPath" && xargs mv -t "$toPath" < "$file"
answered Apr 8 '14 at 11:10
glenn jackman
49.6k569106
49.6k569106
The problem was that each line in the text file was terminating differently instead of justn
.. may be likenr
.. I runsed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
– Mr_Green
Apr 8 '14 at 11:24
1
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
add a comment |
The problem was that each line in the text file was terminating differently instead of justn
.. may be likenr
.. I runsed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
– Mr_Green
Apr 8 '14 at 11:24
1
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
The problem was that each line in the text file was terminating differently instead of just
n
.. may be like nr
.. I run sed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)– Mr_Green
Apr 8 '14 at 11:24
The problem was that each line in the text file was terminating differently instead of just
n
.. may be like nr
.. I run sed 's:r$::' -i filename.txt
at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)– Mr_Green
Apr 8 '14 at 11:24
1
1
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
– Gautam Somani
Apr 9 '14 at 9:29
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
@GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
– Mr_Green
Apr 9 '14 at 9:44
add a comment |
up vote
1
down vote
You might take a look at rsync
, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.
Take a look here, or use your Google foo.
The rsync
option you need is probably --files-from
.
The rsync
incantation will be something like:
rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload
add a comment |
up vote
1
down vote
You might take a look at rsync
, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.
Take a look here, or use your Google foo.
The rsync
option you need is probably --files-from
.
The rsync
incantation will be something like:
rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload
add a comment |
up vote
1
down vote
up vote
1
down vote
You might take a look at rsync
, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.
Take a look here, or use your Google foo.
The rsync
option you need is probably --files-from
.
The rsync
incantation will be something like:
rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload
You might take a look at rsync
, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.
Take a look here, or use your Google foo.
The rsync
option you need is probably --files-from
.
The rsync
incantation will be something like:
rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload
edited Apr 8 '14 at 11:34
answered Apr 8 '14 at 11:27
Stephen Blott
17114
17114
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f123707%2fcopy-files-from-one-path-to-another-path-in-linux%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51