[英]bash script to split and reverse filenames
A bash script request(or fish script). bash脚本请求(或fish脚本)。 I have a bunch of files like: 我有一堆文件,例如:
name - lastname.info
names - lastnames.info
name & name - lastname & lastname.info
but I want to change all of them to: 但我想将它们全部更改为:
lastname - name.info
lastnames - names.info
lastname & lastname - name & name - lastname & lastname.info
going by your sample data, you have " - "
as the delimiter, so 根据样本数据,您使用" - "
作为分隔符,因此
for file in *; do
ext=${file##*.}
no_ext=${file%.*}
name=${no_ext% - *}
lastname=${no_ext##* - }
mv "$file" "$lastname - $name.$ext"
done
For the gory details, see http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion 有关详细信息,请参见http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
You could use a for loop to loop through all the files in the cwd, then use the cut utility to parse the file name based on the '-' delimiters to get first and last name (I then re-cut to parse out the file name extension), then use tr to remove the leading/trailing spaces. 您可以使用for循环遍历cwd中的所有文件,然后使用cut实用程序基于'-'分隔符来解析文件名以获取名字和姓氏(然后我重新剪切以解析出文件)名称扩展名),然后使用tr删除前导/后缀空格。 Finally concatenate, and you've got your new file name: 最后连接,您便有了新的文件名:
for file in *
do
fname=`echo $file | cut -d '-' -f 1 | tr -d ' '`
lname=`echo $file | cut -d '-' -f 2 | cut -d '.' -f 1 | tr -d ' '`
newname=$lname' - '$fname.info
echo $newname
done
Assuming all your files are in one directory: 假设所有文件都在一个目录中:
for f in lastname*
do
mv $f `echo $f | sed 's/lastname/name/'`
done
If your files are anywhere in the hierarchy, you will have to use the find
command but the basic idea remains. 如果文件在层次结构中的任何位置,则必须使用find
命令,但基本思想仍然存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.