在bash中重命名文件的陷阱 [英] pitfalls in renaming files in bash

查看:114
本文介绍了在bash中重命名文件的陷阱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里读一本指南
http://mywiki.wooledge.org/BashFAQ/030
该链路上的几个例子中给出我试图了解他们一个例子code说:

 #猛砸
#替换为下划线的所有空间
在*˚F\\ *;做MV - $ F$ {//˚F/ _}; DONE

我认识到现在是搜索和替换的情况下使用了特殊字符,如〜的空间或#等一个反斜杠
例子还是在这里上面的例子中shell脚本他们结果
使用 $ {//˚F/ _} 结果
正斜杠,我不是随本清这可以吗?结果
在同一页上的另一例子
他们举一个例子,除去空间和下划线替换

找到。 -depth -name*-exec bash的-c'DIR = $ {1%/ *}基地= $ {1 ## * /};结果
 MV$ 1$ DIR / $ {//基地/ _}'_ {} \\;

在上面的例子中,我不能与下面的结果清晰
1) DIR = $ {1%/ *} 结果
2)基地= $ {1 ## * /} 结果
3),当它说 MV$ 1什么是$ 1以上语句的意思,结果
4)最后find命令被关闭与 -exec<&东西GT; _ {} \\; 结果
现在有什么用下划线_,大括号{}和一个反斜杠后跟一个冒号以上\\;?结果
第三个例子,他们说

 #tolower的 - 转换文件的名称为小写
#POSIX
在$ @的文件做
   [-f$文件] ||继续#忽略不存在的名字
 NEWNAME = $(回声$文件| TR'[:上:]''[:降低:]')#小写
 [$文件=$ NEWNAME]&放大器;&安培;继续#无关
 [-f$ NEWNAME]&放大器;&安培;继续#不要覆盖现有文件
  MV - $文件,$ NEWNAME完成

我不跟下面几行清晰的结果
5) [-f$文件] ||继续#忽略不存在的名称结果
如果$文件存在那么什么是使用OR条件我不是用[]明确的测试条件||在这里和continue语句结果
6) [-f$ NEWNAME]&放大器;&安培;继续#这里不覆盖现有文件相同的疑问,在5点


解决方案

  $ {//˚F/ _}

表示:用下划线代替空格所有出现

REF


  $ {1%/ *}

表示:删除 / * 从字符串的结尾

的最短匹配

REF


$ {1 ## * /}

表示:删除 * / 从字符串前面

的最长匹配

REF



  

当它说 MV$ 1什么是$ 1意味着


$ 1 是第一个位置参数。所以,如果你的脚本有此

 回声$ 1

和你这样调用

  foo.sh 111

然后输出将是

  111



  

终于find命令被关闭与 -exec<&东西GT; _ {} \\;
  现在有什么用下划线 _ 中,花括号 {} 和反斜杠
  后跟一个冒号上述 \\;


下划线是参数的占位符 $ 1,0

REF

{} \\; 成语用来与找到,说:一次为每个文件运行此命令

REF


  [-f$文件] ||继续

意味着:如果 $文件不是一个文件,然后继续(立即停止循环的当前迭代,并开始下一个)

REF

I am reading a guide here http://mywiki.wooledge.org/BashFAQ/030 on this link a few examples are given I am trying to understand them one example code says

# Bash
# Replace all spaces with underscores
for f in *\ *; do mv -- "$f" "${f// /_}"; done

what I have known till now was use of a backslash for special characters like space of ~ or # etc in case of search and replace examples or in shell scripts here in above example they have
used ${f// /_}
forward slashes , I am not clear with this is this allowed?
in another example on same page they give an example to remove space and replace it with underscores

find . -depth -name "* *" -exec bash -c 'dir=${1%/*} base=${1##*/};
mv "$1" "$dir/${base// /_}"' _ {} \;

in above example I am not clear with following
1) dir=${1%/*}
2) base=${1##*/}
3) and when it says mv "$1" what is meant by $1 in the above statement,
4) finally the find command is being closed with -exec <something> _ {} \;
now what is the use of an underscore _ ,curly braces {} and a backslash followed by a colon above \;?
in the third example they say

# tolower - convert file names to lower case
# POSIX
for file in "$@"do 
   [ -f "$file" ] || continue                # ignore non-existing names   
 newname=$(echo "$file" | tr '[:upper:]' '[:lower:]')     # lower case   
 [ "$file" = "$newname" ] && continue      # nothing to do   
 [ -f "$newname" ] && continue             # don't overwrite existing files  
  mv -- "$file" "$newname"done 

I am not clear with following lines
5) [ -f "$file" ] || continue # ignore non-existing names
I am not clear with [ ] tests the condition if $file exists then what is the use of OR condition || here and the continue statement
6) [ -f "$newname" ] && continue # don't overwrite existing files same doubt here as in point 5

解决方案

${f// /_}

means: replace all occurences of spaces with underscores

ref


${1%/*}

means: remove shortest match of /* from end of string

ref


${1##*/}

means: remove longest match of */ from front of string

ref


when it says mv "$1" what is meant by $1

$1 is the first positional parameter. So if your script has this

echo $1

and you call like this

foo.sh 111

then output will be

111


finally the find command is being closed with -exec <something> _ {} \; now what is the use of an underscore _ , curly braces {} and a backslash followed by a colon above \;?

The underscore is a placeholder for parameter $0

ref

The {} \; idiom is used with find, to say: run this command once for each file

ref


[ -f "$file" ] || continue

means: if $file is not a FILE, then continue (immediately end the current iteration of the loop and start the next)

ref

这篇关于在bash中重命名文件的陷阱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆