I have the following file structure:
我有以下文件结构:
Applications/Snowflake/applications/Salford_100/wrongname_120.nui; wrongname_200_d.nui
Applications/Snowflake/applications/Salford_900/wrongname_120.nui; wrongname_200_d.nui
Applications/Snowflake/applications/Salford_122/wrongname_120.nui; wrongname_200_d.nui
And I want to rename the fles to the same name as the directories they're in, but the files with "_d" at the end should retain its last 2 characters. The file pattern would always be "salford_xxx" where xxx is always 3 digits. So the resulting files would be:
我想将fles重命名为与它们所在目录相同的名称,但最后带有“_d”的文件应保留其最后2个字符。文件模式始终为“salford_xxx”,其中xxx始终为3位数。所以生成的文件将是:
Applications/Snowflake/applications/Salford_100/Salford_100.nui; Salford_100_d.nui
Applications/Snowflake/applications/Salford_900/Salford_900.nui; Salford_900_d.nui
Applications/Snowflake/applications/Salford_122/Salford_122.nui; Salford_122_d.nui
The script would run from a different location in
该脚本将从另一个位置运行
Applications/Snowflake/Table-updater
I imagine this would require a for loop and a sed regex, but Im open to any suggestions. (Thanks @ghoti for your advice) I've Tried this, which currently does not account for files with "_d" yet and I just get one file renamed correctly. Some help would be appreciated.
我想这需要一个for循环和一个sed正则表达式,但我可以接受任何建议。 (感谢@ghoti的建议)我试过这个,目前还不考虑带有“_d”的文件,我只是正确地重命名了一个文件。一些帮助将不胜感激。
cd /Applications/snowflake/table-updater/Testing/applications/salford_*
dcomp="$(basename "$(pwd)")"
for file in *; do
ext="${file##*.}"
mv -v "$file" "$dcomp.$ext"
done
Ive now updated the script following @varun advice (thank you) and it now also searches through all files in the parent dir that contain salford in the name, missing out the parent name. Please see below
我现在更新@varun建议之后的脚本(谢谢),它现在还搜索父目录中包含名称中的salford的所有文件,遗漏了父名称。请看下面
#!/bin/sh
#
# RenameToDirName2.sh
#
set -e
cd /Applications/snowflake/table-updater/Testing/Applications/
find salford* -maxdepth 1 -type d \( ! -name . \) -exec sh -c '(cd {} &&
(
dcomp="$(basename "$(pwd)")"
for file in *;
do ext="${file#*.}"
zz=$(echo $file|grep _d)
if [ -z $zz ]
then
mv -v "$file" "$dcomp.$ext"
else
mv -v "$file" "${dcomp}_d.$ext"
fi
done
)
)' ';'
The thing is, I've just realised that in these salford sub directories there are other files with different extensions that I don't want renaming. Ive tried putting in an else if statement to stipulate *.Nui files only, calling my $dcomp variable, like this
问题是,我刚刚意识到在这些salford子目录中有其他文件具有不同的扩展名,我不想重命名。我试过把else if语句只用来规定* .Nui文件,调用我的$ dcomp变量,就像这样
else
if file in $dcomp/*.nui
then
#continue...
But I get errors. Where should this go in my script and also do I have the correct syntax for this loop? Can you help?
但我得到错误。这应该放在我的脚本中,还有这个循环的正确语法吗?你能帮我吗?
2 个解决方案
#1
1
@eggfoot,I have modified my script, which will look into all the directories in folder applications and look for for folders which have Salford in it. So you can call my script like this
@ eggfoot,我修改了我的脚本,它会查看文件夹应用程序中的所有目录,并查找包含Salford的文件夹。所以你可以这样调用我的脚本
./rename.sh /home/username/Applications/Snowflake e/applications
./rename.sh / home / username / Applications / Snowflake e / applications
#!/bin/bash
# set -x
path=$1
dir_list=$(find $path/ -type d)
for index_dir in $dir_list
do
aa=$(echo $index_dir|grep Salford)
if [ ! -z $aa ]
then
files_list=$(find $index_dir/ -type f)
for index in $files_list
do
xx=$(basename $index)
z=$(echo $xx|grep '_d')
if [ -z $z ]
then
result=$(echo $index | sed 's/\/\(.*\)\/\(.*\)\/\(.*\)\(\..*$\)/\/\1\/\2\/\2\4/')
mv "$index" "$result"
else
result=$(echo $index | sed 's/\/\(.*\)\/\(.*\)\/\(.*\)_d\(\..*$\)/\/\1\/\2\/\2_d\4/')
mv "$index" "$result"
fi
done
fi
done
Regarding sed, it uses the s command of sed and substitute the file name with directory name, keeping the extension as it is.
关于sed,它使用sed的s命令并用目录名替换文件名,保持扩展名不变。
Regarding your script, you need to use grep command to find files which have _d and than you can use parameter substitution changing the mv for files with _d and one without _d.
关于您的脚本,您需要使用grep命令查找具有_d的文件,并且您可以使用参数替换来更改带有_d的文件的mv和不带_d的文件的mv。
dcomp="$(basename "$(pwd)")"
for file in *; do
ext="${file##*.}"
zz=$(echo $file|grep _d)
if [ -z $zz ]
then
mv -v "$file" "$dcomp.$ext"
else
mv -v "$file" "${dcomp}_d.$ext"
fi
done
#2
1
You can write:
你可以写:
(
cd ../applications/ && \
for name in Salford_[0-9][0-9][0-9] ; do
mv "$name"/*_[0-9][0-9][0-9].nui "$name/$name.nui"
mv "$name"/*_[0-9][0-9][0-9]_d.nui "$name/${name}_d.nui"
done
)
(Note: the (...)
is a subshell, to restrict the scope of the directory-change and of the name
variable.)
(注意:(...)是一个子shell,用于限制目录更改和名称变量的范围。)
#1
1
@eggfoot,I have modified my script, which will look into all the directories in folder applications and look for for folders which have Salford in it. So you can call my script like this
@ eggfoot,我修改了我的脚本,它会查看文件夹应用程序中的所有目录,并查找包含Salford的文件夹。所以你可以这样调用我的脚本
./rename.sh /home/username/Applications/Snowflake e/applications
./rename.sh / home / username / Applications / Snowflake e / applications
#!/bin/bash
# set -x
path=$1
dir_list=$(find $path/ -type d)
for index_dir in $dir_list
do
aa=$(echo $index_dir|grep Salford)
if [ ! -z $aa ]
then
files_list=$(find $index_dir/ -type f)
for index in $files_list
do
xx=$(basename $index)
z=$(echo $xx|grep '_d')
if [ -z $z ]
then
result=$(echo $index | sed 's/\/\(.*\)\/\(.*\)\/\(.*\)\(\..*$\)/\/\1\/\2\/\2\4/')
mv "$index" "$result"
else
result=$(echo $index | sed 's/\/\(.*\)\/\(.*\)\/\(.*\)_d\(\..*$\)/\/\1\/\2\/\2_d\4/')
mv "$index" "$result"
fi
done
fi
done
Regarding sed, it uses the s command of sed and substitute the file name with directory name, keeping the extension as it is.
关于sed,它使用sed的s命令并用目录名替换文件名,保持扩展名不变。
Regarding your script, you need to use grep command to find files which have _d and than you can use parameter substitution changing the mv for files with _d and one without _d.
关于您的脚本,您需要使用grep命令查找具有_d的文件,并且您可以使用参数替换来更改带有_d的文件的mv和不带_d的文件的mv。
dcomp="$(basename "$(pwd)")"
for file in *; do
ext="${file##*.}"
zz=$(echo $file|grep _d)
if [ -z $zz ]
then
mv -v "$file" "$dcomp.$ext"
else
mv -v "$file" "${dcomp}_d.$ext"
fi
done
#2
1
You can write:
你可以写:
(
cd ../applications/ && \
for name in Salford_[0-9][0-9][0-9] ; do
mv "$name"/*_[0-9][0-9][0-9].nui "$name/$name.nui"
mv "$name"/*_[0-9][0-9][0-9]_d.nui "$name/${name}_d.nui"
done
)
(Note: the (...)
is a subshell, to restrict the scope of the directory-change and of the name
variable.)
(注意:(...)是一个子shell,用于限制目录更改和名称变量的范围。)