Discussions

linux - shell script to create empty files with all possible permissions - Stack Overflow
How to write a shell script that creates empty files with all possible permissions. File names should be, for example, rwxrw_r__.txt. I know how to do it manually. As an example: #!/bin/bash touch... More on stackoverflow.com
🌐 stackoverflow.com
How do I create an empty file in all user directories?
For current users: for user in /home/*; do touch ${user}/.emptyfile done For new users, create an empty file in the /etc/skel directory, and then use the -m option while creating user with useradd. edit, r/make_onions_cry for logic is better, updated the for loop to use their's. More on reddit.com
🌐 r/bash
8
3
October 3, 2020
bash - Create an empty file in a directory which may or may not exist - Stack Overflow
I want to make a shell script that creates an empty file as /data/app/log/app.log.pos. The /data/app/log/ dir may or may not exist. If it does not exist, it should create it. When the path does not More on stackoverflow.com
🌐 stackoverflow.com
April 24, 2017
Empty a file in linux bash - Stack Overflow
which one is the best way to clean or empty a file in Linux? I have to zip ( tar ) a file to an archive and then clean/empty it; this is what I do and it works correctly: tar -zcvf /mnt/file.tar.g... More on stackoverflow.com
🌐 stackoverflow.com
🌐
nixCraft
cyberciti.biz β€Ί nixcraft β€Ί tutorials β€Ί shell scripting β€Ί bash shell create empty temporary files quickly
BASH Shell Create Empty Temporary Files Quickly - nixCraft
March 13, 2024 - In this example, create temporary filename in using a user’s $TMPDIR environment variable: ... Use /tmp/tmp.yTfJX35144 to store your output. You can store filename to a variable: ... The following bash scripting illustrates a simple use of mktemp where the script should quit if it cannot get a safe temporary file:
🌐
BrandCrock gmbh
brandcrock.com β€Ί home β€Ί blog β€Ί how to create an empty file in linux command line
Create an empty file in Linux Command Line - BrandCrock
January 16, 2025 - One of the key strengths of Linux is its ability to automate tasks using shell scripts. You can easily write a script to automate the creation of empty files. bash #!/bin/bash # Script to create multiple empty files for i in {1..5} do touch "file_$i.txt" done
Top answer
1 of 3
3

Do a loop:

for ((i=0; i < 512; i++)); do 
    mod=$(printf "%03o" "$i"); 
    touch ${mod}.txt; chmod mod.txt; 
done

Rather than trying to construct the names, if you want the names to look like the output of ls -l, just do something like

for ((i=0; i < 512; i++)); do
    mod=$(printf "%03o" "$i") 
    touch ${mod}.txt
    chmod mod.txt
    n=mod.txt | cut -b1-10)
    mv -- $mod.txt "$n.txt"
done
2 of 3
3

It's just a permutations problem.

p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for u in "${p[@]}"; do for g in "${p[@]}"; do for o in "${p[@]}"; do
  f="task5/g$o.txt"; touch -- "$f" && chmod "u={g//-/},o=${o//-/}" -- "$f";
done; done; done

NOTE

  • thanks to @kvantour for pointing out I was passing dashed to chmod, and that it doesn't know what to do with them. I am surprised I wasn't getting errors.

Let's break it down and look at what's happening.

If you have any questions about what permissions sets mean or how chmod works, see here.

So for each of the user, group, or other, there are eight possible symbolic representations (representing the values of one octal digit, 0-7).

We set those into a simple array we can loop over.

p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions 

You can access any element with it's octal digit (technically the decimal equivalent, but that doesn't matter unless you go over 7) so ${p[5]} is r-x. Indexing with @ returns the whole array, so the loops walk through them sequentially with ${p[@]}.

To get every possible permutation, we loop over them for each of user/group/other.

for u in "${p[@]}"; do # assign each permission set for the user
for g in "${p[@]}"; do # assign each permission set for the group
for o in "${p[@]}"; do # assign each permission set for the other

This is just simple iterations in nested loops to hit every permutation.

  f="task5/g$o.txt" # assign the permissions string AS the filename

By putting the path and filename info into a variable, we can maintain any changes in one place, and it makes the rest of the line shorter and easier to read.

  touch -- "$f" && # create the file and test for success

touch will create an empty file. Because the filenames could sometimes begin with a dash (any time the permissions disallow user read), we give touch a first argument of --, which is a *NIX standard idiom meaning "options are done now, anything left is arguments"; otherwise it would try to interpret a leading dash as an invalid option set and fail. This won't be a problem while you are putting "task5/" at the beginning of the filename, but if you end up using the filename bare it would.

The && is a boolean test to see whether touch succeeded. If it did not, then we silently skip trying the chmod (touch should have emitted an error message for your debugging, but if that fails, you probably got a ton of them, and will need to fix whatever ...)

  chmod "u={g//-/},o=${o//-/}" -- "$f" # change the permissions

This uses chmod's symbolic mode. We have the permissions of each section from the nexted loops - just apply them. Again, we use the -- to tell chmod when we are done passing options so that leading dashes in filenames won't be a problem later if you refactor just just cd into the directory and create the files locally, though even then you could always prefix ./ or $PWD/ on it.

We have to get rid of thew dashes in the symbolic file modes, though, as (thanks agains @kvantour) chmod doesn't recognize those. An inline string edit works beautirully: in "u={g//-/},o=${o//-/}", the // inside the variable spec is a replacement of all occurrences, replacing - with the nothing between the following / and }.

done; done; done # these just close each of the loops

We could (and probably should) put each of these on separate lines, but the interpreter doesn't care since we used semicolons. It lets us compact the code to put the loop nesting and closures on lines together, so long as you are comfortable with the ONE thing that's changing being obvious enough.

Anything you still have questions about that I didn't cover?

Alternate

Another version, because I like going through a loop once instead of nested shenannigans...

p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for dec in {0..511}; do oct="$(printf "%03o" "$dec")"
  u="${p[${oct:0:1}]}"; g="${p[${oct:1:1}]}"; o="${p[${oct:2:1}]}";
  f="task5/g$o.txt"; touch "$f"; chmod "u={g//-/},o=${o//-/}" "$f";
done

This walks through the combinations numerically, converts decimal to octal with printf, slices each digit out of the octal permission set with basic substring parsing and uses it to look up the relevant string from the array, assign the segments, assign the resulting filename, touch/create the file, then apply the scrubbed permissions strings with chmod. It's once-through and faster, if a little harder to understand.

u="${p[${oct:0:1}]}" # grabs 1 byte from offset 0 of $oct as index to $p

As suggested, to skip the decimal to octal conversion step, replace:

for dec in {0..511}; do oct="$(printf "%03o" "$dec")"

with

for oct in {0..7}{0..7}{0..7}; do 
🌐
nixCraft
cyberciti.biz β€Ί nixcraft β€Ί howto β€Ί linux β€Ί how to create empty file in linux
How to create empty file in Linux - nixCraft
February 21, 2022 - Me thinks that touch command is the easiest way to create new, empty files on Linux or Unix-like systems including macOS. touch foo bar baz ls foo bar baz ... Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin. Next FAQ: How to create snapshot in Linux KVM VM/Domain
🌐
Reddit
reddit.com β€Ί r/bash β€Ί how do i create an empty file in all user directories?
How do I create an empty file in all user directories? : r/bash
October 3, 2020 - Best practice is to copy all stuff ... format because im writing it with my phone. ... For new users, create an empty file in the /etc/skel directory, and then use the -m option while creating user with useradd....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί linux-unix β€Ί touch-command-in-linux-with-examples
Creating an Empty File in Linux | Touch Command - GeeksforGeeks
It’s one of the simplest and most commonly used commands for file management. The touch command creates a new, empty file if the file does not already exist.
Published Β  December 8, 2025
🌐
Quora
quora.com β€Ί How-do-you-create-an-empty-text-file-in-Linux
How to create an empty text file in Linux - Quora
Answer: 1. Open a terminal window. Press CTRL+ALT+T on Linux to open the Terminal app. 2. To create an empty file from command line in Linux: touch fileNameHere 3. Verify that file has been created with the ls -l fileNameHere on Linux Let us see some examples and commands to create empty file in ...
🌐
YouTube
youtube.com β€Ί watch
How To Create Empty File In Linux - YouTube
Creating an empty file in Linux is a simple task that can be done using various commands available in the Linux command line. Below is a step-by-step guide o...
Published Β  August 9, 2024
🌐
Unix.com
unix.com β€Ί shell programming and scripting
Creating Empty File - Shell Programming and Scripting - Unix Linux Community
November 19, 2017 - Hi, I want to create EMPTY File through Shell Script but not using touch Command, Please let me know any work around.. Thanks, Samadhan
🌐
CyberCiti
bash.cyberciti.biz β€Ί guide β€Ί Empty_file_creation
Empty file creation - Linux Bash Shell Scripting Tutorial Wiki
February 21, 2022 - If no command given and if file doesn't exist it will create empty file. For example, create a shell script called tarbackup.sh: #!/bin/bash TAR=/bin/tar # SCSI tape device TAPE=/dev/st0 # Backup dir names BDIRS="/www /home /etc /usr/local/mailboxes /phpjail /pythonjail /perlcgibin" # Logfile name ERRLOG=/tmp/tar.logfile.txt # Remove old log file and create the empty log file >$ERRLOG # Okay lets make a backup $TAR -cvf $TAPE $BDIRS 2>$ERRLOG
🌐
Quora
quora.com β€Ί How-do-I-create-a-blank-file-in-Linux-from-command-line
How to create a blank file in Linux from command line - Quora
Answer (1 of 14): [code]$ touch file.extension [/code]^ The proper way [code]cat > file.extension [/code]cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to concatenate files. [wikipedia]. You are displaying not...
🌐
Vultr
docs.vultr.com β€Ί how-to-create-an-empty-file-in-linux-using-the-touch-command
Create Empty Files in Linux with the touch Command Guide | Vultr Docs
November 21, 2023 - Create log files in a Bash script using touch. Create a sample script log_script.sh using a text editor such as Nano ... Add the following contents to the file. ... Save and close the file. ... The above script creates five empty log files in your working directory using the format logfile_1.log to logfile_5.log.
🌐
Bash Commands
bashcommands.com β€Ί bash-create-empty-file
Bash Create Empty File: Your Quick Guide to Getting Started
April 17, 2025 - Syntax for Creating an Empty File The syntax for this method is simple: ... Explanation: This command tells Bash to take no input (an empty output) and redirect it to create `another_empty_file.txt`. If the file already exists, this command will empty its contents.
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-create-a-new-file-in-linux-from-bash
How to Create a File in Linux from the Command Line?
To create a new file, you need to run the touch command followed by the name of the file. For example, ... It will create an empty file called "hello.txt" in the current directory.