How can I create and open a file from terminal with a single command?

后端 未结 8 1290
野性不改
野性不改 2020-12-24 06:56

To create a file from terminal I type the following...

$ touch filename.py

To open the file I just created from terminal, I then type...

相关标签:
8条回答
  • 2020-12-24 07:33

    you can use:

    cat > youNewFile.someExtension
    

    Example:

    cat > myNewFile.txt
    

    After you are done press Ctrl + d to save or Ctrl + c to abort (but in this case it's gonna save an empty file. The redirection operator ( > ) will create the file if it doesn't already exists in your folder and you will be able to edit it right a way through the terminal.

    0 讨论(0)
  • 2020-12-24 07:35

    On a Mac to create a lazytouch function to create and open a file in one line you have to edit .bashrc. You might have to first create it. Beware if you are a novice programmer. Some of these commands might require you to prepend sudo for permission to create and save. Enter these commands in terminal.

    $ cd ~
    
    $ touch .bashrc
    
    $ open .bash_profile
    

    Enter this profile in .bash_profile to check for .bashrc

    # To get aliases and functions
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    

    Remember to save .bash_profile. Then in bash do this.

    $ open .bashrc
    

    Enter this text in the .bashrc

    # .bashrc
    
    # User specific aliases and functions
    
    lazytouch() {
        touch $1
        open $1
    }
    

    Remember to save .bashrc

    Now you can cd to any folder then create and open a file with one line.

    $ lazytouch anything.really
    
    0 讨论(0)
提交回复
热议问题