Linux (mv or cp) specific files from a text list of files?
Solution 1
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
Solution 2
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.
Solution 3
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!
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Doug Fir
Updated on September 17, 2022Comments
-
Doug Fir almost 2 years
I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy
But, when I try to do formulas based on the dates, something is clearly amiss.
For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.
For each record I tried a formula to parse it apart.
=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
That did not help.
I also selected the column and switched it from general to date format
I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run
=year(right(A1,4))
I get1903
instead of2013
. When I run=date(right(A1,4),mid(A1,3,2),left(A1,2))
the formula returns2/10/3192
-
Admin over 13 yearsAlso, 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.
-
-
Kjetil Joergensen over 13 yearsHowever; see Ignacio's answer, rsync is sort-of made for this stuff.
-
Jason R about 12 yearsOne 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. Thersync
approach in the accepted answer doesn't suffer from this limitation. -
Doug Fir over 10 yearsThanks for the info. Can you clarify your sentence: "Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column): " Does this mean that if I do this I do not need to enter the formula you provided?
-
Dmitry Pavliv over 10 years@DougFirr, it's much easier to use my macro. Or you can do following: let your date be in
A2:A100
. Let your columnZ
is empty. Select columnZ
and apply dates format for it. Than write inZ2
formula=A2*1
and drag it down untilZ100
. Than select columnA
and apply dates format as well. Copy values fromZ2:Z100
and make paste special->paste values inA2:A100
. Thats all) -
Dmitry Pavliv over 10 yearsif you have additional questions, please ask:)
-
Dmitry Pavliv over 10 yearsand yes, there is no need to use this awful formula:
=DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy"),4,2),LEFT(TEXT(A2,"dd/mm/yyyy"),2))
-
Doug Fir over 10 yearsThanks again @simoco. So I tried what you said in your last comment. In column Z I format as date, then write =G2*1 which produces #value. In G2 there is 26/10/13 and it's format is general (I had not yet reached the step where I change it to date format). Am I missing something?
-
Dmitry Pavliv over 10 yearsDoes your
G2
has some spaces or"
? it shouldn't return #value -
Dmitry Pavliv over 10 yearslets try
=TRIM(G2)*1
. Is it return#VALUE
too? -
Doug Fir over 10 yearsSame reult I'm afraid :-(
-
Dmitry Pavliv over 10 yearsok, try to use
=DATE(RIGHT(TEXT(G2,"dd/mm/yyyy"),2),MID(TEXT(G2,"dd/mm/yyyy"),4,2),LEFT(TEXT(G2,"dd/mm/yyyy"),2))
-
Doug Fir over 10 yearsThank you very much it works now! If I interpret it right, we have converted each component of the =date formula to text, then custom formatted each one? How would you word it? Thanks again
-
Dmitry Pavliv over 10 yearsyes, you're right! we can't apply
RIGHT(G2,2)
directly, because dates in excel aredouble
values and we have to convert it to the text. -
PseudoNoise about 9 yearsThis worked for me because I actually wanted the files from different source paths in the same target directory.
-
Spamwich almost 9 yearsThis 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.
-
ignivs almost 9 yearscould have been
while read line; do mv $line /images; done < list.txt
as a single command -
user1182474 over 8 yearsNice, just I had problems with spaces, so I modified it to:
while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
-
MadHatter over 8 years@user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
-
Charlie Gorichanaz almost 8 years@user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
-
bzero about 7 yearsWhy is
/path/to/source
needed when it takes the paths from the file list? -
anneb over 5 yearsexample:
rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
-
Admin over 5 years
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) -
DreadfulWeather over 5 years@bzero maybe list of files can contain relative paths?
-
kristianp almost 5 yearsThis is good for a Dockerfile, as tar is available in the ubuntu base image, but not rsync.
-
yahol almost 4 yearsIf files list contains absolute paths, use
/
as source directory. -
Ruslan over 3 yearsFor GNU Tar you can replace the subshell with
tar xvf - -C /path/to/new/dir
. -
thouliha about 3 yearsYou also must use --no-relative if you don't want to keep the path, and have it be flat.
-
pob about 3 years-a normally implies -r, but not when using the "--files-from" option. For recursive behavior, you need to explicitly add the -r option also, as in "rsync -ar /source/director --files-from=/full/path/to/listfile /destination/directory"