Change file size linux

How to cut a file to a given size under Linux?

I want to shrink a file’s size by brute-force, that is, I don’t care about the rest, I just want to cut the file, say by half, and discard the rest. The first thing that comes to mind is Perl’s truncate. I’m following the example on that page and did the exactly the same thing:

seq 9 > test.txt ls -l test.txt perl -we 'open( FILE, "< ./test.txt" ) && truncate( FILE, 8 ) && close(FILE);' 
$ ls -lgG test.txt -rw-rw---- 1 18 2013-08-08 09:49 test.txt 

5 Answers 5

SIZE can be specified as bytes, KB, K, MB, M, etc. I assume you can calculate the desired size by hand; if not, you could probably use the stat command to get information about the file's current size.

Note that this chops off from the bottom. i.e. if used on log files you will loose the most recent log lines.

opens the file for reading. However, to truncate the file you need to modify it, so a read-only file handle isn't going to work. You need to use the "modify" mode ( "+>" ).

As a side issue, it always amazes me when people let system calls fail silently and then ask what went wrong. An essential part of diagnosing a problem is looking at the error message produced; even if you don't understand it, it makes life much easier for those you ask for help.

The following would have been somewhat more helpful:

although admittedly that would only have reported "invalid argument". Still, that is useful information and might well have led you to the conclusion that the open mode was wrong (as it did for me).

Источник

How to shrink or extend the size of a file in Linux?

The truncate command is used to shrink or extend the size of a file to the given size. The truncate command cannot remove the file whereas removes the contents of the file and set size of file is zero byte. The meaning of truncate is reducing. While reducing the size of the file is the specified size is less than actual size then extra data will be lost.

Syntax

The general syntax of the truncate command is as follows.

Brief description of options available in the truncate command.

Sr.No. Option & Description
1 -c, --no-create
Do not create any files
2 -o, --io-blocks
Manage size as number of IO blocks rather than bytes.
3 -r, --reference=RFILE
Set size of file as reference file.
4 -s, --size=SIZE
Set size of the file to SIZE bytes.
5 --help
Display this help and exit.
6 --version
Output version information and exit
Читайте также:  Arduino nano ch340 linux

To set the size of the file from actual size to zero and remove all contents of the file, we use the truncate command as shown in below.

After executing this command all contents of the file will be removed.

If we are using truncate command, and file is not available then the truncate create a new file with specified size and name in the Linux system using terminal.

Here, a file.txt file is not available in the current directory but after execution of this command ‘file.txt’ will be created in current working directory.

To pervert creation of new file, we use -c or –no-create option with the truncate command.

If file is not available in current directory then new file will not be create using -c or --no -create option with the truncate command in the Linux system.

$ truncate -c -s 200K file.txt

Example

To set the size of the file actual size to specified size, we use the truncate command in the Linux system using terminal as shown in below.

If the specified size is less than actual size then extra data will be lost.

Источник

truncate (1) - Linux Manuals

truncate: shrink or extend the size of a file to the specified size

Command to display truncate manual in Linux: $ man 1 truncate

NAME

truncate - shrink or extend the size of a file to the specified size

SYNOPSIS

DESCRIPTION

Shrink or extend the size of each FILE to the specified size

A FILE argument that does not exist is created.

If a FILE is larger than the specified size, the extra data is lost. If a FILE is shorter, it is extended and the sparse extended part (hole) reads as zero bytes.

Mandatory arguments to long options are mandatory for short options too.

-c, --no-create do not create any files -o, --io-blocks treat SIZE as number of IO blocks instead of bytes -r, --reference=,RFILE/ base size on RFILE -s, --size=,SIZE/ set or adjust the file size by SIZE bytes --help display this help and exit --version output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024). Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB. (powers of 1000). Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '' at least, '/' round down to multiple of, '%' round up to multiple of.

AUTHOR

REPORTING BUGS

Copyright © 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

Full documentation
or available locally via: info '(coreutils) truncate invocation'

  • truncate (2) - truncate a file to a specified length
  • truncate (3p) - truncate a file to a specified length
  • truncate (7) - empty a table or set of tables
  • true (1) - do nothing, successfully
  • trust (1) - Tool for operating on the trust policy store
  • tr (1) - translate or delete characters
  • bdftruncate (1) - generate truncated BDF font from ISO 10646-1-encoded BDF font

Index

SEE ALSO

Full documentation
or available locally via: info '(coreutils) truncate invocation'

  • truncate (2) - truncate a file to a specified length
  • truncate (3p) - truncate a file to a specified length
  • truncate (7) - empty a table or set of tables
  • true (1) - do nothing, successfully
  • trust (1) - Tool for operating on the trust policy store
  • tr (1) - translate or delete characters
  • bdftruncate (1) - generate truncated BDF font from ISO 10646-1-encoded BDF font
Читайте также:  How to encrypt file in linux

Linux Manuals Copyright Respective Owners. Site Copyright © SysTutorials. All Rights Reserved. Terms and Privacy

Источник

How To Use “Truncate” Command In Linux?

Sometimes we need to remove the content of a file without deleting the file; for that Linux operating system offers a command called “truncate”. It is used to extend or reduce the file size. Truncating a file is much quicker and simpler without modifying the permissions and ownership of the file.

The truncated size depends on the original size of the file; the extra data will be lost if the file size is greater than the specified size.

Let’s start with different examples to see how we can truncate the file size.

Installing Coreutils Packages

The “truncate” command comes with most Linux distribution. It can also be installed, if not present, using the command given below:

Use the “grep” command to list the detail of packages:

How to Use the “truncate” Command?

The “>” shell redirection operator is the most popular and simplest way to truncate files.

The syntax for truncating files with redirection is:

The “:” colon denotes true and has no output and the redirection operator “>” redirect the output to a specific file.

The file I am truncating is “test.sh”:

Another way to truncate file is:

It is removing the content of “test.sh” file.

Clear the Content of File

Use the “-s” option to remove the content of the files. This is a preferable way to manually delete a file. The truncate command effectively eliminates all the contents of a file. It does not delete the file itelf, but leaves it as a zero-byte file on the disk.

Let’s use truncate to clear file.txt to 0 bytes:

The file permissions and ownership will be preserved if you use the truncate command.

Use the “ls -lh” command to confirm the size:

Truncating a File to a Specific Size

To confirm the file permission and size of the file, use:

Let’s truncate the file to 100 bytes size:

To truncate a file size to 300K:

Type below mentioned command to check the size:

Extending the File Size

You can increase the file size by using the “+” with “-s” option. The file is currently 300k in size, as shown in the image below:

I’d like to increase the size of the file from 300k to 600k bytes:

The file size has been extended from 300k to 600k. Check the size:

Reducing the File Size

Let’s assume you have a 600k file and want to reduce its size to 270k, use “-s” option and “” with the size figured:

The current size of the file is 330k.

Getting Help

To get a help message, use:

Checking Version

To check the version of the truncate command, use:

Conclusion:

Truncate is a very useful command for removing the content of a file while not deleting the file. You can also change the size of the file to the size you want it to be. We have learned how to truncate the content of a file, as well as how to shrink or extend the files in this article.

Читайте также:  Postgresql linux restore backup

About the author

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related
articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.

Источник

What is the fastest way to increase the size of a file in linux on a ext4 filesystem from a C executable without creating holes in the file?

The fastest way to increase the file size that I know of would be to ftruncate() or lseek() to the desired size and write a single byte. That doesn't fit my needs in this case because the resulting hole in the file doesn't reserve space in the file system. Is the best alternative to use calloc() and write()?

int increase_file_size_(int fd, int pages) < int pagesize = 4096; void* data = calloc(pagesize, 1); for(int i = 0; i < pages; ++i) < // In a real world program this would handle partial writes and interruptions if (write(fd, data, pagesize) != pagesize) < return -1; >return 0; > 

Perhaps this can be made even faster by using writev. The next version should be faster since calloc has to zero initialize less memory, and more of the data fits in the CPU cache.

int increase_file_size_(int fd, int pages) < int pagesize = 4096/16; void* data = calloc(pagesize, 1); struct iovec iov[16]; for(int i = 0; i < 16; ++i) < iov[i].iov_base = data; iov[i].iov_len = pagesize ; >for(int i = 0; i < pages; ++i) < // In a real world program this would handle partial writes and interruptions if (writev(fd, data, pagesize) != pagesize * 16) < return -1; >return 0; > 

I can experiment and see which of these approaches and which page size is the faster. However, is there another approach that is considered the normal best practice for extending a file? Are there other approaches that I should performance test? Thank you.

@CareyGregory There are 2 reasons for callocing 1) Becasue the executable is compiled with -Wframe-larger-than=N and where N is smaller than the page size. 2) it is can be faster than bzeroing() data on the stack.

1 Answer 1

Take a look at the posix_fallocate() function: it reserves space for a file without necessarily writing any data to occupy that space. The allocated space works sort of like a sparse file in that you can read from it even though you haven't explicitly written anything to it, but unlike a sparse file, it actually reduces the amount of free space in the filesystem. You're assured that you can write to that region of the file later without running out of space.

Note that posix_fallocate() doesn't seem to make any guarantees about the content of the allocated space if you read it before writing to it. I think the Linux implementation will return all zero bytes, similar to a sparse file, but you probably shouldn't rely on that. Treat it as garbage before you write something real to it.

Also note that not all filesystem drivers support the preallocation feature that posix_fallocate() takes advantage of, and I think it'll fall back on actually writing data to the file (the normal way) if preallocation isn't supported. Typical Linux filesystems like ext4 and XFS are OK, but if you try it on something like FAT or NTFS, your program will probably end up blocking on I/O for awhile.

Источник

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