- How to display contents of all files under a directory on the screen using unix commands
- 7 Answers 7
- How do i open all files that are the result of a ls command?
- 3 Answers 3
- How do I see all files in a directory Linux?
- How can I get a list of files in a directory?
- Which command in Linux is used to list all the files in the current directory?
- How do I open a directory in Linux terminal?
- How do I open and edit a file in Linux terminal?
- Open all files in a folder
- 6 Answers 6
- Get a list of all files in folder and sub-folder in a file
- 7 Answers 7
- tree
How to display contents of all files under a directory on the screen using unix commands
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?
7 Answers 7
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*
It is working for current directory but If I want to display the content of files under a sub directory of current directory then how?
Otherwise do find . -type f -exec cat <> \; Be careful you don’t have any non-character files, as you might screw up your display if you cat those.
@bvb that wasn’t in your question, but yes, if you want to go to one subdirectory below too, you can do a cat * */* .
If it’s just one level of subdirectory, use cat * */* Otherwise,
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.
How do i open all files that are the result of a ls command?
I’d like to open all text files that are the result of a ls command using a text editor. How do i do this?
3 Answers 3
Gilles is exactly right, ls is a really bad example because file name glob expansion can be done much easier on the command line without having to use ls at all! If your so called «text files» have file name extensions to identify them, you could do something like this:
For the sake of demonstrating a technique, lets use a more complicated example that could not be done with just a filename match and open files based on the content instead of just the file name. Lets say you wanted to open all the files that contained the string «content-type».
Assuming your editor will accept multiple file names and open them all at once in separate buffers or sequentially work it’s way through them, you can simply run:
editor-command $(grep -i content-type)
Now back to your original question, lets say you don’t know if they are text files or not based on their names. You would then need to use another program to identify them, then open them based on that data. The program file will tell you what kind of file something is, and you can grep that list for just text files, and then open just the matching file names like this:
editor-command $(file -ni * | grep 'text/plain' | cut -d: -f1)
The output of the command chain inside the $() construct will be used as arguments for the editor. I sometimes do this in two stages. Say I’m looking through some set of files and get myself a list of every xml file containing the string «content-type»
find -type f -iname '*xml' | xargs grep -Hi 'content-type' | cut -d: -f1
. and decide I want to open them. I then use the last command history shortcut and do this:
. to open all of the results of the previous command in vim, my favorite editor.
If your editor will only accept one file at a time and you need to keep spawing editors, you will need to use a variant of either the xargs or for loop in jmtd’s answer depending on whether you are using a terminal based editor that needs stdio.
How do I see all files in a directory Linux?
The answer to my question was you can’t, but you can open two files in one bash window using VIM’s :split command:
- Open a file with $ vim file1 , open a second file within VIM using :split file2 command.
- Or, use $ vim -o file1 file2 from bash.
- Switch between files–toggle active file–in VIM with ctrl – w ctrl – w .
How do you open a file in a directory in Linux?
- To navigate into the root directory, use “cd /”
- To navigate to your home directory, use “cd” or “cd ~”
- To navigate up one directory level, use “cd ..”
- To navigate to the previous directory (or back), use “cd -“
How do I view files in Linux?
Open the file using tail command.
- Open File Using cat Command. This is the most popular and easy way to display the file content.
- Open File Using less Command.
- Open File Using more Command.
- Open File Using nl Command.
- Open File Using gnome-open Command.
- Open File by Using head Command.
- Open the file by Using tail Command.
How can I get a list of files in a directory?
Which command in Linux is used to list all the files in the current directory?
The ls command
The ls command is used to list files. “ls” on its own lists all files in the current directory except for hidden files.
How do I open a Gvim file?
You can mouse click your way around or use the keyboard. Click or hit the enter key on the file you want to open it. Try using the keyboard to position the cursor over the file you want to open and then hit ‘t’. This opens the selected file in a new tab, keeping the file browser open in the first tab.
How do I open multiple windows in Linux?
You can also have multiple windows with panes and switch between them.
- Create windows: Ctrl b and c.
- Switch to next window: Ctrl b and n.
- Switch to previous window: Ctrl b and p.
- Destroy current window: Ctrl b and Shift 7.
How do I open a directory in Linux terminal?
(In)CLI Method: You can open folder in terminal by cd folder1 or dir folder1 or ls folder1 . I have found that simply typing gnome-open “any-oject” opens any folder or file in the default program on Ubuntu.
How do I open and edit a file in Linux terminal?
To modify the configuration files:
- Log on to the Linux machine as “root” with a SSH client such as PuTTy.
- Back up the configuration file you would like to edit in /var/tmp with the command “cp”. For example: # cp /etc/iscan/intscan.ini /var/tmp.
- Edit the file with vim: Open the file in vim with the command “vim”.
How do I open and edit a file in Linux?
How to edit files in Linux
- Press the ESC key for normal mode.
- Press i Key for insert mode.
- Press :q! keys to exit from the editor without saving a file.
- Press :wq! Keys to save the updated file and exit from the editor.
- Press :w test. txt to save the file as test. txt.
How do I get a list of files in a directory and subfolder?
Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.
Open all files in a folder
Suppose you’d like to open all the files in your checkout folder under the /trunk subdirectory. Assume the files are called first.c second.r third.cpp. How can you open all the files into vim with a single command. The obvious answer is the following
$ vim first.c second.r third.cpp
6 Answers 6
To edit all files in the current folder, use:
To edit all files in tabs, use:
To edit all files in horizontally split windows, use:
For completeness sake: To edit all files in vertically split windows, use vim -O * And to close everything use the classic :qa
Sounds like you’re on linux or some Unix variant. Using the asterisk gets you all files in the current folder:
stackoverflow is truly a wonder in the modern era. Your solution is simple, easy to remember and works.
@Milktrader, you do realise its a shell thing and not a vim thing, right?. How do you copy all the files in a folder? cp * /some/other/place . How do you delete all the files in a folder? rm * etc.
This will not open dot files by default. For all files you need <,.>* (in bash, assuming that exists at least one file that start with a dot) or (|.)* (in zsh, works even if there are no dot files).,.>
Check out the vim man page for some more info about using command line arguments to vim. I like vim * -p to open in tabs, or vim -O fileA fileB to open a couple of files in a split view. If they are similar I might use vimdiff file
David — I will check out vim man now that I know that information lies therein, thanks. The extra information you posted is particularly useful. Jeff- I see how you characterize this as a shell question. vim and shell are very closely related so knowing one helps with operating the other. I characterized it as a vim question on the basis of accomplishing a specific task in vim, on which I was stumped. Next time I’ll look for answers in shell and vim forums.
Get a list of all files in folder and sub-folder in a file
How do I get a list of all files in a folder, including all the files within all the subfolders and put the output in a file?
7 Answers 7
You can do this on command line, using the -R switch (recursive) and then piping the output to a file thus:
this will make a file called filename1 in the current directory, containing a full directory listing of the current directory and all of the sub-directories under it.
You can list directories other than the current one by specifying the full path eg:
will list everything in and under /var and put the results in a file in the current directory called filename2. This works on directories owned by another user including root as long as you have read access for the directories.
You can also list directories you don’t have access to such as /root with the use of the sudo command. eg:
sudo ls -R /root > filename3
Would list everything in /root, putting the results in a file called filename3 in the current directory. Since most Ubuntu systems have nothing in this directory filename3 will not contain anything, but it would work if it did.
Maybe telling the person to cd into the directory first could be added to answer.Also this works fine if i own the directory but if trying in a directory say owned by root it didnt.I got the usual permission denied and sudo followed by your command also gave permission denied. IS there a work around without logging in as root?
Well I did say «current» directory. The correct use of CD might the subject of another question, and I’m sure it has been. You can list directories owned by root as long as you have read access to them. Directories owned by root to which the user has read access can be listed with ls -R. It’s hard to imagine why you’d want to list directories owned by root to which you don’t have read access, but sudo does indeed work if you give the full path. I’m adding examples for both of these, but excluding the use of CD.
Just use the find command with the directory name. For example to see the files and all files within folders in your home directory, use
Also check find GNU info page by using info find command in a terminal.
This is the most powerful approach. find has many parameters to customize output format and file selection.
That’s the best approach in my opinion. Simple and practical. Could also do $ find . > output if there’s many directories.
tree
An alternative to recursive ls is the command line tool tree that comes with quite a lot of options to customize the format of the output diplayed. See the manpage for tree for all options.
will give you the same as tree using other characters for the lines.
to display hidden files too
- Go to the folder you want to get a content list from.
- Select the files you want in your list ( Ctrl + A if you want the entire folder).
- Copy the content with Ctrl + C .
- Open gedit and paste the content using Ctrl + V . It will be pasted as a list and you can then save the file.
This method will not include subfolder, content though.
You could also use the GUI counterpart to Takkat’s tree suggestion which is Baobab. It is used to view folders and subfolders, often for the purpose of analysing disk usage. You may have it installed already if you are using a GNOME desktop (it is often called disk usage analyser).
sudo apt-get install baobab
You can select a folder and also view all its subfolders, while also getting the sizes of the folders and their contents as the screenshot below shows. You just click the small down arrow to view a subfolder within a folder. It is very useful for gaining a quick insight into what you’ve got in your folders and can produce viewable lists, but at the present moment it cannot export them to file. It has been requested as a feature, however, at Launchpad. You can even use it to view the root filesystem if you use gksudo baobab .
(You can also get a list of files with their sizes by using ls -shR ~/myfolder and then export that to file.)