最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Linux shell中的空的(左右)大括号的含义

Linux crifan 6834浏览 0评论

【背景】

折腾:

【已解决】用find和cp组合实现将find出来的文件批量拷贝到目标文件夹

期间,虽然搞懂了find和xarg加上cp的用法,但是不知道其中:

find . -name *.pdf | xargs -i cp {} ../docbook_pdf/

出现的空的大括号{}的含义。

【折腾过程】

1.去参考了:

Bash Brace Expansion

知道了:

{a,b,c}

是以此展开每个item,但是空的大括号:{},是啥意思,还是不知道。

2.参考:

16.2. Complex Commands

终于明白了:

空的(左右)大括号:{}

的含义是:

占位符

意思是:

对于(find等输出的一个列表的内容)依次循环每一个;

其给出了一些例子:

find ~/ -name 'core*' -exec rm {} \;
# Removes all core dump files from user's home directory.
 
find /home/bozo/projects -mtime -1
#                               ^   Note minus sign!
#  Lists all files in /home/bozo/projects directory tree
#+ that were modified within the last day (current_day - 1).
#
find /home/bozo/projects -mtime 1
#  Same as above, but modified *exactly* one day ago.
#
#  mtime = last modification time of the target file
#  ctime = last status change time (via 'chmod' or otherwise)
#  atime = last access time

DIR=/home/bozo/junk_files
find "$DIR" -type f -atime +5 -exec rm {} \;
#                          ^           ^^
#  Curly brackets are placeholder for the path name output by "find."
#
#  Deletes all files in "/home/bozo/junk_files"
#+ that have not been accessed in *at least* 5 days (plus sign ... +5).
#
#  "-type filetype", where
#  f = regular file
#  d = directory
#  l = symbolic link, etc.
#
#  (The 'find' manpage and info page have complete option listings.)
 
find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;

# Finds all IP addresses (xxx.xxx.xxx.xxx) in /etc directory files.
# There a few extraneous hits. Can they be filtered out?

# Possibly by:

find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' \
| grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'
#
#  [:digit:] is one of the character classes
#+ introduced with the POSIX 1003.2 standard. 

# Thanks, Stéphane Chazelas. 

 

注:

 

另外的,比如find的话,对于输出结果,也是可以通过-exec去执行的,所以可以有下面例子中的写法:

#!/bin/bash
# badname.sh
# Delete filenames in current directory containing bad characters.

for filename in *
do
  badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p`
# badname=`echo "$filename" | sed -n '/[+{;"\=?~()<>&*|$]/p'`  also works.
# Deletes files containing these nasties:     + { ; " \ = ? ~ ( ) < > & * | $
#
  rm $badname 2>/dev/null
#             ^^^^^^^^^^^ Error messages deep-sixed.
done

# Now, take care of files containing all manner of whitespace.
find . -name "* *" -exec rm -f {} \;
# The path name of the file that _find_ finds replaces the "{}".
# The '\' ensures that the ';' is interpreted literally, as end of command.

exit 0

#---------------------------------------------------------------------
# Commands below this line will not execute because of _exit_ command.

# An alternative to the above script:
find . -name '*[+{;"\\=?~()<>&*|$ ]*' -maxdepth 0 \
-exec rm -f '{}' \;
#  The "-maxdepth 0" option ensures that _find_ will not search
#+ subdirectories below $PWD.

# (Thanks, S.C.)

即:

find . -name "* *" -exec rm -f {} \;

其中,最后的反斜杠的含义是:

参考:

Unable to understand the syntax of the command find

用于识别最后的参数

即,反斜杠后面,就是find的另外一个参数了。

举例:

find euler/ -iname "*.c*" -exec echo {} \; -or -iname "*.py" -exec echo {} \;

 

【总结】

Linux中的(Bash的)shell中的空的大括号,是占位符的含义。

举例:

find . -name *.pdf | xargs -i cp {} ../docbook_pdf/

此处空的大括号就是:

find出来的,每一个文件,的意思。

对于每一个文件,占座,等find出来后,放到对应的{}的位置。

转载请注明:在路上 » 【已解决】Linux shell中的空的(左右)大括号的含义

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 很用心的总结。thx~
    zqhong10年前 (2014-11-04)回复
85 queries in 1.068 seconds, using 22.14MB memory