Linux Command: touch (create file)

“touch” is the most handy command to create a new file in Linux.

Note: Files in a Linux file system can exist without any extension. So don’t be surprised to see files without any extension in the file system.

Create file

To create a new file just type “touch” and then the file name-

$ touch bigboxfile.txt

$ ls -l

total 0
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 03:54 bigboxfile.txt
Bash

Use “touch” on Existing File

We can issue the “touch” command again with the same file name and that will update the timestamp of the file-

$ touch bigboxfile.txt

$ ls -l

total 0
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 03:56 bigboxfile.txt
Bash

Create Multiple Files

We can create multiple files using the same “touch” command at the same time. Just pass the file names one by one after “touch”-

$ touch file1 file2 file3

$ ls -l

total 0
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 03:56 bigboxfile.txt
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 04:04 file1
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 04:04 file2
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 04:04 file3
Bash

Alternative #1: echo

Alternative to the “touch” command we can use “echo” to create a new file-

$ echo > sample_file_1.txt

$ ls -l

total 4
-rw-r--r-- 1 bigboxcode bigboxcode 0 Jul 21 03:56 bigboxfile.txt
-rw-r--r-- 1 bigboxcode bigboxcode 1 Jul 21 03:58 sample_file_1.txt
Bash

We can create the file using “echo” and add content at the same time, by adding the content after “echo”-

$ echo "hello bigboxcode" > sample_file_2.txt

$ ls -l

total 8
-rw-r--r-- 1 bigboxcode bigboxcode  0 Jul 21 03:56 bigboxfile.txt
-rw-r--r-- 1 bigboxcode bigboxcode  1 Jul 21 03:58 sample_file_1.txt
-rw-r--r-- 1 bigboxcode bigboxcode 17 Jul 21 03:59 sample_file_2.txt

$ cat sample_file_2.txt

hello bigboxcode
Bash

Or you can append the new content to existing file content using “>>”. Check the example below-

$ echo alias gohome='cd ~' >> .bash_aliases
Bash

Alternative #2: cat

Use “cat” to create a file and then adding content to the fiel at the same time.

-> After “cat” use “>” and then the file name.
Press “Enter” and the terminal will wait for content.
Add some content and then press “CTRL + D” at the end.

# Press CTRL + D when you want to end the content input
$ cat > sample_cat_file.txt
big
box

code

last line here

$ ls -l

total 4
-rw-r--r-- 1 bigboxcode bigboxcode  0 Jul 21 03:56 bigboxfile.txt
-rw-r--r-- 1 bigboxcode bigboxcode  0 Jul 21 04:04 file1
-rw-r--r-- 1 bigboxcode bigboxcode  0 Jul 21 04:04 file2
-rw-r--r-- 1 bigboxcode bigboxcode  0 Jul 21 04:04 file3
-rw-r--r-- 1 bigboxcode bigboxcode 30 Jul 21 04:12 sample_cat_file.txt

# Check file content
$ cat sample_cat_file.txt
big
box

code

last line here
Bash

Command Info

$ touch --help

Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.
Bash

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.