Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display contents of all files under a directory on the screen using unix commands

Tags:

Using cat command as follows we can display content of multiple files on screen

cat file1 file2 file3

But in a directory if there are more than 20 files and I want content of all those files to be displayed on the screen without using the cat command as above by mentioning the names of all files.

How can I do this?

like image 411
bvb Avatar asked Jan 22 '14 08:01

bvb


People also ask

Which Unix command can display the contents of files?

You can display the contents of a file using the cat command, which stands for concatenate.

Which command is used to display all the files and the directories?

The ls command is used to list files. "ls" on its own lists all files in the current directory except for hidden files.

What is the Unix Linux command for viewing files in a directory?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


3 Answers

You can use the * character to match all the files in your current directory.
cat * will display the content of all the files.
If you want to display only files with .txt extension, you can use cat *.txt, or if you want to display all the files whose filenames start with "file" like your example, you can use cat file*

like image 161
Munim Avatar answered Sep 29 '22 09:09

Munim


If it's just one level of subdirectory, use cat * */* Otherwise,

find . -type f -exec cat {} \;

which means run the find command, to search the current directory (.) for all ordinary files (-type f). For each file found, run the application (-exec) cat, with the current file name as a parameter (the {} is a placeholder for the filename). The escaped semicolon is required to terminate the -exec clause.

like image 34
rojomoke Avatar answered Sep 29 '22 10:09

rojomoke


I also found it useful to print filename before printing content of each file:

find ./ -type f | xargs tail -n +1

It will go through all subdirectories as well.

like image 21
Oleg Oleg Avatar answered Sep 29 '22 08:09

Oleg Oleg