The Computer Oracle

Move a list of files(in a text file) to a directory?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence

--

Chapters
00:00 Move A List Of Files(In A Text File) To A Directory?
00:33 Accepted Answer Score 16
00:46 Answer 2 Score 10
01:14 Answer 3 Score 5
01:32 Answer 4 Score 2
01:35 Thank you

--

Full question
https://superuser.com/questions/538306/m...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #mv

#avk47



ACCEPTED ANSWER

Score 16


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



ANSWER 2

Score 10


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.




ANSWER 3

Score 5


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.




ANSWER 4

Score 2


Try this:

python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"