Linux how to file size

How to find large file size on linux

By mkyong | Last updated: August 30, 2012

Viewed: 80,284 (+704 pv/w)

Often time, you may need to know which file contains large file size, and delete it to save space. Here’s a code pattern to show you how to find large file size on Linux :

1. File size >= 100MB

Find all files that have a size >= 100MB, from root folder and its sub-directories.

2. File size >= 50MB

Find all files that have a size >= 50MB, from folder ‘/Users/mkyong’ and its sub-directories.

References

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

When you use find with size option. I guess you are assuming that file size has to be more than +100000k which will is not the solution to the topic you are covering
Topic is: “How to find large file size on linux (Solution)”

So this will give only the file larger than size specified.
find / -type f -size +100000k …

The solution that might get the results.
find . -type f -exec du <> \; | sort -rn | head -10

so the command says:- find all files and run then run du (estimate file space usage) then sort as per the size and get me 10 largest. I might like to know largest file even if it few kb’s …

The only problem I am working on is this will take long time to get me results. Any ideas on how to make this command faster ??

Источник

How to Get the Size of a Directory in Linux

Home » SysAdmin » How to Get the Size of a Directory in Linux

Many users run Linux from the command line. However, the command line — sometimes known as the terminal — doesn’t have an intuitive interface for checking disk space in Linux.

This guide shows you how to find the size of a specific directory in Linux from the command line.

  • A system running Linux
  • A command line / terminal window (available by clicking Search, then typing terminal)
  • A user account with sudo or root privileges

Note: In Linux, a directory is the equivalent of a folder in Windows. A directory may have directories inside (called subdirectories), or it may only contain files.

Option 1: Display the Size of a Directory Using the du Command

The du command stands for disk usage. This command is included by default in most Linux distributions.

You can display the size of your current directory by typing du in the command line:

The system should display a list of the contents of your home directory, with a number to the left. That number is the size of the object in kilobytes.

Читайте также:  Assassins creed odyssey не сохраняется windows 10

You can add the -h option to make the output more readable:

Each entry will start with a number and a letter. The number is the amount of space used, and the letter (usually K, M, or G) indicates Kilobytes, Megabytes, or Gigabytes. For example:

To find the size of a specific directory different from your current working directory. The du command allows you to specify a directory to examine:

This displays the size of the contents of the /var directory. You may see some entries with an error, as in the image below.

This happens when your user account does not have permission to access a particular directory. Use the sudo or su command to get access privileges:

Note: Some versions of Linux don’t enable sudo by default. You can use the su command to switch to the root user account instead.

To display total disk usage of a particular directory, use the -c command:

Options can be combined. If you wanted to repeat the previous command in human-readable format, enter the following:

You can limit the scan to a certain level of subdirectory by using the max-depth option. For example, to scan only the size of the top directory, use —max-depth=0 :

If you wanted to list only the top directory and the first layer of subdirectories, change —max-depth=1 :

If you run into trouble or want to explore more options for the du command, enter the following command to display the help file:

Option 2: Get Size of Directory in Linux Using tree Command

By default, the tree command is not included in some versions of Linux. To install it, enter the following:

  • For Debian / Ubuntu
  • For CentOS / RedHat

The tree command displays a visual representation of your directories. It uses lines to indicate which subdirectories belong where, and it uses colors to indicate directories and files.

tree can also be used with options. To display a human-readable size of the current directory’s subdirectories, enter the following:

Like the du command, tree can target a specific directory:

This command takes a few moments since the /var directory has many entries.

The tree command also has a help file, which you can access by entering:

Option 3: Find the Size of a Linux Directory Using ncdu Command

The ncdu tool stands for NCurses Disk Usage. Like the tree command, it is not installed by default on some versions of Linux. To install it, enter the following:

  • For Debian / Ubuntu
  • For CentOS / RedHat

The ncdu utility is an interactive display of your disk usage. For example, enter the following:

In the upper left corner, it displays the current directory being scanned. A column on the left displays the numerical size, a graph of #- signs to indicate the relative size, and the file or directory.

Use the up and down arrows to select different lines. The right arrow will browse into a directory, and the left arrow will take you back.

ncdu can be used to target a specific directory, for example:

For help, press the ? key inside the ncdu interface. To quit, press the letter q .

Note: Learn how to move directories in Linux using the GUI or system commands.

You now have three different options to find the size of a directory in Linux operating systems.

If you want to learn more about directories in Linux, read our article how to rename directories in Linux.

Источник

How To Create Files Of A Certain Size In Linux

A while ago, I setup a local file server using PSiTransfer. While using my file server, I wanted to test the upload limit, maximum upload size, and download speed of the files/folders. For that purpose, I needed different size files. I have various size files in my hard drive. But, I want the file to be exactly 100 MB in size. After a bit of web search, I found out how to create files of a certain size in Linux and Unix-like systems.

Create Files Of A Certain Size In Linux

There are few ways to create files with given size. I will show them all with practical examples.

Читайте также:  Что такое engine exe windows 10

All commands mentioned in this guide are part of GNU coreutils, so you don’t have to install them. These commands comes pre-installed by default.

1. Create files of a certain size using truncate command

To create a specific size file, for example 5 MB, using truncate command, run:

The above command will create a file called ostechnix.txt with size exactly 5MB .

For more details about this command, refer truncate man pages.

2. Create files of a certain size using fallocate command

The another command to create a particular size file is fallocate . Please note that you can only specify file sizes in bytes using fallocate command. To calculate the size for a specific file, for example 5MB , you need to do — 510241024=5242880 . Clear? Good!

Now let us create a file of size 5MB using command:

As Luc Van Rompaey suggested in the comment section, with the bash shell, you can do inline arithmetic, so you won’t have to calculate how many bytes go into 5 MiB beforehand.

So, we can use this as shown below:

For more details about this command, I suggest you to go through the fallocate man pages.

3. Create files of a certain size using head command

We use head command to output the first part of files, right? Well, we can use this command to create a file of certain size too.

To create a file with 5 MB in size using head command, run:

The above command will create 5MB size file filled with random data. You can also create the file with 0s as shown below.

Refer man pages for further details about head command.

4. Create files of a certain size using dd command

We already knew we can convert and copy a file using dd command. We also use dd command to create a bootable disk. However, we can use this command to create files of certain size as well.

To create a file with size 5MB, run:

Sample output:

The command will create ostechnix.txt file of size 5MB filed with some random data.

To create a file filled with 0s , you can use:

As usual, for details about this command, refer the dd command man pages.

And, that’s all. You know now how to create a file with certain size. As you can see in the above examples, creating files of certain size is no big deal. Hope this helps.

Источник

How To Find Files Bigger Or Smaller Than X Size In Linux

Let us say you want to find files which are less than or greater than a certain size in your system. How would you do that? Manually check each and every file’s size? No, it is time consuming task. Besides, a good system admin won’t do it. There is always an easiest and fastest way to do things in Linux. This brief tutorial covers how to find files bigger or smaller than X size in Linux and Unix operating systems.

Find files bigger or smaller than X size

Using find command, we can also easily find files bigger or smaller than given size.

For instance, to find files that are bigger than 4GB in a directory, just enter:

Sample output from my system:

As you can see, I have some files with size bigger than 4GiB. Here, the dot (.) indicates the current directory.

To search for files bigger than 4 GiB in the entire filesystem, run:

To know files bigger than X size in a specific directory, replace the dot (.) in the above command with the directory path like below.

The above command find files bigger than 4GiB in Downloads directory.

Similarly, to find the files which are smaller than X size, for example 4GiB, use the following command:

You can use size switch for other formats, such as

  • ‘c’ for bytes
  • ‘w’ for two-byte words
  • ‘k’ for Kilobytes
  • ‘M’ for Megabytes
  • ‘G’ for Gigabytes

For example, to find files which are bigger than 4MB, use the following command:

To find files smaller than 4MB, use this command:

You might wonder how to find files between a certain size. For instance, you can find files between 30MB and 40MB using the following command:

Читайте также:  Lazarus компиляция под windows

To find files of exact size, for example 30MB, run:

For more details, refer man pages.

Update:

As one of the reader mentioned in the comment section below, the find command can also display a long listing of all the files it finds by using the -exec switch. The command below will find all of the files between 30M and 40M, and display a long listing of each file.

Do you know any other useful and easiest way to find files which are smaller or bigger than a particular size? Please feel free to comment them in comment section below.

Источник

6 Methods to create files of specific size in Linux

If you want to create a specific size of a file in Linux to test your upload or download speed on your file server. Or you want to create a certain size of a file in Linux to extend your swap partition then you can use one of the below method to create it.

Navigate to the following URL, if you want to know about multiple ways to create a file in Linux.

As we already know few commands to perform this but i have included all the possible options in this article.

You can check all the commands which is available in this article and choose the preferred one for you.

I’m going to create a 10M file using all commands to test this.

It can be done using the following 6 methods.

  • fallocate: fallocate is used to preallocate or deallocate space to a file.
  • truncate: truncate is used to shrink or extend the size of a file to the specified size.
  • dd: Copy a file, converting and formatting according to the operands.
  • head: head is used to print the first part of files.
  • xfs_mkfile: xfs_mkfile command allow us to create a specific size of a file in Linux.
  • perl: Perl is a programming language specially designed for text editing.

How To Create A File In Specific Size In Linux Using fallocate Command?

fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For filesystems which support the fallocate system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks. This is much faster than creating a file by filling it with zeroes.

Use the ls command to check the given file size.

How To Create A File In Specific Size In Linux Using truncate Command?

truncate is used to shrink or extend the size of a file to the specified size.

Use the ls command to check the given file size.

How To Create A File In Specific Size In Linux Using dd Command?

The dd command stands for data duplicator. It is used for copying and converting data (from standard input to standard output, by default).

Also, dd command allow us to create a Bootable USB Disk in Linux.

Use the ls command to check the given file size.

How To Create A File In Specific Size In Linux Using head Command?

The head command reads the first few lines of any text given to it as an input and writes them to standard output (which, by default, is the display screen).

Use the ls command to check the given file size.

How To Create A File In Specific Size In Linux Using xfs_mkfile Command?

xfs_mkfile creates one or more files. The file is padded with zeroes by default. The default size is in bytes, but it can be flagged as kilobytes, blocks, megabytes, or gigabytes with the k, b, m or g suffixes, respectively.

Use the ls command to check the given file size.

How To Create A File In Specific Size In Linux Using perl Command?

Perl stands in for “Practical Extraction and Reporting Language”. Perl is a programming language specially designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, web development, etc.

Use the ls command to check the given file size.

Источник

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