Linux touch file size

How can I create a file with a specific size from a command line?

How can I create a file (e.g. using the touch command or another similar way), but with a specific size (e.g. 14 MB)? The use case is to test FTP with various file sizes. If we can create a file with a specific size, it will be useful to verify FTP transfer etc.

7 Answers 7

DESCRIPTION Shrink or extend the size of each FILE to the specified size [. ] -s, --size=SIZE set or adjust the file size by SIZE 

Note: truncate may not be available on your system, e.g. on Mac OS X it’s not installed by default (but you can easily install it, using macports for example). In these cases you may need to use dd or head instead.

Very nice indeed. The touch is not needed though, you can simply run truncate -s 14M filename it will create filename directly.

This seems to create a sparse file, whereas the below answer (that uses «dd») doesn’t. I don’t know if the difference is important to you, but I figured I’d point it out

EDIT: The simplest way is probably the truncate of Ashutosh Vishwa Bandhu’s answer, but as pointed out by @offby1 that creates sparse files which may not be what you want. The solutions below create normal, full files.

The following commands create a 14MB file called foo :

    fallocate (thanks to @Breakthrough who suggested it in the comments and vote up Ahmed Masud’s answer below which also mentions it.)

This command is particularly impressive since it is as fast as truncate (instantaneous) irrespective of the desired file size (unlike the other solutions which will be slow for large files) and yet creates normal files, not sparse ones :

$ truncate -s 14MB foo1.txt $ fallocate -l 14000000 foo2.txt $ ls -ls foo?.txt 0 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:54 foo1.txt 13672 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:55 foo2.txt 
dd if=/dev/urandom of=foo bs=14MB count=1 
head -c 14MB /dev/urandom > foo 
dd if=/dev/zero of=foo.txt bs=14MB count=1 

In all of the above examples, the file will be 14*1000*1000 if you want 14*1024*1024 , replace MB with M . For example:

dd if=/dev/urandom of=foo bs=14M count=1 head -c 14M /dev/zero > foo 

fallocate only deals in bytes, so you’d have to do (14*1024*1024=14680064)

Источник

How To Use the touch Command in Linux

The touch command’s primary function is to modify a timestamp. Commonly, the utility is used for file creation, although this is not its primary function. The terminal program can change the modification and access time for any given file. The touch command creates a file only if the file doesn’t already exist.

Читайте также:  Acronis backup recovery linux

This tutorial explains how to use the touch command with basic and advanced options.

How To Use The touch Command In Linux

  • A system running Linux.
  • Access to the command line/terminal.
  • Basic terminal commands, such as ls .

touch Command Syntax

The fundamental syntax for the touch command is:

The touch utility works without any options or with multiple options for advanced queries. Some options have a long and short format. If an option requires additional information, then the data is mandatory for both long and short forms.

touch Command Options

Below is a reference table for all available touch command options:

Option Description
-a Changes the access time.
-c
—no-create
Avoids creating a new file.
-d=
—date=
Changes a timestamp using a date string.
-f No effect. In older BSD’s the option forces changes.
-h
—no-dereference
Changes a symbolic link’s timestamp.
-m Changes the modification time.
-r=
—reference=
Changes a timestamp to the referenced file’s timestamp.
-t Modifies a timestamp, where the stamp is the date/time format.
—help Opens the help menu.
-v
—version
Prints the program version.

Linux touch Examples

When working with files in Linux, there are three timestamps to be aware of:

1. Access time or atime changes when a command reads the file’s contents, such as grep or cat. The ls -lu command displays the atime for files.

2. Change time or ctime changes when a file’s property changes, such as renaming files, modifying file permission, or moving the file. The ls -lc command shows the ctime for files.

3. Modification time or mtime changes when a file’s contents change. The ls -l command shows the mtime for files.

The examples below are all run from the terminal and demonstrate how to use the Linux touch command with various options and what output to expect.

Create File

The simplest way to use the touch command is without any options:

If a file does not exist, touch creates the file. For example, to create a file called test, run:

touch test terminal output

List directory contents to see the file using the ls command.

If the file already exists, touch changes the timestamp to the current time.

touch change timestamp terminal output

The file contents and permissions stay unchanged.

Create Multiple Files

The touch command can create multiple files as well. To do so, list the filenames separated by spaces:

touch multiple files terminal output

A useful way to apply touch is to create large batches of files. To do so, add curly braces and indicate the first and last element in addition to the filename:

For example, to create ten files with appended numbering, run:

touch ten files terminal output

The command also works with letters. For example:

touch letter files terminal output

Important: The command cannot combine numbers and letters.

Set Specific Timestamp

Use the touch command to set a specific timestamp for an existing file, for example:

The timestamp format follows a specific pattern:

The digits in the square brackets are optional. When using the two-digit year format, setting YY to any number between 0-68 automatically assumes CC is 20, whereas 69-99 assumes CC is 19.

For example, to change the timestamp for a file called test to midnight January 1 st , 1999, run:

touch -t 199901010000 test

touch -t terminal output

Use the —full-time option with ls to see timestamp details.

Set File Timestamp Using Date String

The touch command uses the -d option to set a timestamp using a date string. The syntax is:

The date string is a flexible time format and accepts many different human-readable textual forms. Some examples include:

  • Calendar dates, such as 19 August 2020 .
  • Time of day, such as 9:27pm or 8:02am .
  • Days of the week, such as Sunday , Monday , etc.
  • Relative time, such as 5 years ago , yesterday , next tuesday , etc.

For example, change the timestamp using the -d option to tomorrow :

touch -d terminal output

To see a complete list of the possible string input options, visit the Date input formats GNU documentation.

Change Access Time to Current

Use the -a tag to change a file’s access time. The general syntax is:

For example, to show a file’s access time, run:

Next, change the access time for the file named test with:

Lastly, view the changed time by running:

touch -a terminal output

The access time changes to the current timestamp.

Change Access Time Explicitly

Modify the access time to a specific timestamp by combining the -a and -t options:

Check the access time for files before changing it:

Change the access time for the file test to midnight January 1 st , 1999, by adding the timestamp:

Lastly, check the access time after the change:

touch -at terminal output

After running the command, the access time changes to the value set with the -t tag.

Change Modification Time to Current

The touch command offers an option to change the modification time. The basic syntax is:

As an example, check the file’s mtime before changing the timestamp:

Next, change the modification time for the test file:

Lastly, check the mtime after the change:

touch -m terminal output

The -m option changes the modification time to the current timestamp by default.

Change Modification Time Explicitly

Combine the -m option with -t to explicitly state the modification timestamp. The general syntax is:

Check the file’s mtime before changing it:

Change the modification time to midnight January 1 st , 1999, by running:

Lastly, recheck the modification time:

touch -mt terminal output

Adding the -t option updates the modification time to a specific value.

Change Both Modification and Access Time

The touch utility allows changing the modification and access time with a single command. To do so, run:

Before changing the atime and mtime, check it with:

Next, change both times for the test file to the current timestamp:

Check the atime and mtime after the change:

touch -am terminal output

The combined options change both times in one go to the current time. Combine further with the -t tag to state an explicit timestamp.

Avoid Creating a New File

By default, touch generates a new file if it doesn’t exist. However, certain situations require overriding this functionality. Add the -c option to avoid creating a new file when invoking the touch command:

For example, try to run touch with the -c option with a non-existent file:

List directory contents to confirm the file is not there:

touch -c terminal output

On the other hand, if the file does exist, the touch command performs supplied operations on the existing file as usual.

Set Timestamp Using a Reference File

The touch command offers a useful option to change a file’s timestamp based on another file’s timestamp.

To perform such a change, run:

For example, create a new file and reference the timestamp of an existing test file:

Check the timestamp for both files with:

touch -r terminal output

The new_test file inherits the timestamp from the test file.

The touch command allows changing the timestamp for symbolic links without changing the referenced file’s timestamp. Use the -h option to modify the time for a symbolic link:

For example, check the time for an existing symbolic link before any changes:

Change the timestamp for the symbolic link to the current time:

Lastly, recheck the timestamp to confirm the change:

touch -h terminal output

Without the -h option, the touch command only changes the test file’s timestamp.

touch symbolic link unchanged

The symbolic link’s timestamp stays unchanged in this case.

The touch utility is one of the primary terminal programs when working with files in Linux. The tutorial outlined some typical use-cases for the touch command. Next, check out our Linux commands cheat sheet, which features the touch command.

Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.

It is always dangerous to run a Linux terminal command when you aren’t sure what it does. This article lists 14 Linux commands that can have adverse effects on your data or system.

Setting file and directory permission properly is important in multi-user systems such as Linux. You can set permission recursively using the chmod or find.

The dd is a command-line utility is used to convert and copy files on Unix and Unix-like operating systems. Learn how to use the dd command to.

Источник

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