There is one saying in Linux/*nix world, that if you can play with files and folders in Unix you are almost there to become a Linux power user. So let us start our first lesson to become Unix expert by learning about files and folder operations.

How to create a file in Linux

We can create a file in Linux in multiply ways such as using editor’s like vi, ed, emacs or cat command or touch or even with redirection operators in Linux. Creating files depends on the situation where you are.

Use touch command to create empty file’s

SYNTAX:

touch filename

Example:

touch test
T
o create multiple files at a time using touch use below example.

touch {filename1 filename2 filename3}

Example:
touch {1,2,3,4}

The above command will create files with 1, 2, 3, 4 as names respectively. By using this you can create n number of files at a time.

For checking weather your files/folders are created you can use ls command.

[email protected]:/home/surendra# ls -l
 total 32
 -rw-r--r-- 1 root root 0 Mar 6 18:13 1
 -rw-r--r-- 1 root root 0 Mar 6 18:13 2
 -rw-r--r-- 1 root root 0 Mar 6 18:13 3
 -rw-r--r-- 1 root root 0 Mar 6 18:13 4
 Creating files using cat command in combination of output redirecting operator(>)
cat > filename

Once enter this command you have to enter the content of the file and once you finish it just press Ctrl+d this will save the file. Use again ls command to display the files created.

[email protected]:/home/surendra/code/sh/arun/abc# cat > test.txt
 asdfasd
 asdf
 asdfa
 sdfasdf
 aewrasd

Now press ctrl+d once you are done with entering data.

[email protected]:/home/surendra/code/sh/arun/abc# cat test.txt
 asdfasd
 asdf
 asdfa
 sdfasdf
 aewrasd

Creating files using redirection operators. We can create an empty file using > as shown below.

> abc.txt

Output of ls command:

[email protected]:/home/surendra/code/sh/arun/abc# > abc.txt
[email protected]:/home/surendra/code/sh/arun/abc# ls -l
total 36
-rw-r–r– 1 root root 0 Mar 6 18:13 1
-rw-r–r– 1 root root 0 Mar 6 18:13 2
-rw-r–r– 1 root root 0 Mar 6 18:13 3
-rw-r–r– 1 root root 0 Mar 6 18:13 4
-rw-r–r– 1 root root 0 Mar 6 21:50 abc.txt
-rw-rw-r– 1 surendra surendra 30164 Dec 10 2014 abc.zip
-rw-r–r– 1 root root 35 Mar 6 18:15 test.txt

Creating a file with any editor(either VI,emacs etc)
vi filename

Once you execute this command the file will be opened and in order to enter any data we have to first press i to go in to insert mode. Once you type the content in to the file just press esc and :wq to save and quit the file. To know vi editor you should have a look at my vi editor cheatsheet.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.