Linux File Creating/Updating

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the wasteland of a digital desert, a land where countless files and directories lay buried, awaiting the touch of a skilled explorer to bring them into existence or update them. Here lies a dusty expanse of forgotten data, and it will be your mission to navigate it adeptly.

You are a treasure hunter of the sands, known far and wide for your ability to conjure and modify files with a mere flick of your fingers on a keyboard. Your tools are the commands of Linux, and your latest challenge is to master the touch command to create and update files, setting the date and time with precision.

In the whirlwind of a storm that is file management, your target is to learn the nuances of creating and updating files swiftly, without getting lost in the howling winds of complexity. Does this sound like a grand quest? Suit up, brave explorer. The desert of data awaits you, ready to reveal its secrets.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/touch -.-> lab-271409{{"`Linux File Creating/Updating`"}} end

Mastering Basic File Creation with touch

In this step, we will learn how to create a new file using the touch command. This simple command can conjure an empty file out of the thin desert air, which is the first step in organizing the data you may find during your excavation.

Navigate to your working directory /home/labex/project and execute the following command to create an empty file named ancient_artifact.txt.

touch ancient_artifact.txt

The touch command updates the access and modification times of the specified file to the current time. If the file doesn't exist, touch creates it without any content.

After running the command, check if the file has been created with this ls command:

ls -l ancient_artifact.txt

Your terminal should show the details of the new file that was created.

Setting Custom Timestamps with touch

Sometimes, a desert explorer wants to backdate a treasure or update its last accessed record to a specific time. The touch command can be invoked with options to set the dates according to the explorer's choice.

In this step, you'll update the modification and access time of the ancient_artifact.txt file. Let's set its timestamp to 10 days ago.

First, calculate the date of 10 days ago. Here is how to do that:

date --date="10 days ago" +%Y%m%d%H%M

This will give you the date in the format YYYYMMDDHHMM which is needed for the touch command.

Now let's update the file timestamp:

touch -t $(date --date="10 days ago" +%Y%m%d%H%M) ancient_artifact.txt

To confirm the time has been set correctly, list the file details:

ls -l ancient_artifact.txt

The output should show the modified date and time as you specified.

Summary

In this lab, we delved into the powerful and versatile touch command, starting with creating a simple file and advancing to setting custom timestamps. Through practical exploration and hands-on exercises, you've forged your skills and learned to command the winds of the Linux file system. Whether you're a beginner or an experienced user, these basic but intricate tasks reinforce the foundational activities in file management.

This journey across the digital sands may have seemed daunting at first, but you, a true desert treasure hunter, have proven your prowess. With these new skills, may your future endeavors in the Linux environment be as successful as this expedition. Take pride in your achievements – each command learned is a treasure unearthed, each skill acquired is a relic restored.

Other Linux Tutorials you may like