- How to check the file size in Linux/Unix bash shell scripting
- How to check file size in unix using wc command
- Get the size of a file in a bash script using stat command
- How to Find Out Top Directories and Files (Disk Space) in Linux
- How to Find Biggest Files and Directories in Linux
- Find Largest Directories in Linux
- Find Out Top File Sizes Only
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- HowTo: Linux / Unix See File Size Command
- Examples
- Summing up
- How to Display File Size in Human Readable Format (KB, MB, GB) in Linux Terminal
- How To Find Files Bigger Or Smaller Than X Size In Linux
- Find files bigger or smaller than X size
How to check the file size in Linux/Unix bash shell scripting
How to check file size in unix using wc command
The wc command shows the number of lines, words, and bytes contained in file. The syntax is as follows to get the file size:
wc -c /path/to/file
wc -c /etc/passwd
Sample outputs:
You can easily extract the first field either using the cut or awk command:
wc -c /etc/passwd | awk ‘
Sample outputs:
OR assign this size to a bash variable:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Get the size of a file in a bash script using stat command
The stat command shows information about the file. The syntax is as follows to get the file size on GNU/Linux stat:
stat -c %s «/etc/passwd»
OR
stat —format=%s «/etc/passwd»
To assign this size to a bash variable:
The syntax is as follows to get the file size on BSD/MacOS stat:
stat -f %z «/etc/passwd»
Please note that if the file is symlink you will get size of that link only with the stat command.
Источник
How to Find Out Top Directories and Files (Disk Space) in Linux
As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find unnecessary junk and free up them from your hard disk.
This brief tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and find command. If you want to learn more about these two commands, then head over to the following articles.
How to Find Biggest Files and Directories in Linux
Run the following command to find out top biggest directories under /home partition.
Find Largest Directories in Linux
The above command displays the biggest 5 directories of my /home partition.
Find Largest Directories in Linux
If you want to display the biggest directories in the current working directory, run:
Find Biggest Directories Only
Let us break down the command and see what says each parameter.
- du command: Estimate file space usage.
- a : Displays all files and folders.
- sort command : Sort lines of text files.
- -n : Compare according to string numerical value.
- -r : Reverse the result of comparisons.
- head : Output the first part of files.
- -n : Print the first ‘n’ lines. (In our case, We displayed the first 5 lines).
Some of you would like to display the above result in human-readable format. i.e you might want to display the largest files in KB, MB, or GB.
Find Top Directories Sizes in Linux
The above command will show the top directories, which are eating up more disk space. If you feel that some directories are not important, you can simply delete a few sub-directories or delete the entire folder to free up some space.
To display the largest folders/files including the sub-directories, run:
Find Largest Folder and Subdirectories
Find out the meaning of each option using in above command:
- du command: Estimate file space usage.
- -h : Print sizes in human-readable format (e.g., 10MB).
- -S : Do not include the size of subdirectories.
- -s : Display only a total for each argument.
- sort command : sort lines of text files.
- -r : Reverse the result of comparisons.
- -h : Compare human readable numbers (e.g., 2K, 1G).
- head : Output the first part of files.
Find Out Top File Sizes Only
If you want to display the biggest file sizes only, then run the following command:
Find Top File Sizes in Linux
To find the largest files in a particular location, just include the path beside the find command:
Find Top File Size in Specific Location
The above command will display the largest file from /home/tecmint/Downloads directory.
That’s all for now. Finding the biggest files and folders is no big deal. Even a novice administrator can easily find them. If you find this tutorial useful, please share it on your social networks and support TecMint.
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.
Источник
HowTo: Linux / Unix See File Size Command
a] ls command – list directory contents.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | ls/du/stat |
Est. reading time | Less than a one minute |
b] du command – estimate file space usage.
c] stat command – display file or file system status.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Examples
To determine the size of a file called /bin/grep, enter:
In the above output example, the 175488 is the size of the file. For a more user friendly output, pass the -h option to the ls command:
Here is what we see:
In the above output example, the 172K is the size of the file. The du command provides the same output in a more user friendly way and it hides all other details too:
du -h /bin/grep
Now du command reporting file size on screen:
Finally, stat command also provide file size:
stat /bin/grep
Linux stat command outputs:
The following commands are executed on Apple OS X Unix operating systems to file out the file size of the /usr/bin/vim binary file:
$ ls -l /usr/bin/vim
$ ls -lh /usr/bin/vim
$ stat -x /usr/bin/vim
Fig.01: Finding out file size using various command line options on OS X Unix OS
Summing up
You learned different Linux and Unix commands to display file size using the command-line option. You need to type these commands using the terminal application. One can execute them over remote ssh session too, using the ssh command:
Finding file size using ssh command over the remote session (cloud server)
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Display File Size in Human Readable Format (KB, MB, GB) in Linux Terminal
You probably already know that you can use ls command with long listing option -l to show file size in Linux.
But unfortunately, the long listing shows the file size in blocks and that’s not of much use to us humans.
Good thing is that you can combine the option -l with -h to show the file size in human readable format.
As you can see, it is better to display file size in human-readable format.
As you can see, file sizes are now displayed in K (for KB), M for (MB). If the file size is in Bytes, it is not displayed with any suffix. In the above example, char.sh is 140 Bytes in size.
Did you notice the size of new_dir directory? It is 4 KB. If you use ls -lh command on directories, it always shows the size of directory as 4.0 K.
By default, the block size in most Linux system is 4096 Bytes or 4 KB. A directory in Linux is simply a file with the information about the memory location of all the files in it.
You can force ls command to display file size in MB with the —block-size flag.
The problem with this approach is that all the files with size less than 1 MB will also be displayed with file size 1 MB.
The ls command also has -s option to display size. You should combine with -h to show the file size in human readable form.
Here’s the output:
You can also use the stat command in Linux to check the file size.
I hope you find this quick tip helpful in seeing the file size in Linux.
Источник
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:
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.
Источник