shell script to create empty files with all possible permissions
Do a loop:
for ((i=0; i < 512; i++)); do
mod=$(printf "%03o" "$i");
touch ${mod}.txt; chmod $mod $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 $mod.txt
n=$(ls -l $mod.txt | cut -b1-10)
mv -- $mod.txt "$n.txt"
done
How can I create an empty file at the command line in Windows?
Without redirection, Luc Vu or Erik Konstantopoulos point out to:
copy NUL EMptyFile.txt
copy /b NUL EmptyFile.txt
"How to create empty text file from a batch file?" (2008) also points to:
type NUL > EmptyFile.txt
# also
echo. 2>EmptyFile.txt
copy nul file.txt > nul # also in qid's answer below
REM. > empty.file
fsutil file createnew file.cmd 0 # to create a file on a mapped drive
Nomad mentions an original one:
C:\Users\VonC\prog\tests>aaaa > empty_file
'aaaa' is not recognized as an internal or external command, operable program or batch file.
C:\Users\VonC\prog\tests>dir
Folder C:\Users\VonC\prog\tests
27/11/2013 10:40 <REP> .
27/11/2013 10:40 <REP> ..
27/11/2013 10:40 0 empty_file
In the same spirit, Samuel suggests in the comments:
the shortest one I use is basically the one by Nomad:
.>out.txt
It does give an error:
'.' is not recognized as an internal or external command
But this error is on stderr. And >
only redirects stdout, where nothing have been produced.
Hence the creation of an empty file.
The error message can be disregarded here. Or, as in Rain's answer, redirected to NUL
:
.>out.txt 2>NUL
(Original answer, November 2009)
echo.>filename
(echo ""
would actually put "" in the file! And echo
without the '.' would put "Command ECHO activated
" in the file...)
Note: the resulting file is not empty but includes a return line sequence: 2 bytes.
This discussion points to a true batch solution for a real empty file:
<nul (set/p z=) >filename
dir filename
11/09/2009 19:45 0 filename
1 file(s) 0 bytes
The "
<nul
" pipes anul
response to theset/p
command, which will cause the
variable used to remain unchanged. As usual withset/p
, the string to the
right of the equal sign is displayed as a prompt with no CRLF.
Since here the "string to the right of the equal sign" is empty... the result is an empty file.
The difference with cd. > filename
(which is mentioned in Patrick Cuff's answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the <nul...
trick) can be used to echo lines without any CR:
<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt
The
dir
command should indicate the file size as 11 bytes: "helloworld!
".
Bash CLI: Is there a way to simultaneously create and set permissions on a file without repeating the filename?
If you are getting a permissions error when you run your command, that suggests that you do not have permissions to create files where you're trying to create the file. Regarding the rest of your question:
As BroLow said, you can use umask
to affect the default permissions of files created in your session. However, this can be inconvenient, particularly if you only want the new permissions in effect for a single command.
You can use the install
command to create and set permissions on a file:
install -m <mode> -o <owner> -g <group> <srcfile> <destination>
If you want to create an empty file, you can use /dev/null
as a source:
install -m 644 /dev/null <destination>
perserve file permissions when distributing from *nix to Windows
In Windows, all of a user's folders are private by default. In order to share files with other users you must explicitly choose to use one of the standard Public folders, or set up a shared folder of your own.
For this reason, the usual convention is for extracted files to inherit the same permissions as the folder they were extracted into. If the user has extracted the files into a shared folder, it should be safe to assume that they did so deliberately because they want the files to be shared.
Is it possible to create a script to save and restore permissions?
hm. so you need to
1) read file permissions
2) store them somehow, associated to each file
3) read your stored permissions and set them back
not a complete solution but some ideas:
stat -c%a filename
>644
probably in combination with
find -exec
to store this information, this so question has some interesting ideas. basically you create a temporary file structure matching your actual files, with each temp file containing the file permissions
to reset you iterate over your temp files, read permissions and chmod the actual files back.
How to delete files/subfolders in a specific directory at the command prompt in Windows
You can use this shell script to clean up the folder and files within C:\Temp
source:
del /q "C:\Temp\*"
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat
Create a new empty file from linux command line with same permissions and ownership?
touch newfile
chmod `stat -c %a originalfile` newfile
chown `stat -c %U originalfile`:`stat -c %G originalfile` newfile
Related Topics
How to Set Firefox Binary Path of Firefox in Selenium in Linux
How to Delete the Matching Pattern from Given Occurrence
Why Does Autoconf Erroneously Find a Function Which Isn't Available Later
Bash Shell: Cannot Use Variable $ as a Path to Run Tar
Mathematical Expression Result Assigned to a Bash Variable
Wget: Unsupported Scheme on Non-Http Url
Will Data Written via Write() Be Flushed to Disk If a Process Is Killed
Read Data from Proc/Sys/Kernel/
Jupyter Lab - Suppress Console Output
Executable Object Files and Virtual Memory
Docker in Wsl2 Alpine Without Docker Desktop
Gnu Assembler .Data Section Value Corrupted After Syscall
Script to Check for Process & Restart Program If Not Found
How to Get the Output of Ansible Ad-Hoc Command in JSON, CSV or Other Format