How to add file in linux

How to Create a file in Ubuntu Linux using command & GUI

Creating files on Linux is not a cumbersome task, however those who are new to it or just shifting from Windows to Ubuntu like systems, they may face some problem to create files using command line especially.

Well, even on Linux anybody can create files and folders using a graphical user interface that works just like a charm. Simply right-click and select the New folder or New Document for text files. However, this is not true with every Linux system. For example on Ubuntu right-clicking will give you the only option to create a new folder, thus when it comes to creating a text using GUI you will get stuck.

Therefore, to create a file on Ubuntu Linux, there are two options either using the command terminal or enable the “new document” option in the right-click context menu of Ubuntu. We will show both the methods.

Create a file in Ubuntu 20.04 using GUI & right-click

Although here we are using Ubuntu 20.04 LTS, the steps given below are applicable for Ubuntu 19.04/18.04 and previous versions.

By default, when we right-click inside anywhere in Ubuntu Nautilus file manager, it will not give us the “New document” option. Thus, to get this missing option, we need to run a command.

  • Open Ubuntu command terminal.
  • Run command- touch

/Templates/Text\ document

  • Now, go to your Linux File Manager and right-click where you want to create a new file
  • Select New Document and then Text Document
  • This will instantly create a new text file on your Linux OS.
  • For example, I want to create some text document files on my Linux Desktop, then inside the Desktop folder I will right-click, rest of the below picture can describe.

    Command-Line to create a new document on Ubuntu Linux

    There are a couple of ways to create an empty file without any content or one with some on Linux, here we show all the best possible ways to do that using the command terminal. The below-given commands are applicable for all types of Linux distros.

    5 Best Ways to create a new file on Linux

    1. Using Touch command
    2. Nano Text editor
    3. VIM/VI text editor
    4. Echo command
    5. Cat command

    Note: Inside any folder that was created with root rights, to create a file there. we have to use sudo with every command given below.

    1. Using Touch command

    As we already have seen in the GUI method, how we have created an empty document using the Touch command in the terminal. Thus, in the same way anywhere in any directory, we can use the touch tool to create an empty file.

    The command syntax is:

    touch file-name

    Now, here we can define the extension to let the system what kind of file we want to create such as a txt, docx.

    For example, I want to create a Text file or Docx, thus the command will be:

    or

    Here is the example screenshot, where we have created a file on the Ubuntu Desktop using command terminal and touch command:

    2. Nano Editor

    Nano is the popular and easy-to-use command-line visual text editor that not allows users to edit any existing file on the system using the terminal but also lets us create a new file to add some content and save it anywhere on Linux.

    Some Linux may not have nano editor by default, thus to install it run:

    For Ubuntu/Debian – sudo apt install nano

    For REHL based Linux- sudo yum install nano

    Now, how to use a nano text editor to create a new file? Well, it is quite simple, on command terminal type- nano along with the filename.

    for example, I want to create a text file then the command will be:

    Add some line or just press the Enter key. Then save the file by pressing Ctrl+X , type Y , and hit the Enter key.

    3. Vim or Vi text editor

    Vi is a command-line text editor that is available to use on every Linux including Ubuntu out of the box, thus no need to install anything, just use its command to create or edit an existing file on your system.

    Now, what is the difference between VIM and VI text editors? Vi is the early implementation of a visual text editor and that is why it is available on all Linux systems, whereas VIM that stands for Vi IMproved, is the enhanced version for Vi text editor with many additions. Thus, you may not find VIM by default on all Linux distros and need to install it manually.

    To install VIM on Ubuntu/Debian type- sudo apt install vim

    For RHEL based– sudo yum install vim

    Nevertheless, the syntax to create a new file on Linux using both the editors will be the same. However, they are slightly complex to use as compared to nano .

    To create a file using Vim or Vi type

    vi filename.txt

    Example:

    To add the content, press the “INSERT” key on your keyboard. Once you are done and want to save the created file’s content press the Esc key and then type :wq , and finally hit the Enter key to exit.

    4. Echo command using Redirect operator

    The echo command is another one available on all Linux distros to not only create a new file but also add the text in the same. I mean you can add the text into your file right the moment you are creating it.

    echo > file-name

    Let’s see some example using the Echo command:

    So, if I want to create a new file on Linux with echo and at the same time I also want to add some text into it, let’s say “My name is Linux” thus the command will be like this:

    echo «My name is linux» > h2s.txt

    In the above, the command h2s.txt file will be created having the content “My name is Linux“.

    If you want to create a blank file with echo then don’t type anything before Redirect > operator i.e >

    5. Cat command

    Cat is also easy to use Linux command that can create a file using a command-line interface.

    Example

    Add the text you want in your file and hit the Enter button. To save the file, press- Ctrl+D .

    Note: For empty files don’t add any text and use only the shortcut to save it.

    If you want to read the text of any existing file without opening it, then cat can be used.

    For example, let’s create a file demo.txt and then added the text “My name is Linux” into it. Once done, save it and again call the same using the cat to read its text directly inside the terminal but without actually opening the file.

    Add text and hit the Enter key

    Save the file – Ctrl + d

    If you want to read the text of the file using the cat command then type:

    Источник

    Linux append text to end of file

    You need to use the >> to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.

    How to redirect the output of the command or data to end of file

    The procedure is as follows

    1. Append text to end of file using echo command:
      echo ‘text here’ >> filename
    2. Append command output to end of file:
      command-name >> filename

    How to add lines to end of file in Linux

    The >> is called as appending redirected output. Create the file if does not exists. For example, append some networking command to net.eth0.config.sh script:
    echo ‘I=eth0’ >> net.eth0.config.sh
    echo ‘ip link set $I up’ >> net.eth0.config.sh
    echo ‘ip addr add 10.98.222.5/255.255.255.0 dev $I’ >> net.eth0.config.sh
    echo ‘ip route add default via 10.98.222.1’ >> net.eth0.config.sh

    You can also add data to other config files. Another option is to run command and append output to a file. Run data command at the terminal and append output to output.txt:
    date >> output.txt
    Execute ls command and append data to files.txt:
    ls >> files.txt
    To see files.txt use cat command:
    cat files.txt
    more files.txt
    less files.txt

    How to append standard output and standard error

    The following sytax allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file name. The format for appending standard output and standard error is:
    echo ‘text’ &>>filename
    command &>>filename
    find . type d -name «*.projects» &>> list.txt
    This is semantically equivalent to
    echo ‘text’ >>fileNameHere 2>&1
    command >>fileNameHere 2>&1
    date >>data.txt 2>&1

    For more info read redirection topic.

    Append text when using sudo

    Try the tee command:
    echo ‘text’ | sudo tee -a my_file.txt
    echo ‘104.20.186.5 www.cyberciti.biz’ | sudo tee -a /etc/hosts
    Of coruse we can use following syntax to append text to end of file in Linux
    sudo sh -c ‘echo my_text >> file1’
    sudo — bash -c ‘echo «some data» >> /my/path/to/filename.txt’
    The -c option passed to the bash/sh to run command using sudo.
    See “how to append text to a file when using sudo command on Linux or Unix” for more info.

    Conclusion – Append text to end of file on Unix

    To append a new line to a text on Unix or Linux, try:

    Источник

    How to create a file in Linux using the bash shell terminal

    I am a new Linux system user. How do I create a file in Linux using the bash shell terminal? What is the fastest and easiest way to create a file in a Linux terminal?

    Introduction – A file is nothing but a container in a Linux based system for storing information. For example, music stored in a file named foo.mp4. Similarly, my cat’s picture stored in kitten.jpg and so on. This page shows various methods to create a file in Linux using the terminal window.

    Tutorial details
    Difficulty level Easy
    Root privileges No
    Requirements Bash shell/terminal on Linux
    Est. reading time 3 minutes

    How to create a file in Linux from terminal window?

    1. Create an empty text file named foo.txt:
      touch foo.bar
      OR
      > foo.bar
    2. Make a text file on Linux:
      cat > filename.txt
    3. Add data and press CTRL + D to save the filename.txt when using cat on Linux
    4. Run shell command:
      echo ‘This is a test’ > data.txt
    5. Append text to existing file in Linux:
      echo ‘yet another line’ >> data.txt

    Let us see some examples for creating a text files on Linux operating systems.

    How to create a text file using the cat command

    To create a text file named sales.txt, type the following command and then press [Enter] key:
    cat > sales.txt
    Now type your lines of text. For example:

    When done and you need to save and exit, press Ctrl + D to return to the bash shell prompt. To view file use cat or more command/less command:
    cat sales.txt
    more sales.txt

    How to create a file in Linux from terminal window

    How to create an empty text file using the touch command

    Simply type any one of the following command:
    > data.txt
    OR
    touch test.txt
    Verify that empty files are created with the help of ls command:
    ls -l data.txt test.txt

    Creating a file in Linux using the echo or printf

    Let us create a file called quote1.txt using echo command, enter:
    echo «While I thought that I was learning how to live, I have been learning how to die.» > quote1.txt
    OR use the printf command printf ‘Study nature, love nature, stay close to nature. It will never fail you.\n’ > quote2.txt

    Appending data

    Use the the >> instead of > to append data to existing file and to avoid overwriting files. The syntax is:

    • No ads and tracking
    • In-depth guides for developers and sysadmins at Opensourceflare✨
    • Join my Patreon to support independent content creators and start reading latest guides:
      • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
      • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
      • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
      • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
      • How to protect Linux against rogue USB devices using USBGuard

    Join Patreon

    How to create a file in Linux using joe text editor

    JOE is text editor. To create a file called delta.txt, type:
    joe -help delta.txt
    You will see help menu on screen. Next type something. To save the file and leave joe, by typing ^KX (press CTRL+K+X).

    How to create a text file in Linux using vi / vim text editor

    The vi / vim is another text editor. To create a file called purchase.txt, type:
    vi purchase.txt
    OR
    vim purchase.txt
    Press i to insert new text. To save the file and leave vi, type ESC + : + x (press ESC key, type : followed by x and [enter] key).

    Conclusion

    You learned various methods that allow you to create text files in a Linux or Unix/macOS terminal window quickly.

    🐧 Get the latest tutorials on Linux, Open Source & DevOps via

    Источник

    Читайте также:  Перемещение мыши mac os
    Оцените статью