0

If you have a text file with lines

abc
123
def
hij

And you have a directory of files called

abc.txt
tgr.txt
lot.txt
hij.txt

And you want to get the files in the directory that match a line in the text file and move those files in that directory to a sub-directory, what is the best way to do this in bash? The sub-directory, in this example, would have the files:

abc.txt
hij.txt

What I'm thinking is to do something that says: if that string* exists (with a wildcard) in the directory as a filename, move that file to a subdirectory.

I understand that I could potentially use a combination of the find or the grep commands, but I'm honestly stuck as to where to proceed from there. Thanks.

geeb.24
  • 527
  • 2
  • 7
  • 25

1 Answers1

0
cat text.txt | while read line; 
do 
    find . -maxdepth 1 -name "$line*" -print0 | xargs -0 -I {} mv {} ./target
done 

Pipe your text file into a loop and search for each line in the current directory. If you find something pipe into xargs (if you find multiple files) and move each file into your target directory.