The touch
command in Linux is used to change a file’s “Access“, “Modify” and “Change” timestamps to the current time and date, but if the file doesn’t exist, the touch
command creates it.
If you simply want to create an empty file from the command-line prompt (CMD) or a Windows PowerShell – the type
and copy
commands can be considered as a Windows touch
command equivalent.
The file timestamps in Windows can be changed using the built-in PowerShell commands.
Cool Tip: Windows cat
command equivalent in CMD and PowerShell! Read more →
To create a new file, as a Windows touch
equivalent, you can use one of these commands:
C:\> type nul >> "file.txt"
- or -
C:\> copy nul "file.txt"
In the PowerShell the new file can be also create as follows:
PS C:\> New-Item "file.txt"
- or -
PS C:\> ni "file.txt"
To change a file timestamps to the current time and date, execute the following commands from the PowerShell:
PS C:\> (Get-Item "file.txt").CreationTime=$(Get-Date -format o) PS C:\> (Get-Item "file.txt").LastWriteTime=$(Get-Date -format o) PS C:\> (Get-Item "file.txt").LastAccessTime=$(Get-Date -format o)
Cool Tip: Windows grep
command equivalent in CMD and PowerShell! Read more →
To set the specific timestamps, execute:
PS C:\> (Get-Item "file.txt").CreationTime=("01 March 2020 09:00:00") PS C:\> (Get-Item "file.txt").LastWriteTime=("20 April 2020 17:00:00") PS C:\> (Get-Item "file.txt").LastAccessTime=("20 April 2020 17:00:00")
The timestamps can be displayed using the following command:
PS C:\> Get-Item file.txt | Format-List CreationTime, LastAccessTime, LastWriteTime
Was it useful? Share this post with the world!
Введение | |
echo | |
New-Item | |
Похожие статьи |
Введение
Из этой статьи вы узнаете как создать новый файл из терминала PowerShell.
В
Linux
это делает команда touch
echo
Первый вариант
echo $null >> new_file.txt
Второй вариант, который приходит в голову — это использовать echo.
echo $null >> new_file.txt
New-Item
Пример создания файла
config.toml
с помощью New-Item
New-Item -Path . -Name «config.toml» -ItemType «file»
Пример создания файла с конфигурацией PowerShell
New-Item -Path $profile -Force -ItemType «file»
Автор статьи: Андрей Олегович
Похожие статьи
Windows | |
PowerShell | |
Alias | |
Запросы к REST API | |
Пользователи | |
Сеть | |
Установка | |
Файлы | |
Функции | |
Циклы | |
Ошибки PowerShell |
Windows Explorer doesn’t allow you to create files that start with .
because it interprets that as not having a file name.
On Linux/UNIX you would simply use: touch .gitignore
.
Windows doesn’t have a touch
command.
But if you run an invalid command and pipe that to a file that starts with a dot, the file will be created.
For example,
If you run foo>.gitignore
the .gitignore file will still be created — even though foo
is not a valid command. Feel free to try with any command, such as bar>.gitignore
.
Even though the command outputs an error, the file is still created.
Here are some other ways that work as well:
copy nul .gitignore
copy con .gitignore
hit enter, then hit ctrl+zcat>.gitignore
hit enter, then hit ctrl+cecho > .gitignore
foo>.gitignore
fsutil file createnew .gitignore 0
nul > .gitignore
notepad .gitignore
- In Windows Explorer, name the file
.gitignore.
(with a period at the end of the file name) and Windows will rename it to.gitignore
bash -c "touch .gitignore"
Let me know if you know of any other ways and I’ll add them to the list.
Jon
In Linux and Unix there is a “touch” command which will update the timestamp of a file without modifying the contents. You can also create an empty file without having to open an application among other actions. In Windows there isn’t a direct equivalent, but you can get close by using the “copy” command with a “+” at the end of the filename while specifying no destination file. The + symbol points the copy operation back to the source file. This will update the timestamp while not modifying the contents of the file.
Ex.
copy SampleFile.txt+
This process was helpful for my customer testing out automated CI/CD processes with Azure DevOps and Git. Hopefully this will be useful to someone else. Enjoy.
-Frog Out
Repository files navigation
Touch
Unix equivalent of the «touch» command for Windows.
Usage
touch <filename> <flags>
Examples:
Create «notes.txt» file in the current directory:
touch notes.txt
Create «settings.json» file in the current directory, and open it with the default editor:
touch -o settings.json
Flags:
-o Open file after creating/updating with default editor
-a Change access/write time on file when file already exists
-c Do not create any files