简体   繁体   English

没有扩展名的bash文件添加扩展名

[英]bash file without extension adding extension

I am trying to write a very simple bash script in order to add a file extension (.java) to a bunch of files that have no extension. 我试图编写一个非常简单的bash脚本,以便将文件扩展名(.java)添加到一堆没有扩展名的文件中。

If they had an extensions (say .txt) I'd do this: 如果他们有扩展名(例如.txt),我可以这样做:

#!/bin/bash
for file in `*.txt`; 
do mv $file $file.java; 
done

My files, however, don't have an extension. 但是,我的文件没有扩展名。 How do I make the loop? 如何制作循环? I tried *. 我尝试过*. with no luck. 没有运气。

Thank you. 谢谢。

#!/bin/bash
for file in *; do
  mv "$file" "$file".java; 
done

If you only want to move files that do not already have an extension, try: 如果只想移动没有扩展名的文件,请尝试:

#!/bin/sh
for file in *; do
  test "${file%.*}" = "$file" && mv "$file" "$file".java;
done

Note that all of the double-quotes above are superfluous if your filenames are reasonable. 请注意,如果文件名合理,则上面所有的双引号都是多余的。

ls -1 | nawk '/.txt/{old=$0;gsub(/.txt/,".txt.java",$0);system("mv \""old"\" "$0)}'

这里更新了答案

#!/bin/bash
for file in *.txt ; do mv $file `echo $file | sed 's/\(.*\.\)txt/\1java/'` ; done

尝试:

find . -maxdepth 1 -type f | sed 's/^\.\///; /\./d' | while read x; do mv "$x" "$x.java"; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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