Home > Net >  How to copy specific files from directory into new directory zsh
How to copy specific files from directory into new directory zsh

Time:07-08

I have a list of file names contained within a text file (a.txt). I want to extract from a directory (b) the files listed in a.txt to a new directory (c). The syntax of the filenames in a.txt and b match. The files in a.txt are empty and the files in b contain the json message of interest.

For example, the contents of a.txt look like:

ML3DBHCN___005.json
OCO2_L2_Standard___10r.json
GPM_3IMERGM___06.json 

and b:

b/ML3DBHCN___005.json
b/OCO2_L2_Standard___10r.json
b/GPM_3IMERGM___06.json 

Do i need to write a small .sh file that iterates through a.txt and extracts from b or can this be completed at once via command line?

CodePudding user response:

If you know the filenames don't contain whitespace or wildcard characters, you can do it as a simple one-liner:

cp $(<a.txt) b/

If they can contain special characters, you can read them into an array:

readarray files <a.txt
cp "${files[@]}" b/

CodePudding user response:

If you want to move from b to c the files named in a.txt (and they don't have spaces or wildcards):

(cd /path/to/b && mv $(< /path/to/a.txt) /path/to/c/)
  • Related