Linux touch all file

10 practical examples of Touch command on Linux

The touch command can be used to modify the access/modification timestamps of files. It is more often used to actually just create an empty file quickly.

This post shows some very simple and quick examples of using the touch command to modify timestamps or create files.

1. Create a blank file

To simply create a blank file with touch command, use the syntax below.

If the file already exists, its access time will be updated.

2. Create multiple files with touch

To create multiple files, specify their names together separated by a space.

$ touch abc.txt cde.txt xyz.txt

3. Create lots and lots of files

If for some reason you wish to create lots of files, then commands like these would be very helpful

# Create files with names A to Z $ touch # Create files with names 1 to 20 $ touch # Create files with extension $ touch .txt # Create 10K files $ touch

And then use the ls command to see what all has been created.

4. Avoid creating new files

If you want to just update the access time of existing file, without creating it, use the ‘-c’ option. If the file exists, touch will update the access time, else will do nothing

5. Change file access time — ‘a’

To change only access time of a file use the ‘-a’ option with the file name.

To check the access time use the stat command

$ stat a.txt File: ‘a.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 5904730 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/enlightened) Gid: ( 1000/enlightened) Access: 2016-03-10 15:04:24.281533071 +0530 Modify: 2016-03-10 15:00:16.117864128 +0530 Change: 2016-03-10 15:04:24.281533071 +0530

6. Change the modified time ‘-m’

Use the ‘-m’ option to change the modified time of the file

$ touch -m a.txt [term] Then check the file statistics with the stat command - [term] $ stat a.txt File: ‘a.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 5904730 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/enlightened) Gid: ( 1000/enlightened) Access: 2016-03-10 15:04:24.281533071 +0530 Modify: 2016-03-10 15:05:03.409475551 +0530 Change: 2016-03-10 15:05:03.409475551 +0530

To change the modify time of multiple files using wildcard

Читайте также:  Common command line commands linux

7. Change access and modification time together

Use the a and m option together to modify both access and modification time

$ stat a.txt File: ‘a.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 5904730 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/enlightened) Gid: ( 1000/enlightened) Access: 2016-03-10 15:07:39.633235119 +0530 Modify: 2016-03-10 15:07:39.633235119 +0530 Change: 2016-03-10 15:07:39.633235119 +0530

8. Set a specific access/modify time instead of current time

To set the access/modify time to a specific datetime use the t option and specify the datetime in format
[[CC]YY]MMDDhhmm[.ss]

$ touch -c -t 1603051015 a.txt or $ touch -c -t 201603051015 a.txt

Note — If you omit the c option, a new file will be created with the given datetime if it does not exist.

9. Use the timestamp of another file as reference

The above command will set the access/modify time of abc.txt to that of ref.txt

10. Specify datetime as a string

Apart from the t option, there is another option ‘-d’ which accepts datetime in general human readable formats.

The following example provides the date only. The time is automatically set to 00:00

Or just provide the time, and the current date will be selected —

Conclusion

Those were some of the basic examples of touch command. To learn more, check the man page with «man touch» command.

Or read the manual pages online here:

If you have any questions or feedback, let us know in the comments below.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

7 Comments

  1. Chancletto March 6, 2021 at 9:09 am The best way to explain this is to start by explaining what exactly are access, modification, change, creation and birth time

Источник

How to touch all files in subtree recursively Linux?

Touch is a command in Linux that is used to create and modify the timestamps of a file. Additionally, it can create files with no content and a timestamp specified by the user. Often, we have a directory in Linux with multiple subdirectories and files, and we wish to change the timestamps of each subdirectory and file.

In this article, we will look at how to touch all such files and subdirectories recursively, along with a comprehensive breakdown of the commands that are used for this purpose.

Читайте также:  Linux проверить версию bios

What is the touch command, and how to use it?

The purpose of the touch command in Linux is twofold; it can be used to create empty files with a timestamp specified by us, and it can be used to modify and change the timestamps of already existing files. It is a very useful command since it can change a wide range of timestamps, including but not limited to access (the last time the file was accessed) and modified (the last time the file was modified) timestamps. We will list some of the common uses of the touch command and their formats.

  • touch filename: creates an empty file with the name specified by the ‘filename’ argument.
  • touch -a: used to change the last access timestamp of a file.
  • touch -c: checks if a file with the specified name exists or not. If such a file does not exist, it avoids creating it.
  • touch -c -d: used to update the access and modification time.
  • touch -m: used to update the last modification time of the specified file.
  • touch -r: used to store the timestamp of another file as a reference.
  • touch -t: used to create a file with a specified timestamp.

Now that we have got our readers acquainted with the fundamental uses of the touch command, we will move forward to learning how we can use it to touch all files and subdirectories in a subtree recursively.

Step 1 – Open a terminal

We will be using the command-line interface to touch all files in a subtree recursively. To begin, we will open a new command terminal. You can open a terminal window either by accessing it through the application menu or by pressing the keyboard shortcut Ctrl + Alt + T.

Step 2 – Execute the touch command

For demonstration purposes, we will be taking an example directory to freely experiment with the touch command. In this case, our directory is located in ~/example. Obviously, you should follow along with the directory of your choice to ensure maximum learning. You should change it according to the directory you want to touch. After setting the correct directory, all that is left to do is to execute the following command to touch all files and subdirectories in the specified folder.

As we saw earlier, there are many variants of the touch command that one can execute to achieve different purposes. One such case is when the user wants to print all the directories in question. This, too, can indeed be done through the touch command. All that needs to be done is a little bit of modification in the syntax and the addition of “-print” to the command. Run the command below to test out the printing feature (It is once again advised that you make sure that the address is set to your desired directory that needs to be touched).

Читайте также:  Линукс увеличить количество соединений

As you can see from the picture above, the directories are not only just touched but also printed as we ran the previous command.

Breakdown of the commands

Now that we have covered the main part of the process, those that are curious about how the previous commands worked can stick around to learn more details about them. This section will break down each part of the commands we executed one by one and go through their functionalities.

First of all, let us take a look at the find command.

The find command is used to look for files and directories in a particular directory. Some possible methods of its usage are given below.

  • find -name: used to search for a particular filename in a directory.
  • find -iname: used to conduct a case insensitive name search.
  • find -f: used to find a regular file.
  • find -d: used to find directories.
  • find -l: used to find symbolic links.
  • find -c: used to find character devices.
  • find -b: used to find block devices.
  • find -s: used to find sockets

Now, you should understand what the find command does and how to use it. Essentially, it lists all the files and directories at an address given by the user.

Moving forward, we specify the directory where we want to search for files and directories.

-print tells the terminal to show the files and directories that the command finds.

-exec touch executes touch command on each of the found files and directories.

<> makes sure that the names of the files and the directories that they find command lists are being forwarded to touch command as arguments.

\; is used to escape from the exec command.

With that being said, we have covered the purpose behind every part of this command.

Conclusion

This article looked at how to touch all files in a subtree recursively using find and touch commands. Moreover, an overview of the touch and find command is presented, along with a breakdown of their combination used to list and touch files in a subtree recursively.

Источник

Оцените статью
Adblock
detector