我需要收集一个文件列表,并把它放在一个文本文件。 但是,这些文件不应包含其扩展名。
我现在没有删除文件扩展名的命令:
ls -1 /a/dir/*/dir/* > textfile
所有选定的文件将有一个扩展。[az] {3}
(我需要这个的原因是因为我需要一个唯一的基名称列表,并且unique将解决这个步骤后)
我总是喜欢只包含一个bash解决scheme:
for file in /a/dir/*/dir/*; do echo "${file%.*}" >> ~/file_list done
并使用可选的if语句将文件限制为您所请求的扩展名:
shopt -s extglob for file in /a/dir/*/dir/*; do if [[ "$file" =~ \.[a-zA-Z]{3}$ ]]; then echo "${file%.*}" >> ~/file_list fi done
在这个类似的serverfault问题中,我解释了“$ {file%。*}”参数的扩展。
类似的东西会列出当前目录中的文件,删除第一个点之后的所有内容。
find . -maxdepth 1 -type f | xargs -iZ basename Z | cut -d"." -f1 > /tmp/resultfile
它最终会隐藏文件,导致结果文件中出现一个空行。 Basename在这里只是为了去除在查找结果中的文件名“./”。
怎么样:
ls -1 / a / dir / * / dir / * | sed -e's / \ [a-zA-Z0-9] \ {3 \} $ //'
应该做你想做的。
比想象的简单得多:
asd @ locutus:〜$ IZE = file.avi
asd @ locutus:〜$ echo $ IZE
file.avi
asd @ locutus:〜$ echo $ {IZE %%。???}
文件
ASD @ locutus:〜$
所以这样:
ls -1 / a / dir / * / dir / * | 同时读取VAR; 做echo $ {VAR%。???}; DONE
你可以在做和做完之间做任何你想做的事情,回声就是一个例子。
男子bash(参数扩展):
${parameter%%word} The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of param‐ eter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ''%'' case) or the longest match‐ ing pattern (the ''%%'' case) deleted. If parameter is @ or *, the pat‐ tern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array vari‐ able subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
更通用的答案,可以find任何长度的扩展:
find . -type f | sed 's/^.*\.\([^.]*\)$/\1/'
然后,我使用以下命令获取dir中的扩展名列表:
find . -type f | sed 's/^.*\.\([^.]*\)$/\1/' | sort | uniq -c | sort -n
你也可以这样使用剪切:
for file in /a/dir/*/dir/* ; do echo \`echo $file | cut -d"." -f 1` >> textfile; done
它只有在你没有任何带有“。”的文件或目录时才能工作。 在名字里
您已经给出了扩展的明确定义,
但是,标签linux和bash-scriptingbuild议unix环境。
除非你在Windows上使用Cygwin。
在这种情况下,这些点可能并不重要。
md5 ) script.sh )