问题
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.
回答1:
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).
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 *)
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 *)
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.
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
回答2:
You have to be explicit, but extended patterns make that a little easier if the file should come first.
shopt -s extglob
cat vars.css !(vars.css) > result
!(vars.css)
is like *
, but matches everything except vars.css
.
Otherwise, you are looking at defining a custom locale in which vars.css
sorts in the desired location relative to the other files. It's really not something that simple globbing is suitable for.
来源:https://stackoverflow.com/questions/39796743/cat-and-order-of-files