Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

linux - Exracting 200 first lines from a zip file and creating this file into a different folder

I have a folder called /home/myusername/originalFiles where I got tons of large *.gz files. Inside this folder and its subfolders we got also tons of large *.gz files, too. Without deleting or modifying any of the *.gz files I need:

a) For each file f in /home/myusername/originalFiles (and subfolders), expand it,

b) Extract the first 200 lines from the expanded f

c) Convert that "200 lines" file from b) into a gz file again

d) Copy the "gzipped 200 lines" file from c) into another folder called /home/myusername/newSampleFiles but respecting the folder structure and name at /home/myusername/originalFiles. So, if the original file f was in a subfolder like /home/myusername/originalFiles/year2020 then the respective "gzipped 200 lines" file from c) must be in /home/myusername/newSampleFiles/year2020 and using the same name and extension of the original file in /home/myusername/originalFiles

e) Do not keep any expanded file obtained at a)

f) Do this only using Linux cmds

I tried

find . -type f -name "*.gz" -print | xargs -I@ sh -c 'head -n200 @ > /home/myusername/newSampleFiles/@'

but I got the error message:

/home/myusername/newSampleFiles/./someFile.txt.gz: No such file or directory

question from:https://stackoverflow.com/questions/65889329/exracting-200-first-lines-from-a-zip-file-and-creating-this-file-into-a-differen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
while read file;
do
    file2="${file%.*}"
    gzip -cd "$file" | head -n200 > "/home/myusername/newSampleFiles$file2";
    gzip -c "/home/myusername/newSampleFiles$file2" > "/home/myusername/newSampleFiles$file"
 done <<< "$(find /path/to/dir -type f -name "*.gz")"

Redirect the find command into a while loop, reading each line of the output into the variable file and then using parameter expansion to strip any the file extension from file1 and reading the result into file2. The variables are then used in the gzip commands.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...