Skip to main content
ubuntuask.com

Back to all posts

How to Check the Size Of A File In Bash?

Published on
5 min read
How to Check the Size Of A File In Bash? image

Best Bash Scripting Tools to Buy in October 2025

1 Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

BUY & SAVE
$23.74 $39.99
Save 41%
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali
2 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • AFFORDABLE PRICING FOR QUALITY READS WITHOUT THE RETAIL MARKUP.
  • ECO-FRIENDLY CHOICE: SUPPORT RECYCLING BY BUYING USED BOOKS!
  • UNIQUE FINDS: DISCOVER RARE TITLES NOT AVAILABLE IN STORES.
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
3 Shell Scripting: Expert Recipes for Linux, Bash, and more

Shell Scripting: Expert Recipes for Linux, Bash, and more

BUY & SAVE
$38.49 $49.99
Save 23%
Shell Scripting: Expert Recipes for Linux, Bash, and more
4 Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting

Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting

BUY & SAVE
$37.98
Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting
5 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$25.73 $49.99
Save 49%
Classic Shell Scripting
6 Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

BUY & SAVE
$2.99
Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming
+
ONE MORE?

To check the size of a file in Bash, you can use the stat command in combination with the file path.

Here is an example of how you can do it:

  1. Open your terminal or command prompt.
  2. Type the following command: stat --printf="%s" /path/to/file Replace /path/to/file with the actual path of the file you want to check.
  3. Press Enter.

The stat command retrieves information about files in Linux and provides various data formats. In this case, the --printf="%s" option is used to print only the size of the file in bytes.

The output will be the file size in bytes.

How to navigate to a directory in Bash?

To navigate to a directory in Bash, you can use the cd command followed by the directory path.

Here are the steps:

  1. Open the Bash terminal.
  2. Use the cd command with the directory path as an argument. For example, to navigate to the "Documents" directory in your home directory, you would use: cd Documents
  3. Press Enter. You will be switched to the specified directory.

You can also use relative or absolute paths with the cd command.

  • Relative path: A path that is relative to your current working directory. For example, if you are in the "home" directory and want to navigate to the "Documents" directory, you can use: cd ./Documents Here, ./ represents the current directory. Alternatively, you can omit ./ and simply use cd Documents.
  • Absolute path: A complete path that starts from the root directory. For example, if you want to navigate to the "Documents" directory from any location, you can use: cd /home//Documents Replace with your actual username.

Note: You can use the ls command to list the contents of a directory and verify your current location.

How to read a file line by line in Bash?

To read a file line by line in Bash, you can use the read command. Here's an example:

#!/bin/bash

Specify the path to your file

file_path="path/to/your/file.txt"

Read the file line by line

while IFS= read -r line do echo "$line" done < "$file_path"

In this example, the read command is used in a while loop. It reads each line from the file and assigns it to the variable line. Then, you can use $line to process or manipulate each line as desired.

The IFS= in IFS= read -r line is used to preserve leading and trailing whitespace in each line. The -r option is used to prevent any backslashes in the file from being interpreted as escape characters.

The < "$file_path" at the end of the loop is used to redirect the contents of the file to the loop.

Make sure to replace "path/to/your/file.txt" with the actual path to your file.

How to check if a file is empty in Bash?

To check if a file is empty in Bash, you can use the -s operator with an if statement. Here's an example:

#!/bin/bash

file="myfile.txt"

if [ -s "$file" ]; then echo "File is not empty." else echo "File is empty." fi

In the above example, the -s operator checks if the file size is greater than zero. If the file is not empty, it executes the code inside the if block and prints "File is not empty." Otherwise, it executes the code inside the else block and prints "File is empty."

How to write a shell script in Bash?

To write a shell script in Bash, follow these steps:

  1. Open a text editor of your choice (e.g. Notepad, Sublime Text, or Visual Studio Code).
  2. Start the file with a shebang line, which specifies the path to the Bash interpreter: #!/bin/bash
  3. Write the commands and logic of your script below the shebang line. For example, let's create a script that prints "Hello, World!": #!/bin/bash echo "Hello, World!"
  4. Save the file with a .sh extension, such as script.sh.
  5. In order to run the script, you need to set the file as executable. Open your terminal and navigate to the directory containing the script. Then, run the following command: chmod +x script.sh This command grants execute permission to the file.
  6. Finally, you can run the script by typing its filename preceded by ./: ./script.sh The output should be: "Hello, World!"

These are the basic steps to create and run a simple shell script in Bash. You can add more commands, use variables, handle user input, and create complex logic to automate tasks based on your specific requirements.

How to change permissions of a file in Bash?

To change the permissions of a file in Bash, you can use the chmod command.

The general syntax of the chmod command is as follows:

chmod [options]

Here are some examples of how to change file permissions using chmod:

  1. Change the permissions to read and write for the owner, and read for everyone else:

chmod 644 file.txt

  1. Give read, write, and execute permissions to the owner, read and execute permissions to the group, and read permissions to others:

chmod 755 script.sh

  1. Give read, write, and execute permissions to the owner and group, and no permissions to others:

chmod 770 directory/

There are also various options you can use with chmod command:

  • -c: Prints a message for each file whose permissions are changed.
  • -v: Prints a verbose output, showing all the files whose permissions are changed.
  • -R: Applies the permissions recursively to all files and directories within the specified directory.

For example, if you want to change the permissions of a directory and its contents recursively, you can use the following command:

chmod -R 755 directory/

Note: To execute the chmod command, make sure you have the necessary permissions to modify the file or directory.