Linux How can I add a string to the beginning of each file in a folder in bash?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5796615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add a string to the beginning of each file in a folder in bash?
提问by therin
I want to be able to prepend a string to the beginning of each text file in a folder. How can I do this using bash on Linux?
I want to be able to prepend a string to the beginning of each text file in a folder. How can I do this using bash on Linux?
采纳答案by drysdam
This will do that. You could make it more efficient if you are doing the same text to each file...
This will do that. You could make it more efficient if you are doing the same text to each file...
for f in *; do
echo "whatever" > tmpfile
cat $f >> tmpfile
mv tmpfile $f
done
回答by Cfreak
This should do the trick.
This should do the trick.
FOLDER='path/to/your/folder'
TEXT='Text to prepend'
cd $FOLDER
for i in `ls -1 $FOLDER`; do
CONTENTS=`cat $i`
echo $TEXT > $i # use echo -n if you want the append to be on the same line
echo $CONTENTS >> $i
done
I wouldn't recommending doing this if your files are very big though.
I wouldn't recommending doing this if your files are very big though.
回答by sashang
You can do this as well:
You can do this as well:
for f in *; do
cat <(echo "someline") $f > tempfile
mv tempfile $f
done
It's not much different from the 1st post but does show how to treat the output of the 'echo' statement as a file without having to create a temporay file to store the value.
It's not much different from the 1st post but does show how to treat the output of the 'echo' statement as a file without having to create a temporay file to store the value.
回答by anubhava
And you can do this using sed in 1 single commandas well
And you can do this using sed in 1 single commandas well
for f in *; do
sed -i.bak '1i\
foo-bar
' ${f}
done
回答by ghostdog74
You can do it like this without a loop and cat
You can do it like this without a loop and cat
sed -i '1i whatever' *
if you want to back up your files, use -i.bak
if you want to back up your files, use -i.bak
Or using awk
Or using awk
awk 'FNR==1{for file in *; do
(test ! -f "${file}" || test ! -w "${file}") && continue # sort out non-files and non-writable files
if test -s "${file}" && ! grep -Iqs '.*' "${file}"; then continue; fi # sort out binary files
printf '\n%s\n\n' "FILE: ${file}"
# cf. http://wiki.bash-hackers.org/howto/edit-ed
printf '%s\n' H 0a "foobar" . ',p' q | ed -s "${file}" # dry run (just prints to stdout)
#printf '%s\n' H 0a "foobar" . wq | ed -s "${file}" # in-place file edit without any backup
done | less
="whatever\n"for f in *;
do
mv "$f" "whatever_$f"
done
;}{print ##代码##>FILENAME}' *
回答by edgar
You may use the ed command to do without temporary files if you like:
You may use the ed command to do without temporary files if you like:
##代码##回答by Jay Stan
This is the easiest I have worked out.
This is the easiest I have worked out.
sed -i '1s/^/Text
to add then new file\n/' /file/to/change
sed -i '1s/^/Text
to add then new file\n/' /file/to/change
回答by user151841
A one-liner: rename '' string_ *
A one-liner: rename '' string_ *
回答by ikc
Here is an example :
Here is an example :
##代码##