简体   繁体   中英

How do I copy the beginning of multiple files in Linux?

I want to copy a bunch of files (*.txt) from one directory to another in Ubuntu. I want to reduce them in size, so I am using head to get the first 100 lines of each.

I want the new files to keep their original names but be in the subdirectory small/ . I have tried:

head -n 100 *.txt > small/*.txt

but this creates one file called *.txt . I have also tried:

head -n 100 *.txt > small/

but this gives Is a directory error.

It's got to be easy right, but I am pretty bad at Linux. Any help is much appreciated.

You'll have to create a loop instead:

for file in *.txt; do
    head -n 100 "$file" > small/"$file"
done

This loops through all the .txt files performing a head -n 100 in all of them and outputting into a new file in the small/ directory.

Try

for f in *.txt; do
  head -n 100 $f > small/$f
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM