If I have a BASH variable:
如果我有一个BASH变量:
Exclude="somefile.txt anotherfile.txt"
How can I get the contents of a directory but exclude the above to files in the listing? I want to be able to do something like:
如何获取目录的内容,但将上述内容排除在列表中的文件中?我希望能够做到这样的事情:
Files= #some command here
someprogram ${Files}
someprogram
should be given all of the files in a particular directory, except for those in the ${Exclude}
variable. Modifying someprogram
is not an option.
someprogram应该被赋予特定目录中的所有文件,除了$ {Exclude}变量中的那些文件。修改someprogram不是一个选项。
4 个解决方案
#1
I'm not sure if you were taking about unix shell scripting, but here's a working example for bash:
我不确定你是否正在使用unix shell脚本,但这是一个bash的工作示例:
#!/bin/bash
Exclude=("a" "b" "c")
Listing=(`ls -1Q`)
Files=( $(comm -23 <( printf "%s\n" "${Listing[@]}" ) <( printf "%s\n" "${Exclude[@]}"
) ) )
echo ${Files[@]}
Note that I enclosed every filename in Exclude with double quotes and added parenthesis around them. Replace echo
with someprogram
, change the ls command to the directory you'd like examined and you should have it working. The comm
program is the key, here.
请注意,我使用双引号括起Exclude中的每个文件名,并在它们周围添加括号。将echo替换为someprogram,将ls命令更改为您要检查的目录,并且应该让它工作。 comm程序是关键,在这里。
#2
You can use find. something like:
你可以使用find。就像是:
FILES=`find /tmp/my_directory -type f -maxdepth 1 -name "*.txt" -not -name somefile.txt -not -name anotherfile.txt`
where /tmp/my_directory is the path you want to search.
其中/ tmp / my_directory是您要搜索的路径。
You could build up the "-not -name blah -not -name blah2" list from Excludes if you want with a simple for loop...
如果你想要一个简单的for循环,你可以从Excludes中建立“-not -name blah -not -name blah2”列表...
#3
Here's a one liner for a standard Unix command line:
这是标准Unix命令行的一个内容:
ls | grep -v "^${Exclude}$" | xargs
ls | grep -v“^ $ {Exclude} $”| xargs的
It does have one assumption. ${Exclude} needs to be properly escaped so charaters like period aren't interpreted as part of the regex.
它确实有一个假设。 $ {Exclude}需要被正确转义,因此像句点这样的字符不会被解释为正则表达式的一部分。
#4
Assuming filenames with no spaces or other pathological characters:
假设文件名没有空格或其他病态字符:
shopt -s extglob
Files=(!(@(${Exclude// /|})))
#1
I'm not sure if you were taking about unix shell scripting, but here's a working example for bash:
我不确定你是否正在使用unix shell脚本,但这是一个bash的工作示例:
#!/bin/bash
Exclude=("a" "b" "c")
Listing=(`ls -1Q`)
Files=( $(comm -23 <( printf "%s\n" "${Listing[@]}" ) <( printf "%s\n" "${Exclude[@]}"
) ) )
echo ${Files[@]}
Note that I enclosed every filename in Exclude with double quotes and added parenthesis around them. Replace echo
with someprogram
, change the ls command to the directory you'd like examined and you should have it working. The comm
program is the key, here.
请注意,我使用双引号括起Exclude中的每个文件名,并在它们周围添加括号。将echo替换为someprogram,将ls命令更改为您要检查的目录,并且应该让它工作。 comm程序是关键,在这里。
#2
You can use find. something like:
你可以使用find。就像是:
FILES=`find /tmp/my_directory -type f -maxdepth 1 -name "*.txt" -not -name somefile.txt -not -name anotherfile.txt`
where /tmp/my_directory is the path you want to search.
其中/ tmp / my_directory是您要搜索的路径。
You could build up the "-not -name blah -not -name blah2" list from Excludes if you want with a simple for loop...
如果你想要一个简单的for循环,你可以从Excludes中建立“-not -name blah -not -name blah2”列表...
#3
Here's a one liner for a standard Unix command line:
这是标准Unix命令行的一个内容:
ls | grep -v "^${Exclude}$" | xargs
ls | grep -v“^ $ {Exclude} $”| xargs的
It does have one assumption. ${Exclude} needs to be properly escaped so charaters like period aren't interpreted as part of the regex.
它确实有一个假设。 $ {Exclude}需要被正确转义,因此像句点这样的字符不会被解释为正则表达式的一部分。
#4
Assuming filenames with no spaces or other pathological characters:
假设文件名没有空格或其他病态字符:
shopt -s extglob
Files=(!(@(${Exclude// /|})))