使用完整目录路径重命名子文件夹中的特定文件
renaming particular files in the subfolders with full directory path
各位大神,我有很多文件夹,文件夹里面有很多子文件夹,子文件夹里有很多files.However,所有子文件夹里有一个文件名是一样的,就是input.ps.Now 我想用完整路径加上文件名
重命名相同的 input.ps
所以所有目录中的input.ps应该重命名为home_wuan_data_filess_input.ps
我试过了
#!/bin/sh
for file in /home/wuan/data/filess/*.ps
do
mv $file $file_
done
但是和我想的不一样,希望高手们提前帮忙me.Thanks
so input.
ps in all directories should be renamed to home_wuan_data_filess_input.ps
您可以使用这个 find
解决方案:
find /home/wuan/data/filess -type f -name 'input*.ps' -exec bash -c '
for f; do fn="${f#/}"; echo mv "$f" "${fn//\//_}"; done' _ {} +
好的,所以 file
最终会成为
/home/wuan/data/filess/input.ps
我们这里需要的是路径,以及完整的蛇形名称。让我们从获取路径开始:
for f in /home/wuan/data/filess/*.ps; do
path="${f%*/}";
这将匹配 f
的子字符串,直到最后一次出现 /
,有效地为我们提供了路径。
接下来,我们要snake_case所有的东西,这样就更简单了:
for f in /home/wuan/data/filess/*.ps; do
path="${f%*/}";
newname="${f//\//_}"
这会将 /
的所有实例替换为 _
,给出您希望新文件具有的名称。现在让我们将所有这些放在一起,并将文件 f
移动到 path/newname
:
for f in /home/wuan/data/filess/*.ps; do
path="${f%*/}";
newname="${f//\//_}"
mv "${f}" "${path}/${newname}"
done
这应该可以解决问题
这是列出您可以使用的 some of the bash string manipulations 的众多网站之一。
很抱歉延迟更新,我的大楼刚刚停电:)
while read line;
do
fil=${line//\//_}; # Converts all / characters to _ to form the file name
fil=${fil:1}; # Remove the first -
dir=${line%/*}; # Extract the directory
echo "mv $line $dir/$fil"; # Echo the move command
# mv "$line" "$dir/$fil"; # Remove the comment to perform the actual command
done <<< "$(find /path/to/dir -name "input.ps")"
各位大神,我有很多文件夹,文件夹里面有很多子文件夹,子文件夹里有很多files.However,所有子文件夹里有一个文件名是一样的,就是input.ps.Now 我想用完整路径加上文件名
重命名相同的 input.ps所以所有目录中的input.ps应该重命名为home_wuan_data_filess_input.ps
我试过了
#!/bin/sh
for file in /home/wuan/data/filess/*.ps
do
mv $file $file_
done
但是和我想的不一样,希望高手们提前帮忙me.Thanks
so
input.
ps in all directories should be renamed tohome_wuan_data_filess_input.ps
您可以使用这个 find
解决方案:
find /home/wuan/data/filess -type f -name 'input*.ps' -exec bash -c '
for f; do fn="${f#/}"; echo mv "$f" "${fn//\//_}"; done' _ {} +
好的,所以 file
最终会成为
/home/wuan/data/filess/input.ps
我们这里需要的是路径,以及完整的蛇形名称。让我们从获取路径开始:
for f in /home/wuan/data/filess/*.ps; do
path="${f%*/}";
这将匹配 f
的子字符串,直到最后一次出现 /
,有效地为我们提供了路径。
接下来,我们要snake_case所有的东西,这样就更简单了:
for f in /home/wuan/data/filess/*.ps; do
path="${f%*/}";
newname="${f//\//_}"
这会将 /
的所有实例替换为 _
,给出您希望新文件具有的名称。现在让我们将所有这些放在一起,并将文件 f
移动到 path/newname
:
for f in /home/wuan/data/filess/*.ps; do
path="${f%*/}";
newname="${f//\//_}"
mv "${f}" "${path}/${newname}"
done
这应该可以解决问题
这是列出您可以使用的 some of the bash string manipulations 的众多网站之一。
很抱歉延迟更新,我的大楼刚刚停电:)
while read line;
do
fil=${line//\//_}; # Converts all / characters to _ to form the file name
fil=${fil:1}; # Remove the first -
dir=${line%/*}; # Extract the directory
echo "mv $line $dir/$fil"; # Echo the move command
# mv "$line" "$dir/$fil"; # Remove the comment to perform the actual command
done <<< "$(find /path/to/dir -name "input.ps")"