linux添加信息命令

发布于 2024-10-10 23:39:30 字数 67 浏览 5 评论 0原文

有没有办法找到驱动器中的所有 .java 文件并添加文本“author name :man”...使用 linux 命令

Is there any way to find all .java files in a drive and add the text "author name :man"...with the linux command

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-10-17 23:39:30

最简单的是 ..

find ./ -name '*.java' -exec echo "author name :man" >> {} \;

DC

为了解释上面的作用,

首先 find 是命令。它的任务是查找符合某些条件的文件并选择性地执行操作,它是一个非常强大的工具。

./ 是要搜索的目录,在本例中 ./ 表示当前目录。如果您希望它搜索整个计算机,请使用 / 如果您希望它仅搜索特定文件夹或子文件夹,请使用 /folder/subfoldercd 到该目录并使用 ./ 如上面的示例命令所示

-name 告诉 find 您正在查找其后名称的文件,在示例中我们告诉它搜索以 .java 结尾的任何文件,这就是为什么通配符 * 我们在名称两边加上单引号,如 '*.java' 以防止“Shell 扩展”,请谷歌了解更多信息。

-exec 告诉 find 对于每个文件执行其后面的命令,在本例中该命令是 echo "author name :man" >>> {} 表示回显(写入)字符串“author name :man”并将其附加到找到的文件中。 find 将标记 {} 替换为文件的文件名,它相对于当前文件夹,例如运行命令时所在的文件夹。 \; 告诉 find 这是命令的结尾。如果您愿意,可以在其后面添加更多命令(您必须使用像 -and 这样的逻辑操作)。

因此,为了进行测试,请运行 ssh 到您的服务器并运行此命令,

 find / -name '*.java'

这将找到您服务器上带有 .java 扩展名的所有文件。可能会很多并且可能需要很长时间。

完成后,让我们尝试 -exec 一些最好不会造成任何损坏的东西,所以尝试这个命令

find / -name '*.java' -exec ls -alh {} \;

这告诉 find 为它找到的每个以 .java 结尾的文件运行 ls 命令。

您现在应该能够使用 find 执行您需要的操作

cd 到包含您要修改的 java 文件的文件夹的根目录并运行命令
当然不要忘记将纯文本附加到 java 文件会破坏它。确保文本采用评论的形式。

也许更好的命令是

find ./ -name '*.java' -exec cat authors.txt >> {} \;

Whereauthors.txt包含您要添加的数据。上面的命令会将authors.text的全部内容附加到它找到的每个.java文件中。

小心这个命令非常强大,并且“强大的力量来了”......某种东西或其他

换句话说,在运行进行大规模更改的命令之前,请确保先备份所有内容。否则你可能会后悔

DC

最后将详细信息添加到文件中有点复杂,因为 hhafez 已经说过你可以使用另一个脚本来为你做这件事。

创建脚本 addauthor.sh ...

 #!/bin/bash
cat authors.txt > /tmp/tempfile
cat $1 >> /tmp/tempfile
cp --reply=yes /tmp/tempfile $1

并将 find 命令修改为

find ./ -name '*.java' -exec sh addauthor.sh {} \;

首先测试

DC

The simplest is ..

find ./ -name '*.java' -exec echo "author name :man" >> {} \;

DC

To explain what the above does

first find is the command. Its task is to find files that match certain criteria and optionally do things, it is a very powerful tool.

./ is the directory to search in, in this case ./ means the current directory. If you want it to search the entire computer use / if you want it to search only a specific folder or sub folder use /folder/subfolder OR cd to that directory and use ./ as in the sample command above

-name tells find you are looking for a file with the name that follows it, in the example we are telling it to search for any file ending in .java thats why the wildcard * we put single quotes around the name as in '*.java' to prevent 'Shell expansion' google that for more information.

-exec tells find that for every file execute the command following it, in this case the command is echo "author name :man" >> {} which means echo(write) the string "author name :man" and append >> it to the found file. find replaces the token {} with the filename of the file, it is relative to the current folder e.g. the one you are in when you run the command. \; tells find this is the end of the command. further commands can follow it if you want (you must use a logical operation like -and).

so for a test run ssh into your server and run this

 find / -name '*.java'

This will find all files on your server with the .java extension. it could be a lot and might take a long time.

when that is done lets try to -exec something preferably something that wont cause any damage, so try this command

find / -name '*.java' -exec ls -alh {} \;

This tells find to run the ls command for each and every file ending in .java that it finds.

You should now be able to use find to do what you require

cd to the root of the folder that contains the java files you wish to modify and run the command
of course don't forget that appending plain text to a java file will break it. make sure the text is in the form of a comment.

perhaps a better command is

find ./ -name '*.java' -exec cat authors.txt >> {} \;

Where authors.txt contains the data you wish to add. the command above will append the entire content of authors.text to each and every .java file it finds.

Be careful this command is very powerful, and "with great power comes" ... something or other

In other words make sure you back everything up first BEFORE running a command that makes wholesale changes. you may regret it other wise

DC

Lastly to PREPEND the details to the files is a little more complicated as hhafez has stated you can use another script that does it for you.

create the script addauthor.sh ...

 #!/bin/bash
cat authors.txt > /tmp/tempfile
cat $1 >> /tmp/tempfile
cp --reply=yes /tmp/tempfile $1

and modify the find command to

find ./ -name '*.java' -exec sh addauthor.sh {} \;

Test it first

DC

红尘作伴 2024-10-17 23:39:30

您想以破坏性方式执行此操作还是制作副本(假设您不介意内联

find 执行此操作)。 -name *.java -exec add_text.sh {}\;

其中 add_text.sh 看起来像这样

#!/bin/sh
$input_file = $1
$header_file = /path/to/the/header/file/you/want/to/insert/
mv ${input_file} ${input_file}_tmp
cat $header_file ${input_file}_tmp > ${input_file}

警告这是未经测试的 :)

do you want to do it in a destructive way or make a copy, assuming you don't mind doing it inline

find . -name *.java -exec add_text.sh {}\;

where add_text.sh looks something like this

#!/bin/sh
$input_file = $1
$header_file = /path/to/the/header/file/you/want/to/insert/
mv ${input_file} ${input_file}_tmp
cat $header_file ${input_file}_tmp > ${input_file}

Warning this is untested :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文