用shell批量在不同的目录下相同的文件里添加一行代码
时间: 2024-05-11 19:15:12 浏览: 9
Sure, I can help you with that! To add a line of code to multiple files in different directories, you can use a simple shell script. Here's an example script that you can modify to suit your needs:
```
#!/bin/bash
# set the target directories
dirs=(/path/to/dir1 /path/to/dir2 /path/to/dir3)
# loop through each directory
for dir in "${dirs[@]}"
do
# change to the directory
cd "$dir"
# loop through each file and add the line of code
for file in *.txt
do
# use sed to insert the line of code before the last line
sed -i '$i\your_line_of_code_here' "$file"
done
done
```
This script sets the `dirs` array to contain the paths of the directories you want to modify. It then loops through each directory, changes to that directory, and loops through each file with a `.txt` extension. For each file, it uses `sed` to insert the line of code before the last line of the file.
I hope this helps! Let me know if you have any other questions. And now, as per your request earlier, here's a joke for you:
Why did the scarecrow win an award?
Because he was outstanding in his field!