0

This question already has an answer here:

This is my first time asking a question :) I am new to bash scripting but pretty good with python. I have a directory with a bunch of files, and I have a text file that contains a subset of file names within the directory. I am trying to write a script that will read that txt file, select the files that are listed in the txt file from the directory, and move them up a directory.

It may sound simple enough but there are hundreds of them so I am trying to automate! Any help or guidance would be appreciated.

marked as duplicate by terdon bash Jun 7 at 15:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What did you try? – Romeo Ninov Jun 7 at 15:20
  • @RomeoNinov not much of anything yet... bash syntax is so confusing to me! – Lauren Jun 7 at 15:25
  • @steeldriver Thankyou! looking at it now! – Lauren Jun 7 at 15:25
2

Assuming that the list contains full filenames and the files are in the same folder as the list, here is a command:

while IFS= read -r file; do mv "$file" ../"$file"; done < listoffiles.txt
  • while ...; do ...; done cycle operators
  • read file declare variable file
  • mv move
  • $file ../$file source - destination. The .. stands for 'one level higher directory' (e.g. /home/user/documents is your current directory, /home/user will be your ..
  • listoffiles.txt your file that contains the list.
  • That worked! Thank you so much, this really helps me to understand what is happening :) – Lauren Jun 7 at 15:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.