Linux (mv or cp) specific files from a text list of files?Suggest a lightning fast, feature-light, secure...
Make a transparent 448*448 image
How do anti-virus programs start at Windows boot?
Do I need life insurance if I can cover my own funeral costs?
What has been your most complicated TikZ drawing?
I need to drive a 7/16" nut but am unsure how to use the socket I bought for my screwdriver
Is it normal that my co-workers at a fitness company criticize my food choices?
Welcoming 2019 Pi day: How to draw the letter π?
Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?
How to deal with a cynical class?
Is Mortgage interest accrued after a December payment tax deductible?
Counting certain elements in lists
Happy pi day, everyone!
Schematic conventions for different supply rails
How to deal with taxi scam when on vacation?
Sword in the Stone story where the sword was held in place by electromagnets
Making a sword in the stone, in a medieval world without magic
How to make healing in an exploration game interesting
Instead of Universal Basic Income, why not Universal Basic NEEDS?
Rules about breaking the rules. How do I do it well?
How to explain that I do not want to visit a country due to personal safety concern?
Theorems like the Lovász Local Lemma?
My adviser wants to be the first author
SQL Server Primary Login Restrictions
Brexit - No Deal Rejection
Linux (mv or cp) specific files from a text list of files?
Suggest a lightning fast, feature-light, secure Linux web server to serve static contentText ProcessingIgnore windows hidden files when copying with rsyncsearch files based on text file containing list of filesMigrating Bugzilla - Can't find data directorysuggestions on scripting a move of lots of files from one linux host to anothermounting a linux network shared directory onto a directory of another linux serverzcat/gzcat works in linux, not on osx. general linux/osx compatibilitylinux: find files from a list in txt, the files contain spacesLinux - Find -exec command optimization
I have a directory of many files, something like 50,000 pdf's and other files on a server. I need to move specific ones to another directory. I can generate a list of the files that need to be moved either in csv or any other text format.
What I need to do is run a bash script and move or copy the files that are listed in the text file to another directory.
Is there an easy way of doing this? Any suggestions or resources would be greatly appreciated.
linux files cp mv
add a comment |
I have a directory of many files, something like 50,000 pdf's and other files on a server. I need to move specific ones to another directory. I can generate a list of the files that need to be moved either in csv or any other text format.
What I need to do is run a bash script and move or copy the files that are listed in the text file to another directory.
Is there an easy way of doing this? Any suggestions or resources would be greatly appreciated.
linux files cp mv
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files.
– Jestep
Dec 14 '10 at 14:09
add a comment |
I have a directory of many files, something like 50,000 pdf's and other files on a server. I need to move specific ones to another directory. I can generate a list of the files that need to be moved either in csv or any other text format.
What I need to do is run a bash script and move or copy the files that are listed in the text file to another directory.
Is there an easy way of doing this? Any suggestions or resources would be greatly appreciated.
linux files cp mv
I have a directory of many files, something like 50,000 pdf's and other files on a server. I need to move specific ones to another directory. I can generate a list of the files that need to be moved either in csv or any other text format.
What I need to do is run a bash script and move or copy the files that are listed in the text file to another directory.
Is there an easy way of doing this? Any suggestions or resources would be greatly appreciated.
linux files cp mv
linux files cp mv
asked Dec 14 '10 at 14:07
JestepJestep
3031311
3031311
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files.
– Jestep
Dec 14 '10 at 14:09
add a comment |
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files.
– Jestep
Dec 14 '10 at 14:09
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files.
– Jestep
Dec 14 '10 at 14:09
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files.
– Jestep
Dec 14 '10 at 14:09
add a comment |
10 Answers
10
active
oldest
votes
rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).
For example, this will do the trick:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
2
example:rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
– anneb
Oct 31 '18 at 14:04
add a comment |
In order to avoid a useless use of cat (and if you don't use rsync):
xargs -a file_list.txt mv -t /path/to/dest
This will handle any valid filename, unless it contains a newline, if the files are listed one per line.
2
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use ofmvwill collapse all of them into the same destination directory. Thersyncapproach in the accepted answer doesn't suffer from this limitation.
– Jason R
May 4 '12 at 16:10
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
add a comment |
for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done
assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
3
Nice, just I had problems with spaces, so I modified it to:while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
– user1182474
Feb 28 '16 at 14:19
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
add a comment |
This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:
$ cat your_text_file | xargs cp -t /path/to/destination
Also, you can use find command with -exec option. to copy/move the files.
add a comment |
rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/
Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.
Why is/path/to/sourceneeded when it takes the paths from the file list?
– bzero
Jun 26 '17 at 8:29
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
add a comment |
I think the rsync answer is a better one, but just for another option:
tar -cf - -T FILE_OF_FILENAMES_TO_MOVE.txt |(cd /path/to/new/dir && tar -xvf -)
add a comment |
I accidentally copied the full contents of a directory into a destination directory instead of moving the full directory. This resulted in a cluttered destination directory instead of the origin directory being added to the directory.
To fix this, I did the following:
ls -rt /path/to/cluttered/destination/directory/ > /opt/dircheck/filestomove
The above command creates the filestomove file that will be a list of all contents of the destination directory, reverse sorted by time, meaning oldest to newest.
Then I created a sub-directory of the now cluttered destination directory to move the stuff into.
mkdir /path/to/cluttered/destination/directory/newsubdirectory
Then I repeated the directory listing, except listing to screen and showing more details.
ls -lrht /path/to/cluttered/destination/directory/
This line lists the directory, sorted by date ascending (reverse sort by time) and shows more information, including the date/time stamp of each file in the now cluttered destination directory. I refer to this, starting at the top to show what directories and files I want to keep where they were. There will be a gap in the date/time stamp of the files where all the new files start that shouldn't be there.
Then I edited the filestomove file created in the first step above (that is sorted by date) and deleted the few from the list that were there previously that I want to stay from the original directory.
vim /opt/dircheck/filestomove Delete from the top, all files you don't want to move.
Then I used the command listed previously in this post to move the files in my list to the new directory I made.
sudo xargs -a /opt/dircheck/filestomove mv -t /path/to/cluttered/destination/directory/newsubdirectory
This moved all the files in a split second. (Note: you may not need the sudo at the beginning, this is a matter of file permissions).
Now my original destination directory is nice and clean and contains a new directory with all the files and directories that were cluttering it.
add a comment |
If (and only if), you don't have any nasty characters in the filenames (spaces, newlines, which would confuse xargs on how to break things into individual arguments), and you generate a list of files separated by newlines (one file per line), you could do something like.
cat filenames.txt | xargs mv -t /path/to/move/files/to
(In general, see man xargs, it's awesome)
If your particular mv doesn't have the -t option, you could also do some trickery like
( cat filenames.txt; echo; echo /path/to/move/files/to ) | xargs mv
Note - neither of these will work as expected if there happens to be filenames with newlines in them.
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
cp,mvand the like do not have-toption on bsd's, and the second example with subshell did not work for me (/path/to/moveis appended as the last filename)
– w17t
Jan 9 at 17:04
1
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv
– Kjetil Joergensen
Jan 10 at 23:38
add a comment |
Try something like:
cat list.txt | while read line; do mv "$line" /images; done
could have beenwhile read line; do mv $line /images; done < list.txtas a single command
– ignivs
Sep 17 '15 at 19:12
add a comment |
The following worked for me where I needed to copy all the PNG files from a specific path (and all subdirs) to a new location while preserving the directory structure:
rsync -av --prune-empty-dirs --include='*/' --include='*.png' --exclude='*' source/ destination/
Because RSYNC creates the mirror of the directory structure first, then syncs the files, you can end up with excess folders that are empty. I used the --prune-empty-dirs flag to remove these empty directories.
I have no affiliation, but thought it right to give credit to the source that inspired this solution: http://techblog.zabuchy.net/2011/transfer-only-selected-file-types-with-rsync/
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "2"
};
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%2fserverfault.com%2fquestions%2f212439%2flinux-mv-or-cp-specific-files-from-a-text-list-of-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).
For example, this will do the trick:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
2
example:rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
– anneb
Oct 31 '18 at 14:04
add a comment |
rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).
For example, this will do the trick:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
2
example:rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
– anneb
Oct 31 '18 at 14:04
add a comment |
rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).
For example, this will do the trick:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).
For example, this will do the trick:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
edited 6 mins ago
Vadim Kotov
1094
1094
answered Dec 14 '10 at 14:15
Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams
39.2k45973
39.2k45973
2
example:rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
– anneb
Oct 31 '18 at 14:04
add a comment |
2
example:rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
– anneb
Oct 31 '18 at 14:04
2
2
example:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory– anneb
Oct 31 '18 at 14:04
example:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory– anneb
Oct 31 '18 at 14:04
add a comment |
In order to avoid a useless use of cat (and if you don't use rsync):
xargs -a file_list.txt mv -t /path/to/dest
This will handle any valid filename, unless it contains a newline, if the files are listed one per line.
2
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use ofmvwill collapse all of them into the same destination directory. Thersyncapproach in the accepted answer doesn't suffer from this limitation.
– Jason R
May 4 '12 at 16:10
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
add a comment |
In order to avoid a useless use of cat (and if you don't use rsync):
xargs -a file_list.txt mv -t /path/to/dest
This will handle any valid filename, unless it contains a newline, if the files are listed one per line.
2
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use ofmvwill collapse all of them into the same destination directory. Thersyncapproach in the accepted answer doesn't suffer from this limitation.
– Jason R
May 4 '12 at 16:10
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
add a comment |
In order to avoid a useless use of cat (and if you don't use rsync):
xargs -a file_list.txt mv -t /path/to/dest
This will handle any valid filename, unless it contains a newline, if the files are listed one per line.
In order to avoid a useless use of cat (and if you don't use rsync):
xargs -a file_list.txt mv -t /path/to/dest
This will handle any valid filename, unless it contains a newline, if the files are listed one per line.
answered Dec 14 '10 at 15:02
Dennis WilliamsonDennis Williamson
50.5k1191127
50.5k1191127
2
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use ofmvwill collapse all of them into the same destination directory. Thersyncapproach in the accepted answer doesn't suffer from this limitation.
– Jason R
May 4 '12 at 16:10
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
add a comment |
2
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use ofmvwill collapse all of them into the same destination directory. Thersyncapproach in the accepted answer doesn't suffer from this limitation.
– Jason R
May 4 '12 at 16:10
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
2
2
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use of
mv will collapse all of them into the same destination directory. The rsync approach in the accepted answer doesn't suffer from this limitation.– Jason R
May 4 '12 at 16:10
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use of
mv will collapse all of them into the same destination directory. The rsync approach in the accepted answer doesn't suffer from this limitation.– Jason R
May 4 '12 at 16:10
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote")
– James Beninger
Aug 17 '12 at 1:30
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
This worked for me because I actually wanted the files from different source paths in the same target directory.
– PseudoNoise
Apr 29 '15 at 17:17
add a comment |
for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done
assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
3
Nice, just I had problems with spaces, so I modified it to:while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
– user1182474
Feb 28 '16 at 14:19
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
add a comment |
for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done
assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
3
Nice, just I had problems with spaces, so I modified it to:while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
– user1182474
Feb 28 '16 at 14:19
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
add a comment |
for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done
assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!
for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done
assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!
answered Dec 14 '10 at 15:12
MadHatterMadHatter
70.3k11145207
70.3k11145207
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
3
Nice, just I had problems with spaces, so I modified it to:while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
– user1182474
Feb 28 '16 at 14:19
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
add a comment |
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
3
Nice, just I had problems with spaces, so I modified it to:while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
– user1182474
Feb 28 '16 at 14:19
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
– Spamwich
Aug 6 '15 at 20:50
3
3
Nice, just I had problems with spaces, so I modified it to:
while read -r file; do mv "$file" /path/of/destination ; done < listoffiles– user1182474
Feb 28 '16 at 14:19
Nice, just I had problems with spaces, so I modified it to:
while read -r file; do mv "$file" /path/of/destination ; done < listoffiles– user1182474
Feb 28 '16 at 14:19
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
– MadHatter
Feb 28 '16 at 17:36
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
– Charlie Gorichanaz
Sep 15 '16 at 3:24
add a comment |
This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:
$ cat your_text_file | xargs cp -t /path/to/destination
Also, you can use find command with -exec option. to copy/move the files.
add a comment |
This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:
$ cat your_text_file | xargs cp -t /path/to/destination
Also, you can use find command with -exec option. to copy/move the files.
add a comment |
This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:
$ cat your_text_file | xargs cp -t /path/to/destination
Also, you can use find command with -exec option. to copy/move the files.
This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:
$ cat your_text_file | xargs cp -t /path/to/destination
Also, you can use find command with -exec option. to copy/move the files.
answered Dec 14 '10 at 14:35
KhaledKhaled
31.4k65487
31.4k65487
add a comment |
add a comment |
rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/
Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.
Why is/path/to/sourceneeded when it takes the paths from the file list?
– bzero
Jun 26 '17 at 8:29
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
add a comment |
rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/
Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.
Why is/path/to/sourceneeded when it takes the paths from the file list?
– bzero
Jun 26 '17 at 8:29
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
add a comment |
rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/
Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.
rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/
Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.
answered Sep 17 '15 at 19:02
Paul WenzelPaul Wenzel
1213
1213
Why is/path/to/sourceneeded when it takes the paths from the file list?
– bzero
Jun 26 '17 at 8:29
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
add a comment |
Why is/path/to/sourceneeded when it takes the paths from the file list?
– bzero
Jun 26 '17 at 8:29
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
Why is
/path/to/source needed when it takes the paths from the file list?– bzero
Jun 26 '17 at 8:29
Why is
/path/to/source needed when it takes the paths from the file list?– bzero
Jun 26 '17 at 8:29
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
@bzero maybe list of files can contain relative paths?
– DreadfulWeather
Jan 28 at 14:55
add a comment |
I think the rsync answer is a better one, but just for another option:
tar -cf - -T FILE_OF_FILENAMES_TO_MOVE.txt |(cd /path/to/new/dir && tar -xvf -)
add a comment |
I think the rsync answer is a better one, but just for another option:
tar -cf - -T FILE_OF_FILENAMES_TO_MOVE.txt |(cd /path/to/new/dir && tar -xvf -)
add a comment |
I think the rsync answer is a better one, but just for another option:
tar -cf - -T FILE_OF_FILENAMES_TO_MOVE.txt |(cd /path/to/new/dir && tar -xvf -)
I think the rsync answer is a better one, but just for another option:
tar -cf - -T FILE_OF_FILENAMES_TO_MOVE.txt |(cd /path/to/new/dir && tar -xvf -)
answered Dec 14 '10 at 14:26
jj33jj33
10.1k2949
10.1k2949
add a comment |
add a comment |
I accidentally copied the full contents of a directory into a destination directory instead of moving the full directory. This resulted in a cluttered destination directory instead of the origin directory being added to the directory.
To fix this, I did the following:
ls -rt /path/to/cluttered/destination/directory/ > /opt/dircheck/filestomove
The above command creates the filestomove file that will be a list of all contents of the destination directory, reverse sorted by time, meaning oldest to newest.
Then I created a sub-directory of the now cluttered destination directory to move the stuff into.
mkdir /path/to/cluttered/destination/directory/newsubdirectory
Then I repeated the directory listing, except listing to screen and showing more details.
ls -lrht /path/to/cluttered/destination/directory/
This line lists the directory, sorted by date ascending (reverse sort by time) and shows more information, including the date/time stamp of each file in the now cluttered destination directory. I refer to this, starting at the top to show what directories and files I want to keep where they were. There will be a gap in the date/time stamp of the files where all the new files start that shouldn't be there.
Then I edited the filestomove file created in the first step above (that is sorted by date) and deleted the few from the list that were there previously that I want to stay from the original directory.
vim /opt/dircheck/filestomove Delete from the top, all files you don't want to move.
Then I used the command listed previously in this post to move the files in my list to the new directory I made.
sudo xargs -a /opt/dircheck/filestomove mv -t /path/to/cluttered/destination/directory/newsubdirectory
This moved all the files in a split second. (Note: you may not need the sudo at the beginning, this is a matter of file permissions).
Now my original destination directory is nice and clean and contains a new directory with all the files and directories that were cluttering it.
add a comment |
I accidentally copied the full contents of a directory into a destination directory instead of moving the full directory. This resulted in a cluttered destination directory instead of the origin directory being added to the directory.
To fix this, I did the following:
ls -rt /path/to/cluttered/destination/directory/ > /opt/dircheck/filestomove
The above command creates the filestomove file that will be a list of all contents of the destination directory, reverse sorted by time, meaning oldest to newest.
Then I created a sub-directory of the now cluttered destination directory to move the stuff into.
mkdir /path/to/cluttered/destination/directory/newsubdirectory
Then I repeated the directory listing, except listing to screen and showing more details.
ls -lrht /path/to/cluttered/destination/directory/
This line lists the directory, sorted by date ascending (reverse sort by time) and shows more information, including the date/time stamp of each file in the now cluttered destination directory. I refer to this, starting at the top to show what directories and files I want to keep where they were. There will be a gap in the date/time stamp of the files where all the new files start that shouldn't be there.
Then I edited the filestomove file created in the first step above (that is sorted by date) and deleted the few from the list that were there previously that I want to stay from the original directory.
vim /opt/dircheck/filestomove Delete from the top, all files you don't want to move.
Then I used the command listed previously in this post to move the files in my list to the new directory I made.
sudo xargs -a /opt/dircheck/filestomove mv -t /path/to/cluttered/destination/directory/newsubdirectory
This moved all the files in a split second. (Note: you may not need the sudo at the beginning, this is a matter of file permissions).
Now my original destination directory is nice and clean and contains a new directory with all the files and directories that were cluttering it.
add a comment |
I accidentally copied the full contents of a directory into a destination directory instead of moving the full directory. This resulted in a cluttered destination directory instead of the origin directory being added to the directory.
To fix this, I did the following:
ls -rt /path/to/cluttered/destination/directory/ > /opt/dircheck/filestomove
The above command creates the filestomove file that will be a list of all contents of the destination directory, reverse sorted by time, meaning oldest to newest.
Then I created a sub-directory of the now cluttered destination directory to move the stuff into.
mkdir /path/to/cluttered/destination/directory/newsubdirectory
Then I repeated the directory listing, except listing to screen and showing more details.
ls -lrht /path/to/cluttered/destination/directory/
This line lists the directory, sorted by date ascending (reverse sort by time) and shows more information, including the date/time stamp of each file in the now cluttered destination directory. I refer to this, starting at the top to show what directories and files I want to keep where they were. There will be a gap in the date/time stamp of the files where all the new files start that shouldn't be there.
Then I edited the filestomove file created in the first step above (that is sorted by date) and deleted the few from the list that were there previously that I want to stay from the original directory.
vim /opt/dircheck/filestomove Delete from the top, all files you don't want to move.
Then I used the command listed previously in this post to move the files in my list to the new directory I made.
sudo xargs -a /opt/dircheck/filestomove mv -t /path/to/cluttered/destination/directory/newsubdirectory
This moved all the files in a split second. (Note: you may not need the sudo at the beginning, this is a matter of file permissions).
Now my original destination directory is nice and clean and contains a new directory with all the files and directories that were cluttering it.
I accidentally copied the full contents of a directory into a destination directory instead of moving the full directory. This resulted in a cluttered destination directory instead of the origin directory being added to the directory.
To fix this, I did the following:
ls -rt /path/to/cluttered/destination/directory/ > /opt/dircheck/filestomove
The above command creates the filestomove file that will be a list of all contents of the destination directory, reverse sorted by time, meaning oldest to newest.
Then I created a sub-directory of the now cluttered destination directory to move the stuff into.
mkdir /path/to/cluttered/destination/directory/newsubdirectory
Then I repeated the directory listing, except listing to screen and showing more details.
ls -lrht /path/to/cluttered/destination/directory/
This line lists the directory, sorted by date ascending (reverse sort by time) and shows more information, including the date/time stamp of each file in the now cluttered destination directory. I refer to this, starting at the top to show what directories and files I want to keep where they were. There will be a gap in the date/time stamp of the files where all the new files start that shouldn't be there.
Then I edited the filestomove file created in the first step above (that is sorted by date) and deleted the few from the list that were there previously that I want to stay from the original directory.
vim /opt/dircheck/filestomove Delete from the top, all files you don't want to move.
Then I used the command listed previously in this post to move the files in my list to the new directory I made.
sudo xargs -a /opt/dircheck/filestomove mv -t /path/to/cluttered/destination/directory/newsubdirectory
This moved all the files in a split second. (Note: you may not need the sudo at the beginning, this is a matter of file permissions).
Now my original destination directory is nice and clean and contains a new directory with all the files and directories that were cluttering it.
answered Dec 7 '11 at 4:26
JohnJohn
111
111
add a comment |
add a comment |
If (and only if), you don't have any nasty characters in the filenames (spaces, newlines, which would confuse xargs on how to break things into individual arguments), and you generate a list of files separated by newlines (one file per line), you could do something like.
cat filenames.txt | xargs mv -t /path/to/move/files/to
(In general, see man xargs, it's awesome)
If your particular mv doesn't have the -t option, you could also do some trickery like
( cat filenames.txt; echo; echo /path/to/move/files/to ) | xargs mv
Note - neither of these will work as expected if there happens to be filenames with newlines in them.
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
cp,mvand the like do not have-toption on bsd's, and the second example with subshell did not work for me (/path/to/moveis appended as the last filename)
– w17t
Jan 9 at 17:04
1
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv
– Kjetil Joergensen
Jan 10 at 23:38
add a comment |
If (and only if), you don't have any nasty characters in the filenames (spaces, newlines, which would confuse xargs on how to break things into individual arguments), and you generate a list of files separated by newlines (one file per line), you could do something like.
cat filenames.txt | xargs mv -t /path/to/move/files/to
(In general, see man xargs, it's awesome)
If your particular mv doesn't have the -t option, you could also do some trickery like
( cat filenames.txt; echo; echo /path/to/move/files/to ) | xargs mv
Note - neither of these will work as expected if there happens to be filenames with newlines in them.
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
cp,mvand the like do not have-toption on bsd's, and the second example with subshell did not work for me (/path/to/moveis appended as the last filename)
– w17t
Jan 9 at 17:04
1
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv
– Kjetil Joergensen
Jan 10 at 23:38
add a comment |
If (and only if), you don't have any nasty characters in the filenames (spaces, newlines, which would confuse xargs on how to break things into individual arguments), and you generate a list of files separated by newlines (one file per line), you could do something like.
cat filenames.txt | xargs mv -t /path/to/move/files/to
(In general, see man xargs, it's awesome)
If your particular mv doesn't have the -t option, you could also do some trickery like
( cat filenames.txt; echo; echo /path/to/move/files/to ) | xargs mv
Note - neither of these will work as expected if there happens to be filenames with newlines in them.
If (and only if), you don't have any nasty characters in the filenames (spaces, newlines, which would confuse xargs on how to break things into individual arguments), and you generate a list of files separated by newlines (one file per line), you could do something like.
cat filenames.txt | xargs mv -t /path/to/move/files/to
(In general, see man xargs, it's awesome)
If your particular mv doesn't have the -t option, you could also do some trickery like
( cat filenames.txt; echo; echo /path/to/move/files/to ) | xargs mv
Note - neither of these will work as expected if there happens to be filenames with newlines in them.
edited Jan 10 at 23:41
answered Dec 14 '10 at 14:16
Kjetil JoergensenKjetil Joergensen
4,96412117
4,96412117
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
cp,mvand the like do not have-toption on bsd's, and the second example with subshell did not work for me (/path/to/moveis appended as the last filename)
– w17t
Jan 9 at 17:04
1
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv
– Kjetil Joergensen
Jan 10 at 23:38
add a comment |
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
cp,mvand the like do not have-toption on bsd's, and the second example with subshell did not work for me (/path/to/moveis appended as the last filename)
– w17t
Jan 9 at 17:04
1
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv
– Kjetil Joergensen
Jan 10 at 23:38
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
However; see Ignacio's answer, rsync is sort-of made for this stuff.
– Kjetil Joergensen
Dec 14 '10 at 14:21
cp, mv and the like do not have -t option on bsd's, and the second example with subshell did not work for me (/path/to/move is appended as the last filename)– w17t
Jan 9 at 17:04
cp, mv and the like do not have -t option on bsd's, and the second example with subshell did not work for me (/path/to/move is appended as the last filename)– w17t
Jan 9 at 17:04
1
1
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like
(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv– Kjetil Joergensen
Jan 10 at 23:38
@w17t - sounds like filenames.txt does not end with a newline, either add newline to the end of filenames.txt, or do something like
(cat filenames.txt; echo; echo /path/to/move/files/to) | xargs mv– Kjetil Joergensen
Jan 10 at 23:38
add a comment |
Try something like:
cat list.txt | while read line; do mv "$line" /images; done
could have beenwhile read line; do mv $line /images; done < list.txtas a single command
– ignivs
Sep 17 '15 at 19:12
add a comment |
Try something like:
cat list.txt | while read line; do mv "$line" /images; done
could have beenwhile read line; do mv $line /images; done < list.txtas a single command
– ignivs
Sep 17 '15 at 19:12
add a comment |
Try something like:
cat list.txt | while read line; do mv "$line" /images; done
Try something like:
cat list.txt | while read line; do mv "$line" /images; done
edited Jul 20 '12 at 3:22
Scott Pack
13.4k84481
13.4k84481
answered Dec 14 '10 at 14:16
AliGibbsAliGibbs
2,1101633
2,1101633
could have beenwhile read line; do mv $line /images; done < list.txtas a single command
– ignivs
Sep 17 '15 at 19:12
add a comment |
could have beenwhile read line; do mv $line /images; done < list.txtas a single command
– ignivs
Sep 17 '15 at 19:12
could have been
while read line; do mv $line /images; done < list.txt as a single command– ignivs
Sep 17 '15 at 19:12
could have been
while read line; do mv $line /images; done < list.txt as a single command– ignivs
Sep 17 '15 at 19:12
add a comment |
The following worked for me where I needed to copy all the PNG files from a specific path (and all subdirs) to a new location while preserving the directory structure:
rsync -av --prune-empty-dirs --include='*/' --include='*.png' --exclude='*' source/ destination/
Because RSYNC creates the mirror of the directory structure first, then syncs the files, you can end up with excess folders that are empty. I used the --prune-empty-dirs flag to remove these empty directories.
I have no affiliation, but thought it right to give credit to the source that inspired this solution: http://techblog.zabuchy.net/2011/transfer-only-selected-file-types-with-rsync/
add a comment |
The following worked for me where I needed to copy all the PNG files from a specific path (and all subdirs) to a new location while preserving the directory structure:
rsync -av --prune-empty-dirs --include='*/' --include='*.png' --exclude='*' source/ destination/
Because RSYNC creates the mirror of the directory structure first, then syncs the files, you can end up with excess folders that are empty. I used the --prune-empty-dirs flag to remove these empty directories.
I have no affiliation, but thought it right to give credit to the source that inspired this solution: http://techblog.zabuchy.net/2011/transfer-only-selected-file-types-with-rsync/
add a comment |
The following worked for me where I needed to copy all the PNG files from a specific path (and all subdirs) to a new location while preserving the directory structure:
rsync -av --prune-empty-dirs --include='*/' --include='*.png' --exclude='*' source/ destination/
Because RSYNC creates the mirror of the directory structure first, then syncs the files, you can end up with excess folders that are empty. I used the --prune-empty-dirs flag to remove these empty directories.
I have no affiliation, but thought it right to give credit to the source that inspired this solution: http://techblog.zabuchy.net/2011/transfer-only-selected-file-types-with-rsync/
The following worked for me where I needed to copy all the PNG files from a specific path (and all subdirs) to a new location while preserving the directory structure:
rsync -av --prune-empty-dirs --include='*/' --include='*.png' --exclude='*' source/ destination/
Because RSYNC creates the mirror of the directory structure first, then syncs the files, you can end up with excess folders that are empty. I used the --prune-empty-dirs flag to remove these empty directories.
I have no affiliation, but thought it right to give credit to the source that inspired this solution: http://techblog.zabuchy.net/2011/transfer-only-selected-file-types-with-rsync/
edited Jul 20 '13 at 1:14
slm
5,021124359
5,021124359
answered Jul 26 '12 at 21:47
John Mark MitchellJohn Mark Mitchell
1964
1964
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- 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%2fserverfault.com%2fquestions%2f212439%2flinux-mv-or-cp-specific-files-from-a-text-list-of-files%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
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files.
– Jestep
Dec 14 '10 at 14:09