How do I create a new empty file in a bash script?
Solution 1
Use touch
command. touch filename.txt
.
Solution 2
Use the null command (:
) redirect (> filename
) trick (:>
), as this will truncate to zero or create the named file.
$ echo foo > filea
$ :> filea
$ wc -c filea
0 filea
$ rm filea
$ :> filea
$ wc -c filea
0 filea
(This will fail if the shell sets a NOCLOBBER option.)
Solution 3
The shortest way:
>filename
Solution 4
You could always use perl, too.
$ stat filename.txt
stat: cannot stat 'filename.txt': No such file or directory
$ perl -e 'open($fh,">","filename.txt") or die $!;close($fh)'
$ stat filename.txt
File: 'filename.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 280728 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ xieerqi) Gid: ( 1000/ xieerqi)
Access: 2017-02-08 13:51:01.479121995 -0700
Modify: 2017-02-08 13:51:01.479121995 -0700
Change: 2017-02-08 13:51:01.479121995 -0700
Birth: -
Solution 5
dd
can be used to create an empty file as follows:
dd if=/dev/null of=filename.txt count=0
One case where dd
is more useful than the other approaches is creating an EFI variable (PK) through the efivars
filesystem (/sys/firmware/efi/efivars
).
One some platforms and Linux distributions, the file is created with the "immutible
" attribute set (shows up as the "i
" flag with "lasattr
"). So, any subsequent operations on the file fail.
For example, touch
not only creates the file, but sets the time stamp on the file. Similarly, piping to the file with >
not only creates the file, but performs additional operations that fail. The dd
command does not appear to fail the same way.
Related videos on Youtube
Ioan-Radu Tanasescu
Updated on September 18, 2022Comments
-
Ioan-Radu Tanasescu over 1 year
I have to remove a lot of
font-family:
declarations from my website, they are hundreds and I have replaced most of them with the same line, only commented (ex:/* font-family: "Segoe UI","Helvetica","Arial"; */
).Now, if I could only search for the un-commented ones to find the few that are left. My first atempt clearly demonstrates I do not fully understand regular expressions:
^(?!(?:/*)).*\s*."font-family:"."*"
The syntax is Boost (but only a subpart of it, as the replacement is PCRE-style).
-
Wiktor Stribiżew over 8 yearsIf you are using Find and Replace, the regex syntax is Boost (but only a subpart of it, as the replacement is PCRE-style).
-
Ioan-Radu Tanasescu over 8 yearsOk, thanks :) editing question.
-
Wiktor Stribiżew over 8 yearsSo, you just want to keep
/*...*/
comments and removefont-family: "..","...";
everywhere else? -
Ioan-Radu Tanasescu over 8 yearsI want to not find the commented ones, and wrap the rest in comment tags, but a replace all is not needed, simply finding them is enough, there are not too many left. This somewhat works: ^(?!(?:/*)).*\s*.font-family:.*
-
Wiktor Stribiżew over 8 yearsCould you please check my approach? I "ignore" font-family inside the comments, and remove all others, but I am not sure what the final output is required.
-
Ioan-Radu Tanasescu over 8 yearsI have tried, thank you, it also finds other comments :)
-
Ioan-Radu Tanasescu over 8 yearsAs I said, it returns other results as well, including stuff wrapped in comments :)
-
Wiktor Stribiżew over 8 yearsThen please clarify with examples. In Boost syntax, you cannot use
(*SKIP)(*F)
verbs, you must match and capture. I find the question rather unclear now. -
Ioan-Radu Tanasescu over 8 yearsOk, so I want to run a search that will find all
font-family:
strings not preceded by a comment tag. So the search should return ALL lines containingfont-family:+WHATVER
but NOT return/* font-family:+WHATVER
-
Wiktor Stribiżew over 8 yearsLike
^(?:(?!/\*).)*\s*font-family:.*
? I fixed your tempered greedy token. -
Ioan-Radu Tanasescu over 8 yearsWorks awesome, all my upvotes are belong to you :) post it so I can chose it as correct answer.
-
-
Wiktor Stribiżew over 8 yearsSorry, wife is grumbling too much, I will add explanations later.
-
Hannu over 7 years
echo -n >file
-
ilkkachu over 7 yearsJust
> filename.txt
would do, no need for the:
. Or>> filename.txt
if we don't want to trash it if it does happen to exist. -
can-ned_food about 7 years@ilkkachu although the question does specify Bash, using
:
does make the script more likely to accomplish its task with other shells e.g. Zsh. -
johanvdw almost 6 yearstake care - this will not empty your file if it already exists
-
andrewz about 4 yearsActually I tried touch and "echo -n > file" inside my bash script but I get an error when I execute the script: "No such file or directory" on that line. Can someone help please?
-
DevonDahon over 3 yearsBest answer. Couldn't be shorter.