野声

Hey, 野声!

谁有天大力气可以拎着自己飞呀
twitter
github

A very convenient command: cdtmp

Long, long ago, when watching sorrycc's video, I discovered a very tricky command: cdtmp. After executing this command, the shell would jump to the /tmp/sorrycc-xxxxxx folder, which I found very useful.

I have been using it for a long time myself, and it's really handy. There are many scenarios where it can be used:

If you want to write a small test case to validate an idea, or validate the usage of a function, or clone a repository to check related content, you can simply use cdtmp to open a temporary folder and do whatever you want inside it. You don't have to worry about cleaning up these files later. For Windows users, temporary files are usually deleted when using cleaning software to remove junk files, and Linux also has corresponding logic and methods for clearing the tmp directory.

Let me briefly introduce this command and provide an implementation in zsh as well as a function with the same functionality in PowerShell for Windows.

The purpose of the command is to create a folder in the system's temporary directory and then navigate to it.

zsh#

The cdtmp mentioned by sorrycc:

cdtmp, enter a randomly created temporary directory, simple and easy to use, http://frantic.im/cdtmp
-- https://github.com/sorrycc/zaobao/issues/2

The original article provides a link to the implementation in zsh.

Add the following line of code to your .zshrc file:

alias cdtmp='cd `mktemp -d /tmp/artin-XXXXXX`'

This command will create a folder named artin-XXXXXX in the system's temporary directory and then navigate to it.

PowerShell#

I am using PowerShell Core 7. If you are unable to use it, you can make some modifications.

In PowerShell, it is not possible to achieve this effect with just Set-Alias (or it would be more complex to implement). However, it can be written as a function, which is also very simple and can be executed directly in PowerShell.

However, PowerShell only has the New-TemporaryFile method, which cannot directly create a folder with a single command.

So we need to use a more conventional method to achieve this, which is to concatenate the name of the temp folder to be created and then navigate to it.

function cdtmp {
    $parent = [System.IO.Path]::GetTempPath()
    $name = 'artin-' + $([System.IO.Path]::GetRandomFileName()).Split(".")[0]
    New-Item -ItemType Directory -Path (Join-Path $parent $name)
    cd (Join-Path $parent $name)
}

Simply put this code into the $PROFILE file.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.