开发者

How to display or append several files in a terminal using shell script?

开发者 https://www.devze.com 2023-03-14 16:02 出处:网络
I would like to display several files one after another in a terminal, for instance, all the files of type .java in the current folder. Does anyone know how to do that by a line of shell? I guess prob

I would like to display several files one after another in a terminal, for instance, all the files of type .java in the current folder. Does anyone know how to do that by a line of shell? I guess probably we need to use cat and a variable...

Also, if开发者_Go百科 possible, I would like to add the name of the files... For instance, the final layout in the terminal would be

p1.java
... 
contents of p1.java 
...  

p2.java
... 
contents of p2.java 
...  

Does anyone know how to do it? Thank you very much!


Could be as simple as:

$ cat *.java

If you want to display the filename before each listing, just use a loop:

$ for fn in *.java; do echo $fn; cat $fn; done

Just to enrich this answer a little bit: If you browse source code in your terminal regularly, you can get the content syntax highlighted as well via pygments. I use the following tiny function in my bashrc.

function pless() {
    type -P pygmentize &> /dev/null || { 
        echo "pygmentize required but not installed. Aborting." >&2; return 1; 
    }
    pygmentize "$@" | less -r 
}

Here's an example screenshot:

How to display or append several files in a terminal using shell script?


Simple: cat file1 && cat file2

Or, alternatively: cat dir/*


Use this shell script.

#!/bin/sh

for filename in `find /path_to_file -maxdepth 1 -name "*.java"`
do
        cat $filename
done

What is the use of find command is, you can specify 'maxdepth'. You can go into the deeper of the current folder and read .java files.

0

精彩评论

暂无评论...
验证码 换一张
取 消