How To Create Empty Files For Existing Files In Unix

Empty File

In this example I will show you how to create empty files for files in Unix or Linux based systems. You may need to create empty files for a list of corresponding files in a directory or folder. To create an empty file generally you may use touch command in Unix shell terminal.

It is always simple to execute a command like touch to create a single empty file, but when you need to create a list of empty files then probably you want to execute the same command through loop.

Even a situation may occur, where you need to create empty files for a list of files in a folder then you need to read those files to create corresponding empty files. Your empty files may contain addition extension otherwise you may loose data in the original files.

Prerequisites

Knowledge of Unix

Create Empty Files

What I am going to do here is reading files from a particular folder and creating empty files with an additional extension to the original file names.

for filename in *; do touch "${filename}.ext"; done

I am executing touch command in a for loop to create empty file. First I am iterating through files using for loop. Then for each filename I am creating a new empty file with additional extension .ext to the original file name.

If you want to list all files from the current directory then you can execute the following line of code:

for filename in *; do echo "${filename}"; done

You may save the above line of code (for creating or displaying) in a shell script file. Later you can execute the shell script file to do the necessary job.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *