目录*和文件顺序 [英] Cat * and order of files

查看:88
本文介绍了目录*和文件顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将目录中的所有文件连接为一个文件,但是具有指定名称的文件必须位于输出的顶部.只做 cat *>result 将按字母顺序连接所有文件.有没有办法告诉cat将文件 vars.css 或其他文件放置到输出的开头?现在,我只需要将重命名的文件首先添加到 000-filename ,但是我想知道是否有更好的解决方案而不重命名文件.

I need to concatenate all files in a directory to one file, but files with specified names must be on top of the output. Just doing cat * > result will concatenate all files in alphabetical order. Is there any way to tell cat to place file vars.css or any other to the beginning of output? For now i just renamed files need to be first to 000-filename but i wonder if there is better solution without renaming files.

推荐答案

有很多方法可以实现此目的,其中大多数方法涉及调用另一个程序来帮助生成文件列表(按首选顺序).

There are many ways to achieve this, most of which would involve invoking another program to help generate the list of files (in the preferred order).

仅使用bash(并假设您要手动将少量文件移到前端)的一种方法是:

One way to achieve it using only bash (and assuming a small number of files you want to manually move to the front) would be:

(GLOBIGNORE = vars.css&& cat vars.css *)

如果您有多个文件要放在列表的最前面,则可以将其包含在GLOBIGNORE中:

If you have multiple files to bring to the front of the list, they can be included in GLOBIGNORE:

(GLOBIGNORE = vars.css:other.file& cat vars.css other.file *)

GLOBIGNORE环境变量告诉bash在扩展全局范围时忽略与这些模式匹配的文件(例如 * ).有关更多信息,请参见 bash手册.

The GLOBIGNORE environment variable tells bash to ignore files matching those patterns while expanding a glob (such as *). See the bash manual for more information.

重要的是要确保不要在外壳的其余部分上意外设置GLOBIGNORE(感谢Tim指出了这一点).您可以通过几种方法来实现这一目标,例如,通过将整个语句放在括号内,我已经完成了上述工作.如果运行时没有括号,则应确保之后未设置GLOBIGNORE:取消设置GLOBIGNORE

It is important to ensure that GLOBIGNORE is not accidentally set for the rest of the shell (thanks to Tim for pointing that out). You can achieve that several ways, such as I've done above by putting the whole statement inside of parentheses. If you ran it without parentheses, you should ensure you have unset GLOBIGNORE afterwards: unset GLOBIGNORE

这篇关于目录*和文件顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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