简体   繁体   中英

writing a string into large number of files

I have created 10 files using touch command on linux terminal in a directory having only these created files.

 $ touch a1.c a2.c a3.c a4.c a5.c a6.c a7.c a8.c a9.c a10.c

I want to write a string "stackOverflow"(say) into all these files at a time . How to do that?Is there any command or some other way ?

Platform -ubuntu 10.04

从命令提示符(Bash)

for f in *.c; do echo "stackOverflow" >> $f; done

Once your data is in an array it is easy to loop over, see the example.

Just save this in an empty file writeStringToFiles.sh and right click it then under the permissions tab click Allow executing file as a program. You can double click it or run it from the terminal by "cd" to the directory its in then just type "./writeStringToFiles.sh". You can put any commands you want in a bash script like this.

#!/bin/sh
files=(a1.c a2.c a3.c a4.c a5.c a6.c a7.c a8.c a9.c a10.c)
thingToWrite='stackOverflow'

for i in "${files[@]}"
do :
    touch $i
    echo -e $thingToWrite >> $i
done

exit

If you files are not empty(at least 1 byte):

$ sed -i '1i\
stackOverflow' a{1..10}.c

will insert stackOverflow at the beginning of every file.

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