Move a list of files(in a text file) to a directory?
I have a list of files, with their full paths, one per line in a file "files.txt"
I was trying to move all of those files from their original location to a new directory.
I CDed into the directory where they live now and issued
for file in ~/Desktop/files.txt do mv $file ~/newfolder
but nothing happens. I am sure I am missing something obvious
bash mv
add a comment |
I have a list of files, with their full paths, one per line in a file "files.txt"
I was trying to move all of those files from their original location to a new directory.
I CDed into the directory where they live now and issued
for file in ~/Desktop/files.txt do mv $file ~/newfolder
but nothing happens. I am sure I am missing something obvious
bash mv
1
You need some separators there, try to just echo it with:for file in ~/Desktop/files.txt; do echo $file; done.
– nerdwaller
Jan 18 '13 at 17:01
1
@nerdwaller that won't work, bash will just print "~/Desktop/files.txt" it won't read the file unless explicitly told to either byfor n in $(cat files.txt); do something; doneorwhile read n; do something; done < files.txt.
– terdon
Jan 18 '13 at 17:24
@terdon, I wanted to edit it when I realized I was on non-thinking autopilot but it was past the expiration. Thanks for pointing it out and providing the fix :)
– nerdwaller
Jan 18 '13 at 17:25
add a comment |
I have a list of files, with their full paths, one per line in a file "files.txt"
I was trying to move all of those files from their original location to a new directory.
I CDed into the directory where they live now and issued
for file in ~/Desktop/files.txt do mv $file ~/newfolder
but nothing happens. I am sure I am missing something obvious
bash mv
I have a list of files, with their full paths, one per line in a file "files.txt"
I was trying to move all of those files from their original location to a new directory.
I CDed into the directory where they live now and issued
for file in ~/Desktop/files.txt do mv $file ~/newfolder
but nothing happens. I am sure I am missing something obvious
bash mv
bash mv
edited Apr 2 '14 at 11:14
Der Hochstapler
67.8k49230284
67.8k49230284
asked Jan 18 '13 at 16:54
SteveSteve
242149
242149
1
You need some separators there, try to just echo it with:for file in ~/Desktop/files.txt; do echo $file; done.
– nerdwaller
Jan 18 '13 at 17:01
1
@nerdwaller that won't work, bash will just print "~/Desktop/files.txt" it won't read the file unless explicitly told to either byfor n in $(cat files.txt); do something; doneorwhile read n; do something; done < files.txt.
– terdon
Jan 18 '13 at 17:24
@terdon, I wanted to edit it when I realized I was on non-thinking autopilot but it was past the expiration. Thanks for pointing it out and providing the fix :)
– nerdwaller
Jan 18 '13 at 17:25
add a comment |
1
You need some separators there, try to just echo it with:for file in ~/Desktop/files.txt; do echo $file; done.
– nerdwaller
Jan 18 '13 at 17:01
1
@nerdwaller that won't work, bash will just print "~/Desktop/files.txt" it won't read the file unless explicitly told to either byfor n in $(cat files.txt); do something; doneorwhile read n; do something; done < files.txt.
– terdon
Jan 18 '13 at 17:24
@terdon, I wanted to edit it when I realized I was on non-thinking autopilot but it was past the expiration. Thanks for pointing it out and providing the fix :)
– nerdwaller
Jan 18 '13 at 17:25
1
1
You need some separators there, try to just echo it with:
for file in ~/Desktop/files.txt; do echo $file; done.– nerdwaller
Jan 18 '13 at 17:01
You need some separators there, try to just echo it with:
for file in ~/Desktop/files.txt; do echo $file; done.– nerdwaller
Jan 18 '13 at 17:01
1
1
@nerdwaller that won't work, bash will just print "~/Desktop/files.txt" it won't read the file unless explicitly told to either by
for n in $(cat files.txt); do something; done or while read n; do something; done < files.txt.– terdon
Jan 18 '13 at 17:24
@nerdwaller that won't work, bash will just print "~/Desktop/files.txt" it won't read the file unless explicitly told to either by
for n in $(cat files.txt); do something; done or while read n; do something; done < files.txt.– terdon
Jan 18 '13 at 17:24
@terdon, I wanted to edit it when I realized I was on non-thinking autopilot but it was past the expiration. Thanks for pointing it out and providing the fix :)
– nerdwaller
Jan 18 '13 at 17:25
@terdon, I wanted to edit it when I realized I was on non-thinking autopilot but it was past the expiration. Thanks for pointing it out and providing the fix :)
– nerdwaller
Jan 18 '13 at 17:25
add a comment |
5 Answers
5
active
oldest
votes
bash won't read the contents of the file unless you tell it to.
for file in $(cat ~/Desktop/files.txt); do mv "$file" ~/newfolder; done
If this is on a single line, you would need;between$()anddo.
– nerdwaller
Jan 18 '13 at 17:20
1
@nifle Would this work if the files all had different paths, when a path might be/Users/lombardi/work files/myfile.pngand another in the same file.txt/docs/file.jpg?
– Steve
Jan 18 '13 at 17:56
2
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
@slhck - I don't agree on theuseless use of catdogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what< ~/Desktop/files.txtis not as straight forward.
– Nifle
Jan 18 '13 at 19:14
3
This still breaks on file names that contain spaces. SinceIFSis set to any white space, a file name likeQuarterly report.pdfwill fail above, even though$fileis quoted, since Bash will split loop arguments on the whitespace: first$filewill contain"Quarterly"and on the next iteration it will contain"report.pdf". One workaround is to setIFS='n', but a failsafe solution already exists in the< files.txtversion. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.
– Daniel Andersson
Jan 18 '13 at 20:29
|
show 3 more comments
You need to tell your loop to read the file, otherwise it is just executing:
mv ~/Desktop/files.txt ~/newfolder
In addition, as nerdwaller said, you need separators. Try this:
while read file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
If your paths or file names contain spaces or other strange characters, you may need to do this:
while IFS= read -r file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
Notice the quotes " around the $file variable.
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
1
Yeah, I just checked. Withwhile, it works somewhat okay. It's just the-roption should be used to retain backslashes, and settingIFS=makesreadnot trim any leading or trailing whitespace. It'sforthat causes issues. See: Don't Read Lines With For
– slhck
Jan 18 '13 at 18:29
|
show 3 more comments
If the filenames do not contain whitespace:
mv -t dest_dir $(< text.file)
is probably the most concise way.
If there is whitespace in the filenames
while IFS= read -r filename; do mv "$filename" dest_dir; done < test.file
is safe.
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
add a comment |
Possibly
mv -t dest_dir $(sed 's|^|"|;s|$|"|' text.file)
also deals with filenames with spaces while still using a single mv command and no loop. Untested.
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
add a comment |
Try this:
python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fsuperuser.com%2fquestions%2f538306%2fmove-a-list-of-filesin-a-text-file-to-a-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
bash won't read the contents of the file unless you tell it to.
for file in $(cat ~/Desktop/files.txt); do mv "$file" ~/newfolder; done
If this is on a single line, you would need;between$()anddo.
– nerdwaller
Jan 18 '13 at 17:20
1
@nifle Would this work if the files all had different paths, when a path might be/Users/lombardi/work files/myfile.pngand another in the same file.txt/docs/file.jpg?
– Steve
Jan 18 '13 at 17:56
2
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
@slhck - I don't agree on theuseless use of catdogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what< ~/Desktop/files.txtis not as straight forward.
– Nifle
Jan 18 '13 at 19:14
3
This still breaks on file names that contain spaces. SinceIFSis set to any white space, a file name likeQuarterly report.pdfwill fail above, even though$fileis quoted, since Bash will split loop arguments on the whitespace: first$filewill contain"Quarterly"and on the next iteration it will contain"report.pdf". One workaround is to setIFS='n', but a failsafe solution already exists in the< files.txtversion. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.
– Daniel Andersson
Jan 18 '13 at 20:29
|
show 3 more comments
bash won't read the contents of the file unless you tell it to.
for file in $(cat ~/Desktop/files.txt); do mv "$file" ~/newfolder; done
If this is on a single line, you would need;between$()anddo.
– nerdwaller
Jan 18 '13 at 17:20
1
@nifle Would this work if the files all had different paths, when a path might be/Users/lombardi/work files/myfile.pngand another in the same file.txt/docs/file.jpg?
– Steve
Jan 18 '13 at 17:56
2
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
@slhck - I don't agree on theuseless use of catdogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what< ~/Desktop/files.txtis not as straight forward.
– Nifle
Jan 18 '13 at 19:14
3
This still breaks on file names that contain spaces. SinceIFSis set to any white space, a file name likeQuarterly report.pdfwill fail above, even though$fileis quoted, since Bash will split loop arguments on the whitespace: first$filewill contain"Quarterly"and on the next iteration it will contain"report.pdf". One workaround is to setIFS='n', but a failsafe solution already exists in the< files.txtversion. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.
– Daniel Andersson
Jan 18 '13 at 20:29
|
show 3 more comments
bash won't read the contents of the file unless you tell it to.
for file in $(cat ~/Desktop/files.txt); do mv "$file" ~/newfolder; done
bash won't read the contents of the file unless you tell it to.
for file in $(cat ~/Desktop/files.txt); do mv "$file" ~/newfolder; done
edited Jan 18 '13 at 19:16
answered Jan 18 '13 at 17:16
NifleNifle
28k2393128
28k2393128
If this is on a single line, you would need;between$()anddo.
– nerdwaller
Jan 18 '13 at 17:20
1
@nifle Would this work if the files all had different paths, when a path might be/Users/lombardi/work files/myfile.pngand another in the same file.txt/docs/file.jpg?
– Steve
Jan 18 '13 at 17:56
2
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
@slhck - I don't agree on theuseless use of catdogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what< ~/Desktop/files.txtis not as straight forward.
– Nifle
Jan 18 '13 at 19:14
3
This still breaks on file names that contain spaces. SinceIFSis set to any white space, a file name likeQuarterly report.pdfwill fail above, even though$fileis quoted, since Bash will split loop arguments on the whitespace: first$filewill contain"Quarterly"and on the next iteration it will contain"report.pdf". One workaround is to setIFS='n', but a failsafe solution already exists in the< files.txtversion. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.
– Daniel Andersson
Jan 18 '13 at 20:29
|
show 3 more comments
If this is on a single line, you would need;between$()anddo.
– nerdwaller
Jan 18 '13 at 17:20
1
@nifle Would this work if the files all had different paths, when a path might be/Users/lombardi/work files/myfile.pngand another in the same file.txt/docs/file.jpg?
– Steve
Jan 18 '13 at 17:56
2
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
@slhck - I don't agree on theuseless use of catdogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what< ~/Desktop/files.txtis not as straight forward.
– Nifle
Jan 18 '13 at 19:14
3
This still breaks on file names that contain spaces. SinceIFSis set to any white space, a file name likeQuarterly report.pdfwill fail above, even though$fileis quoted, since Bash will split loop arguments on the whitespace: first$filewill contain"Quarterly"and on the next iteration it will contain"report.pdf". One workaround is to setIFS='n', but a failsafe solution already exists in the< files.txtversion. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.
– Daniel Andersson
Jan 18 '13 at 20:29
If this is on a single line, you would need
; between $() and do.– nerdwaller
Jan 18 '13 at 17:20
If this is on a single line, you would need
; between $() and do.– nerdwaller
Jan 18 '13 at 17:20
1
1
@nifle Would this work if the files all had different paths, when a path might be
/Users/lombardi/work files/myfile.png and another in the same file.txt /docs/file.jpg?– Steve
Jan 18 '13 at 17:56
@nifle Would this work if the files all had different paths, when a path might be
/Users/lombardi/work files/myfile.png and another in the same file.txt /docs/file.jpg?– Steve
Jan 18 '13 at 17:56
2
2
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
Useless use of cat, and your script breaks in multiple ways, even if a file just contains a space in its path. Please fix this, as this kind of a common cause for problems.
– slhck
Jan 18 '13 at 18:06
@slhck - I don't agree on the
useless use of cat dogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what < ~/Desktop/files.txt is not as straight forward.– Nifle
Jan 18 '13 at 19:14
@slhck - I don't agree on the
useless use of cat dogma, especially for oneliners like this. Finding out what cat does if you are unfamilliar is simple, trying to understand what < ~/Desktop/files.txt is not as straight forward.– Nifle
Jan 18 '13 at 19:14
3
3
This still breaks on file names that contain spaces. Since
IFS is set to any white space, a file name like Quarterly report.pdf will fail above, even though $file is quoted, since Bash will split loop arguments on the whitespace: first $file will contain "Quarterly" and on the next iteration it will contain "report.pdf". One workaround is to set IFS='n', but a failsafe solution already exists in the < files.txt version. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.– Daniel Andersson
Jan 18 '13 at 20:29
This still breaks on file names that contain spaces. Since
IFS is set to any white space, a file name like Quarterly report.pdf will fail above, even though $file is quoted, since Bash will split loop arguments on the whitespace: first $file will contain "Quarterly" and on the next iteration it will contain "report.pdf". One workaround is to set IFS='n', but a failsafe solution already exists in the < files.txt version. This is one of the reasons that it is not good to learn people to loop like this, since it will break sooner or later.– Daniel Andersson
Jan 18 '13 at 20:29
|
show 3 more comments
You need to tell your loop to read the file, otherwise it is just executing:
mv ~/Desktop/files.txt ~/newfolder
In addition, as nerdwaller said, you need separators. Try this:
while read file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
If your paths or file names contain spaces or other strange characters, you may need to do this:
while IFS= read -r file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
Notice the quotes " around the $file variable.
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
1
Yeah, I just checked. Withwhile, it works somewhat okay. It's just the-roption should be used to retain backslashes, and settingIFS=makesreadnot trim any leading or trailing whitespace. It'sforthat causes issues. See: Don't Read Lines With For
– slhck
Jan 18 '13 at 18:29
|
show 3 more comments
You need to tell your loop to read the file, otherwise it is just executing:
mv ~/Desktop/files.txt ~/newfolder
In addition, as nerdwaller said, you need separators. Try this:
while read file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
If your paths or file names contain spaces or other strange characters, you may need to do this:
while IFS= read -r file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
Notice the quotes " around the $file variable.
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
1
Yeah, I just checked. Withwhile, it works somewhat okay. It's just the-roption should be used to retain backslashes, and settingIFS=makesreadnot trim any leading or trailing whitespace. It'sforthat causes issues. See: Don't Read Lines With For
– slhck
Jan 18 '13 at 18:29
|
show 3 more comments
You need to tell your loop to read the file, otherwise it is just executing:
mv ~/Desktop/files.txt ~/newfolder
In addition, as nerdwaller said, you need separators. Try this:
while read file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
If your paths or file names contain spaces or other strange characters, you may need to do this:
while IFS= read -r file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
Notice the quotes " around the $file variable.
You need to tell your loop to read the file, otherwise it is just executing:
mv ~/Desktop/files.txt ~/newfolder
In addition, as nerdwaller said, you need separators. Try this:
while read file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
If your paths or file names contain spaces or other strange characters, you may need to do this:
while IFS= read -r file; do mv "$file" ~/newfolder; done < ~/Desktop/files.txt
Notice the quotes " around the $file variable.
edited Jan 18 '13 at 18:21
answered Jan 18 '13 at 17:16
terdonterdon
41.5k888137
41.5k888137
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
1
Yeah, I just checked. Withwhile, it works somewhat okay. It's just the-roption should be used to retain backslashes, and settingIFS=makesreadnot trim any leading or trailing whitespace. It'sforthat causes issues. See: Don't Read Lines With For
– slhck
Jan 18 '13 at 18:29
|
show 3 more comments
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
1
Yeah, I just checked. Withwhile, it works somewhat okay. It's just the-roption should be used to retain backslashes, and settingIFS=makesreadnot trim any leading or trailing whitespace. It'sforthat causes issues. See: Don't Read Lines With For
– slhck
Jan 18 '13 at 18:29
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks for editing mine, I realized my error but was unable to correct (comment edit time limit) and was writing an answer when yours popped up. Thanks!
– nerdwaller
Jan 18 '13 at 17:21
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
Thanks, that works too but I found the other post to be easier to remember for the future.
– Steve
Jan 18 '13 at 17:47
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
No worries, they are both correct.
– terdon
Jan 18 '13 at 17:50
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
I was going to make the same edit as you did, thanks :) (Sorry, it's just that there are so many "good enough" or mostly wrong Bash loops around on the Internet…)
– slhck
Jan 18 '13 at 18:15
1
1
Yeah, I just checked. With
while, it works somewhat okay. It's just the -r option should be used to retain backslashes, and setting IFS= makes read not trim any leading or trailing whitespace. It's for that causes issues. See: Don't Read Lines With For– slhck
Jan 18 '13 at 18:29
Yeah, I just checked. With
while, it works somewhat okay. It's just the -r option should be used to retain backslashes, and setting IFS= makes read not trim any leading or trailing whitespace. It's for that causes issues. See: Don't Read Lines With For– slhck
Jan 18 '13 at 18:29
|
show 3 more comments
If the filenames do not contain whitespace:
mv -t dest_dir $(< text.file)
is probably the most concise way.
If there is whitespace in the filenames
while IFS= read -r filename; do mv "$filename" dest_dir; done < test.file
is safe.
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
add a comment |
If the filenames do not contain whitespace:
mv -t dest_dir $(< text.file)
is probably the most concise way.
If there is whitespace in the filenames
while IFS= read -r filename; do mv "$filename" dest_dir; done < test.file
is safe.
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
add a comment |
If the filenames do not contain whitespace:
mv -t dest_dir $(< text.file)
is probably the most concise way.
If there is whitespace in the filenames
while IFS= read -r filename; do mv "$filename" dest_dir; done < test.file
is safe.
If the filenames do not contain whitespace:
mv -t dest_dir $(< text.file)
is probably the most concise way.
If there is whitespace in the filenames
while IFS= read -r filename; do mv "$filename" dest_dir; done < test.file
is safe.
answered Jan 18 '13 at 20:40
glenn jackmanglenn jackman
16k22645
16k22645
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
add a comment |
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
The mv command only supports the -t flag on systems with the GNU version of mv; this version won't work on OS X or non-GNU UNIX derivatives.
– Craig Finch
Jan 29 at 17:56
add a comment |
Possibly
mv -t dest_dir $(sed 's|^|"|;s|$|"|' text.file)
also deals with filenames with spaces while still using a single mv command and no loop. Untested.
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
add a comment |
Possibly
mv -t dest_dir $(sed 's|^|"|;s|$|"|' text.file)
also deals with filenames with spaces while still using a single mv command and no loop. Untested.
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
add a comment |
Possibly
mv -t dest_dir $(sed 's|^|"|;s|$|"|' text.file)
also deals with filenames with spaces while still using a single mv command and no loop. Untested.
Possibly
mv -t dest_dir $(sed 's|^|"|;s|$|"|' text.file)
also deals with filenames with spaces while still using a single mv command and no loop. Untested.
answered Feb 20 '16 at 17:21
lo guestolo guesto
1
1
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
add a comment |
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
There are three other answers, all upvoted and one accepted. This answer is unexplained code that you labeled as untested. Why would someone use this answer?
– fixer1234
Feb 20 '16 at 18:03
add a comment |
Try this:
python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
add a comment |
Try this:
python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
add a comment |
Try this:
python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"
Try this:
python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"
answered Jan 23 at 7:49
Miao XuMiao Xu
1
1
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
add a comment |
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
(1) Can you explain this? (And have you tested it?) (2) Does this have any advantage over the other answers? … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
– Scott
Jan 23 at 8:18
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f538306%2fmove-a-list-of-filesin-a-text-file-to-a-directory%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
1
You need some separators there, try to just echo it with:
for file in ~/Desktop/files.txt; do echo $file; done.– nerdwaller
Jan 18 '13 at 17:01
1
@nerdwaller that won't work, bash will just print "~/Desktop/files.txt" it won't read the file unless explicitly told to either by
for n in $(cat files.txt); do something; doneorwhile read n; do something; done < files.txt.– terdon
Jan 18 '13 at 17:24
@terdon, I wanted to edit it when I realized I was on non-thinking autopilot but it was past the expiration. Thanks for pointing it out and providing the fix :)
– nerdwaller
Jan 18 '13 at 17:25