我有这个变量:
input_file = file.csv
我想将此文件的扩展名从file.csv更改为file.old
我在想mv $input_file basename $input_file . old
之类的东西,但我真的不知道怎么做
答案 0 :(得分:1)
尝试mv $input_file ${input_file%.*}.old
答案 1 :(得分:0)
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/bootstrap-select.js"></script>
这应该让你排序
这里提到如何提取文件名和扩展名: Extract filename and extension in Bash
答案 2 :(得分:0)
以下脚本应该按照您的要求运行。如果您使用basename
而没有后缀(.csv),则会返回带扩展名的文件名。另外,不要在变量声明和它的值之间使用空格。
#/bin/bash
input_file=file.csv
filename="$(basename $input_file .csv)"
mv "$input_file" "$filename.old"
以下是basename命令的用法供您参考。
Usage: basename NAME [SUFFIX]
or: basename OPTION... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.
Mandatory arguments to long options are mandatory for short options too.
-a, --multiple support multiple arguments and treat each as a NAME
-s, --suffix=SUFFIX remove a trailing SUFFIX; implies -a
-z, --zero end each output line with NUL, not newline
--help display this help and exit
--version output version information and exit
Examples:
basename /usr/bin/sort -> "sort"
basename include/stdio.h .h -> "stdio"
basename -s .h include/stdio.h -> "stdio"
basename -a any/str1 any/str2 -> "str1" followed by "str2"