- 10 practical examples of Touch command on Linux
- Linux Touch command
- 1. Create a blank file
- 2. Create multiple files with touch
- 3. Create lots and lots of files
- 4. Avoid creating new files
- 5. Change file access time — ‘a’
- 6. Change the modified time ‘-m’
- 7. Change access and modification time together
- 8. Set a specific access/modify time instead of current time
- 9. Use the timestamp of another file as reference
- 10. Specify datetime as a string
- Conclusion
- 7 thoughts on “ 10 practical examples of Touch command on Linux ”
- touch command in Linux with Examples
- Using touch Command
- touch Command Options
- 8 Practical Examples of Linux “Touch” Command
- Touch Command Options
- 1. How to Create an Empty File
- 2. How to Create Multiple Files
- 3. How to Change File Access and Modification Time
- 4. How to Avoid Creating New File
- 5. How to Change File Modification Time
- 6. Explicitly Set the Access and Modification times
- 7. How to Use the time stamp of another File
- 8. Create a File using a specified time
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- 8 Common Uses of the Linux Touch Command
- 1. Create a single empty file with the touch command
- 2. Create multiple files at once with touch command
- 3. Force avoid creating a new file with touch command
- 4. Change both access and modification times of a file
- 5. Change either access time or modification time
- 6. How to copy access & modification time from one file to another file
- 7. Create a new file with a specified timestamp
- 8. Change timestamp of a file to some other time
- Karim Buzdar
10 practical examples of Touch command on Linux
Linux Touch command
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.
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
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
6. Change the modified time ‘-m’
Use the ‘-m’ option to change the modified time of the file
To change the modify time of multiple files using wildcard
7. Change access and modification time together
Use the a and m option together to modify both access and modification time
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]
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 thoughts on “ 10 practical examples of Touch command on Linux ”
The best way to explain this is to start by explaining what exactly are access, modification, change, creation and birth time
Hi,
I want to create sequence from 1..10 but I don’t want 2 & 5 number files is there any way to create particular sequence files
very good, the article is very clearly and the script very smart.
Actually the
Try this with dash, for an example where it doesn’t work:
bash:/tmp/test$ dash
$ touch
$ ls
Practical is when you combine touch with find command… if you are synchronizing two computers you may touch a file then work lot into of files and when you need to synchronize you use the command:
tar jcvf modified_files.tar.bz2 `find
Then yoy may untar this modified_files into the other computer and BINGO!
I have another., generally used as part of a build process
track releases just by the time portion of the file
touch -d ’02:05′ my_script
marks the file as second version, fifth revision.
touch is a nice command 😀
Pretty sure
# Create 10K files
$ touch <1..10><1..1000>
doesn’t give you a nice tidy range of numbers like you might/would/could want (it’s not multiplication!). It should give you a weird mix where your first number is 11 and your 10000th number will be named 101000 and because there are no leading zeros it wont sort nicely either. Which can be all fine and dandy.
I haven’t bothered to try <1..10000>but maybe that will actually work and give you what’s intended (and faster to type). Still no leading zeros though.
However instead one could do
$ touch <0..9><0..9><0..9><0..9>
because each individual digit is 0 to 9 (it is a regular expression after all). First file is 0000 and 10000th file is 9999.
Or if you’re allergic to counting from 0 and/or do NOT want tidy numbers with leading zeros and for whatever reason <1..10000>doesn’t work (because I didn’t test that one) then one can do
$ for x in `seq 1 10000`; do touch $x; done
(No I didn’t test it to 10000, I did something like “for i in `seq 2 12`; do touch $i.okay; done” instead).
Anyway it all depends/it might not be important at all and one just want 10000 files named whatever 🙂
Источник
touch command in Linux with Examples
The touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file. Basically, there are two different commands to create a file in the Linux system which is as follows:
- cat command: It is used to create the file with content.
- touch command: It is used to create a file without any content. The file created using touch command is empty. This command can be used when the user doesn’t have data to store at the time of file creation.
Using touch Command
Initially, we are in home directory and this can be checked using the pwd command. Checking the existing files using command ls and then long listing command(ll) is used to gather more details about existing files. As you can see in the below figure there is no existing files.
Touch command Syntax to create a new file: You can create a single file at a time using touch command.
Syntax:
The file which is created can be viewed by ls command and to get more details about the file you can use long listing command ll or ls -l command . Here file with name ‘File1‘ is created using touch command.
Touch command to create multiple files: Touch command can be used to create the multiple numbers of files at the same time. These files would be empty while creation.
Syntax:
Multiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here.
touch Command Options
Like all other command Touch command have various options. These options are very useful for various purpose.
touch -a: This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used.
Syntax:
Here touch -a command changes access time of the file named Doc1.
touch -c : This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files.
Syntax:
touch -c-d : This is used to update access and modification time.
Syntax:
touch -m : This is used to change the modification time only. It only updates last modification time.
Syntax:
touch -r : This command is used to use the timestamp of another file. Here Doc2 file is updated with the time stamp of File 1.
Syntax:
touch -t : This is used to create a file using a specified time.
Источник
8 Practical Examples of Linux “Touch” Command
In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.
Linux Touch Command Examples
In this article we will cover some useful practical examples of Linux touch command. The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options.
Touch Command Options
- -a, change the access time only
- -c, if the file does not exist, do not create it
- -d, update the access and modification times
- -m, change the modification time only
- -r, use the access and modification times of file
- -t, creates a file using a specified time
1. How to Create an Empty File
The following touch command creates an empty (zero byte) new file called sheena.
2. How to Create Multiple Files
By using touch command, you can also create more than one single file. For example the following command will create 3 files named, sheena, meena and leena.
3. How to Change File Access and Modification Time
To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name.
The most popular Linux commands such as find command and ls command uses timestamps for listing and finding files.
4. How to Avoid Creating New File
Using -c option with touch command avoids creating new files. For example the following command will not create a file called leena if it does not exists.
5. How to Change File Modification Time
If you like to change the only modification time of a file called leena, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.
6. Explicitly Set the Access and Modification times
You can explicitly set the time using -c and -t option with touch command. The format would be as follows.
For example the following command sets the access and modification date and time to a file leena as 17:30 (17:30 p.m.) December 10 of the current year (2012).
Next verify the access and modification time of file leena, with ls -l command.
7. How to Use the time stamp of another File
The following touch command with -r option, will update the time-stamp of file meena with the time-stamp of leena file. So, both the file holds the same time stamp.
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time, then the format should be.
For example the below command touch command with -t option will gives the tecmint file a time stamp of 18:30:55 p.m. on December 10, 2012.
We’ve almost covered all the options available in the touch command for more options use “man touch“. If we’ve still missed any options and you would like to include in this list, please update us via comment box.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
8 Common Uses of the Linux Touch Command
The Linux touch command can be used for much more than simply creating an empty file on Linux. You can use it to change the timestamp of existing files including their access as well as modification times. This article presents 8 scenarios where you can utilize the touch command through your Linux Terminal.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. Since the touch command is a command line utility, we will be using the Ubuntu Terminal for this article. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
1. Create a single empty file with the touch command
The simplest and the most basic use of the touch command is to create an empty file through the command line. If you are a Terminal-savvy person, you can quickly create a new file in the command line through the following command:
In the following example, I have created an empty file with the name “samplefile” through the touch command. I have then used the ls command to view the presence of the file on my system as the touch command does not prompt if the file has been created or not.
2. Create multiple files at once with touch command
Although the cat command and the standard redirect symbol are also ways to create files through the command line, the touch command takes an edge because you can create multiple files with it at once. You can use the following syntax in order to create multiple files through the touch command:
In the following example I have created three files simultaneously through the touch command and then used the ls command in order to view the presence of those files:
3. Force avoid creating a new file with touch command
At times there is a need to avoid creating a new file if it already does not exist. In that case, you can use the ‘-c’ option with the touch command as follows:
In the following example, I have used the touch command to forcefully avoid the creation of the mentioned new file.
Advertisement
When I use the ls command to list that file, the following output verifies that such a file does not exist in my system.
4. Change both access and modification times of a file
Another use of the touch command is to change both the access time and the modification time of a file.
Let us present an example to show how you can do it. I created a file named “testfile” through the touch command and viewed its statistics through the stat command:
Then I entered the following touch command:
This touch command changed the access and modification time to the time when I ran the touch command again for the “testfile”. You can see the changed access and modification times in the following image:
5. Change either access time or modification time
Instead of changing both the access and modification times, we can choose to change only one of them through the touch command.
In the following example, I created a file by the name of “samplefile” and viewed it statistics through the stat command:
I can change only the access time of this file by using the ‘-a’ option through the touch command on this file:
The output of the stat command now shows that the access time has been changed to the time when I ran the touch command with the ‘-a’ option:
I can change only the modification time of this file by using the ‘-m’ option through the touch command on this file:
The output of the stat command now shows that the modification time has been changed to the time when I ran the touch command with the ‘-m’ option:
6. How to copy access & modification time from one file to another file
Let us suppose we have a file named samplefileA:
And another file named samplefileB:
If you want to change the access & modification time of samplefileA to that of the samplefileB, you can sue the touch command as follows:
The output of the stat command in the above image shows that the samplefileA now has the same access and modify values as that of samplefileB.
7. Create a new file with a specified timestamp
In order to create a new empty file with a specified timestamp instead of the actual time you created it, you can use the following syntax of the touch command:
The following example shows how the stat command on my samplefile shows that its access and modification times are based on the timestamp I provided while creating it through the touch command:
8. Change timestamp of a file to some other time
You can change the timestamp of an existing file to some other time using the following syntax of the touch command:
In the following example, I have changed the timestamp of an existing file through the touch command and then verified the changes through the stat command on that sample file:
Through the basic yet useful scenarios we presented in this article, you can begin to master the touch command and use it for quickly performing some seemingly complex tasks through the Linux command line.
Karim Buzdar
About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn
Источник