How to Use Touch Command in Linux

by Colby Ernser
17 minutes read

How to Use Touch Command in Linux

Black As well as Sliver Laptop Using Mobile Feature Image Touch Command

Yearn to concoct a documents correct away, but don’t want to open a text editor or navigate with a graphical interface? Filter no even more than the touch command. This command is worthwhile for inventing bare files and also simplifying documents timestamps, which is fulcra for controling fallbacks and also implementing manuscripts capably.

This overview will explore how to utilise the touch command in Linux to concoct uncompanionable or innumerable bare files, or adjust timestamps for scripting ultimatums.

Content
  • How The Touch Command Jobs
  • Invent an Bare Documents
  • Invent Numerous Bare Data
  • Touch Command Contingencies
  • Replacement to Touch Command

How The Touch Command Jobs

In UNIX/Linux contraptions, the touch command is commonly brought out to correct away concoct bare files. In addition, you can utilise it to tweak the timestamps – namely the schedule and also recollection times of files that already exist.

If the documents doesn’t exist, touch will stimulate it, but without any kind of content. In addition, its ability to tweak timestamps makes it inestimable in scripting and also controling documents contraptions.

The syntax of the touch command is as adheres to:

touch [options] [file_name]

Invent an Bare Documents

Formulating an bare documents with the touch command is truly humble. Just utilise the touch command without any kind of flags/fallbacks:

touch newfile.txt

To attest the innovation of the documents, you can investigate its documents dimension obtaining utilise of the stat command:

stat newfile.txt
Studying the documents dimension of the bare documents obtaining utilise of the stat command.

In the output, considerable aesthetic attractions for the Size arena. If it substantiates 0, the documents is bare.

In addition, if the documents already exists, the touch command enhances its schedule (atime) and also recollection (mtime) times to the current time without influencing the documents’s contents or authorizations.

Invent Numerous Bare Data

What if you last offer supplementary than one bare documents? Using touch, you can supply innumerable files at when. For example, let’s concoct innumerable files that are differentiated by enclosures, earn a choice this:

touch file1.txt file2.txt file3.txt

You can attest the innovation of these innumerable bare files by sprinting this:

ls -l *.txt
Formulating and also summarizing the dimension of the innumerable bare files.

You can also concoct a series of files with phoned digit or lettered names obtaining utilise of curly braces.

Permit’s concoct innumerable files with phoned digit names:

touch file{1..3}.txt

Confirm this by seeing your files in the identified catalog.

Perceiving innumerable bare files in the identified catalog.

In a equal way, you can also concoct files with lettered names:

touch file_{a..e}.txt

Chit: The touch command can’t mixture digits and also letters in the truly same validate propagation.

Touch Command Contingencies

As cited earlier, that touch command isn’t merely around inventing files – it’s also a tool for controling documents timestamps. To adjust these timestamps, you have to recognize how touch command fallbacks job. Permit’s confiscate a considerable aesthetic attractions at them:

ReplacementDescription
-aEnriches the documents schedule time only.
-cHampers the innovation of a neoteric documents if it doesn’t already exist.
-d=, –day=Revises a documents’s timestamp obtaining utilise of a identified day string.
-mEnriches only the documents recollection time.
-rGains earn usage of of the atime and also mtime from a referral documents.
-tRevises a documents’s timestamp obtaining utilise of a identified day and also time.

Matching Availability Time of Documents

Availability time, or atime, is the timestamp that enhances whenever a documents’s contents are accessed obtaining utilise of command-jabber contraptions earn a choice grep or kitty, or text editors such as vim or nano.

By default, touch enhances both schedule and also recollection times. Yet, if you only want to modify the schedule time, you can utilise the -a recourse with the touch command. This is especially worthwhile for mimicking documents utilise or acclimating logs for particular process.

Before revising the schedule time, let’s initially display display the current schedule time of the documents we’re visiting modify:

Availability time of the identified documents in yesteryear revising.

Now, you can modify the schedule time of a identified documents obtaining utilise of touch -a command:

touch -a happy.txt

In addition, the overhead command will modernize the schedule time of the identified documents to the current time without influencing the mtime.

Permit’s attest the modify by sprinting:

ls -lu
Perceiving schedule time of the identified documents in terminal window after recollection.

In addition, you can also package a particular schedule time by finalizing the -a and also -t fallbacks. For example, to modify the identified documents schedule time to twelve o’clock at night on June 1st, 2010:

touch -at 201006010000 happy.txt

Here, the -a recourse readjusts the schedule time and also the -t recourse identifies the time in the layout [[CC]YY]MMDDhhmm[.ss].

Anew, to attest the readjusts, you can utilise this command:

ls -lu
Earmarking particular schedule time to a documents obtaining utilise of touch command fallbacks.

Matching Revision Time

The recollection time, or time, is the timestamp that readjusts whenever the contents of a documents are tweaked. Yet, it doesn’t track readjusts observed documents authorizations or possession.

To modernize only the last recollection time of a documents without spoiling the schedule time, you can utilise the -m recourse of touch command:

touch -m happy.txt

To attest the readjusts, utilise the stat command:

stat happy.txt
Showing the recollection time after transforming it obtaining utilise of the -m recourse.

This readjusts the mtime to the current moment while running away the schedule time, or atime unharmed.

If you have to package a particular timestamp for the recollection time, you have to fuse the -m fallbacks with the -t recourse. For example, to package the recollection time to 08:45 AM on June 19, 2014, dashed:

touch -mt 201406190845.00 happy.txt

Next, attest the boosted recollection time by sprinting:

Stat happy.txt
Showing the documents recollection time obtaining utilise of the stat command.

Match Revision and also Availability Time

There are instances where you can have to modernize both the atime and also mtime simultaneously. To implement that, utilise the touch command regardless of any kind of recourse.

touch happy.txt

Running this command readjusts both the atime and also mtime to the current time. Intake this when you want to bring a documents’s timestamps to teach current interactions or alterations without transforming the documents’s content.

In addition, you can utilise the -a and also -m recourse with each other with the touch command to tweak both the schedule and also recollection times:

touch -am happy.txt

You can attest the boosted times by sprinting this:

stat happy.txt

Formulating Documents Earning utilise of Identified Time

In some housings, you can want to package a documents’s timestamp to a particular time and also day instead than the current moment. The -t recourse obeyed by the desired timestamp in the layout [[CC]YY]MMDDhhmm[.ss], enables you to implement merely that.

For example, if you want to package the schedule and also recollection times of a particular documents to January 1, 2024, at 12:30 PM, you have to enter:

touch -t 202401011230.00 happy.txt

To attest the recollection, dashed:

stat happy.txt
Showing the atime and also mtime worthiness after revising it obtaining utilise of the -t recourse of touch command.

You can also utilise the -r recourse to modernize a documents’s timestamp to suit that of one more documents. For example, let’s modify the happy.txt documents timestamp based on the new_file.txt timestamp.

Yet in yesteryear recollection, let’s respect the recollection time of the identified files:

ls -l
list unanimously the files with their recollection time.

After that, modify a documents’s timestamp to suit one more documents’s timestamp:

touch -r happy.txt new_file.txt

Confirm the modify with:

ls -l
List unanimously the files along with their recollection times after transforming the timestamp with -r recourse.

Replacement to Touch Command

While touch is my go-to tool for fast documents innovation and also timestamp control, there are different other traits you can utilise. Permit’s explore a couple of of these fallbacks.

Pet cat Command

You commonly utilise the kitty command to display display documents contents, but it can also concoct files. By obtaining utilise of kitty with a redirection licensed operator, you can stimulate an bare documents:

cat > filename.txt

This command collects filename.txt and also enters you in input position. If you press Ctrl + D, it ends the input and also vacates the documents bare.

Redirection Vehicle driver

Redirection operators (>, >>) are one more behavior for inventing files. By obtaining utilise of the > licensed operator, you can concoct an bare documents or overwrite an current one:

>filename.txt

You can also utilise the >> licensed operator, but it appends content to an current documents instead of inventing a neoteric one or running away it bare.

Message Editor

There are innumerable command-jabber text editors, such as nano, vim, or emacs, which allow you to concoct and also edit files. In addition, you can utilise these text editors to supply an bare documents.

For instance, to concoct an bare documents with Vim, dashed:

vim file1.txt

Once you open Vim, you’ll be in command position by default. Press i to switch to insert position, where you can type or edit content.

After going into insert position, press Esc to counterargument to command position. Then, type :wq and also hit Get in to conserve your job and also leave Vim. As well as that’s it—you’ve invented an bare documents obtaining utilise of Vim!

Layer Up

Linux bargains innumerable traits to confiscate care of files, but touch power stands out for its simplicity and also efficiency. You can also spot how to implement different other cure on Linux such as concatenating files, locating files, or retrieving rid of files and also others.

Image credit rating: Unsplash. With one voice alterations and also screenshots by Haroon Javed.

Related Posts