How to put a list (names) of all files in folder in one file (example in Modicia Linux)

Written by Administrator on Thursday April 23, 2020

Just imagine you have a folder with 100 digital books in different format, meaning you have a lot of files with different file extensions and you want to share the list with your friends. In Linux you don't need to install additional software to do it. Standard terminal, which is a part of every (EVERY!) linux distribution, can help you to do it within a second.

For my example I've chosen /etc folder in my Modicia linux system, because there are a lot of files. Let's put all file names (including file's extension) in one text file.

Solution is very easy and simple. First, we are listing all files and then we're saving those listed names in a file:

Go into a folder, where you have files (in my case it is /etc):

$ cd /etc

Then we have to list all files and save filenames into .txt file.

Listing of file in a folder is very easy: type this command in terminal:

$ ls

Linux is very comfortable to work with, because it gives you an opportunity to save output of every command executed in a file. you just add ">> filename" in the end of command. In our case:

$ ls >> filelist.txt

In that case the file filelist.txt will be created in a directory, you are currently in. In my case this file is created in /etc directory.

This is not all. Actually, you can be independent of the folder you're currently in. In that case you need to put absolute paths instead of relative. What does it mean? Let's look at an example:

Let's go to /home/modicia folder:

$ cd /home/modicia

and list all files and folders we have there:

$ ls

In my case I see this:

Now I want to put a list of all files, stored in scr folder (absolute path is /home/modicia/scr) in a file named "list_of_files.txt" and save this file in /home/modicia folder. Now we have two possibilities, how to do it.

Using relative paths

$ ls scr >> ../list_of_files.txt

Why this way? I'll try to explain. Now we are in /home/modicia folder and if we want to list files in scr folder, we just type "scr", because system already knows, that we are now in /home/modicia.  

Using absolute paths

$ ls /home/modicia/scr >> /home/modicia/list_of_files.txt

Now we are operating with the system regardless, where we are. That's why we are putting the whole path to scr folder (/home/modicia/scr). Concerning the name and location of list_of_files.txt file - the same situation. We don't care, where we are, so we are putting the whole "address" of the file (/home/modicia/list_of_files.txt).



Let's check, what we have in /home/modicia/scr folder:

$ ls

ouput:

and now let's check our list_of_files.txt file (what was written here):

and let's open list_of_files.txt file:

This is all we needed!

Category: linux Tag: modicia-linux