假设我想复制一个目录的内容,不包括名称包含单词“音乐”的文件和文件夹。
cp [exclude-matches] *Music* /target_directory
应该用什么来代替[排除匹配]来实现这一点?
假设我想复制一个目录的内容,不包括名称包含单词“音乐”的文件和文件夹。
cp [exclude-matches] *Music* /target_directory
应该用什么来代替[排除匹配]来实现这一点?
当前回答
如果您想避免使用exec命令的mem成本,我相信您可以使用xargs做得更好。我认为以下是一个更有效的替代
find foo -type f ! -name '*Music*' -exec cp {} bar \; # new proc for each exec
find . -maxdepth 1 -name '*Music*' -prune -o -print0 | xargs -0 -i cp {} dest/
其他回答
find可以找到一个解决方案。
$ mkdir foo bar
$ touch foo/a.txt foo/Music.txt
$ find foo -type f ! -name '*Music*' -exec cp {} bar \;
$ ls bar
a.txt
Find有相当多的选项,你可以非常具体地包括和排除什么。
编辑:Adam在评论中指出,这是递归的。查找选项mindepth和maxdepth可以用来控制这个。
不是bash(据我所知),而是:
cp `ls | grep -v Music` /target_directory
我知道这不是你想要的,但它能解决你的问题。
你也可以使用一个非常简单的for循环:
for f in `find . -not -name "*Music*"`
do
cp $f /target/dir
done
下面的作品列出了当前目录下的所有*.txt文件,除了以数字开头的文件。
这适用于bash, dash, zsh和所有其他POSIX兼容的shell。
for FILE in /some/dir/*.txt; do # for each *.txt file
case "${FILE##*/}" in # if file basename...
[0-9]*) continue ;; # starts with digit: skip
esac
## otherwise, do stuff with $FILE here
done
In line one the pattern /some/dir/*.txt will cause the for loop to iterate over all files in /some/dir whose name end with .txt. In line two a case statement is used to weed out undesired files. – The ${FILE##*/} expression strips off any leading dir name component from the filename (here /some/dir/) so that patters can match against only the basename of the file. (If you're only weeding out filenames based on suffixes, you can shorten this to $FILE instead.) In line three, all files matching the case pattern [0-9]*) line will be skipped (the continue statement jumps to the next iteration of the for loop). – If you want to you can do something more interesting here, e.g. like skipping all files which do not start with a letter (a–z) using [!a-z]*, or you could use multiple patterns to skip several kinds of filenames e.g. [0-9]*|*.bak to skip files both .bak files, and files which does not start with a number.
我还没有在这里看到一个不使用extglob, find或grep的技巧是将两个文件列表视为集合,并使用comm对它们进行“diff”:
comm -23 <(ls) <(ls *Music*)
Comm比diff更可取,因为它没有额外的麻烦。
返回集合1,ls中不存在于集合2,ls *Music*中的所有元素。这需要两个集合都按顺序排序才能正常工作。ls和glob展开没有问题,但如果使用find之类的东西,一定要调用sort。
comm -23 <(find . | sort) <(find . | grep -i '.jpg' | sort)
可能有用。