- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Create a File in the Linux/Unix system using terminal?
In the Linux/Unix operating system there are two ways available to creating files.
- Using the touch command
- Using the cat command
Creating a File using touch command
The touch command is used to create file/files without any content and update the access date or modification date of a file or directory in the Linux system. This is the simplest way to create a file in Linux/Unix using a terminal.
Syntax
The general syntax of the touch command is as follow −
$ touch [option] ... FILE...
Brief description of option available in the touch command.
Sr.No. | Option & Description |
---|---|
1 | -a Change the access time of file |
2 | -c,--no-create Check file is available or not if not then pervert to create a new file |
3 | -d, --date=STRING Set time of file instead of current time |
4 | -f Ignored |
5 | -m Change the modification time of file |
6 | -r, --reference=FILE Use another file time instead of current time |
7 | -t STAMP Use specified time instead of current time |
8 | --help Displays a help message and then exits. |
9 | --version It gives info about the version and then exits. |
Here, using the touch command we can create a file in the Linux system. Before executing touch command, we will check how many files are available in our current directory using the below command.
$ ls -l
After using below command a new file created newfile.txt in the current directory.
$ touch newfile.txt
To ensure that file is created or not we will again execute ls command to list the directory contents.
To change the access time of a file in the Linux system using -a option with touch command as shown in below.
$ touch -a newfile.txt
Creating a File using cat command
The cat (concatenate) command is used to create, view, concatenate files in the Linux operating system. The touch command is also used to create files in the Linux system without content whereas the cat creates files with some content. The cat command reads the content of a file and prompts it.
Syntax
The general syntax of the cat command is as follows −
$ cat [option]... FILE...
Brief description of option available in the cat command.
Sr.No. | Option & Description |
---|---|
1 | -A, --show-all Show all content of a file |
2 | -b, --number-nonblank Display number of non-empty lines, overrides -n |
3 | -n, --number Display number of all output lines. |
4 | -T, --show-tabs Display tab separated Lines |
5 | --help Displays a help message and then exits. |
6 | --version It gives info about the version and then exits. |
To create a file with some content, we use the cat command and file name after that write some content and press CTRL + C when writing is complete as shown in below.
$ cat >file.txt Hey, write some contents here... ^C
The cat command is also used to view the contents of the file. After using cat command along with file name contents of the file will be prompt as shown in below.
$ cat file.txt Hey, here your contents...
- Related Articles
- How to create key binds in the Linux system using the terminal?
- How to create a new directory in Linux using the terminal?
- How to Create a New Ext4 File System in Linux?
- How to remove files and directories in the Linux operating system using the terminal?
- How to change file or directory permission in Linux/Unix?
- How to flushes file system buffers in the Linux operating system?
- How to decorate your Linux Terminal using Shell?
- How to create an Animated Art on Your Linux Terminal?
- How to format contents of a text file in the Linux system?
- What are the differences between Unix and Linux Operating System?
- How to Create a New File in Linux from Bash?
- Formatted text in Linux Terminal using Python
- How to check total space and available space in Linux using the terminal?
- How to display the first part of the file in the Linux system?
- How to display the last part of the file in the Linux system?
