From the course: Linux CentOS 7: Files and Permissions

Create files and dirs - Linux Tutorial

From the course: Linux CentOS 7: Files and Permissions

Start my 1-month free trial

Create files and dirs

- [Instructor] We can use applications like text editors such as Vim or Gedit to create files just the same as we would in other operating systems. I'll use Vim for this exercise. In a terminal type in vim ~/textfile.txt and hit Enter. And now we're in Vim and we can add text by pressing the Insert key and typing. You'll notice that the bottom line says INSERT. This let's you know that you're in insert mode. Go ahead and type in some text. When you're done, press Escape:wq and hit Enter. If you'd like to get a quick overview of how to use Vim, check out my course in the library titled Linux Directory Structure and Basic Tools. Now let's verify that we have a file by typing ls -l ~ and hitting Enter. We can see that we have a textfile.txt in the list. In both cases I use the ~ as a shortcut to my home directory. Using Vim to create ~/textfile.txt creates a file called textfile.txt in my home directory. Listing ~ with ls will display the contents of my home directory, including the attributes of this file. For simple text files, we can create them easier using a redirect. Type in echo "A new line" >> ~/textfile.txt and hit Enter. This will append the text, a new line, to the end of the textfile.txt file. Had I used one greater-than symbol instead of two, it would've overwritten the file. We can verify this by using the cat command to view the file. Type in cat ~/textfile.txt and hit Enter. We can see the original line that I edited with Vim, and then we can see the line that I appended using echo. If all we want is an empty file, we can use the touch command. Type in touch ~/emptyfile.txt. This should create a file called emptyfile.txt. Let's verify with ls -l ~ and hit Enter. We can see that we do have an emptyfile.txt, and that the size of the file is actually zero. The touch command's real job is to update the timestamp of a file. But if the file doesn't exist, it creates it. To create a directory or folder we'll use the mkdir command. Type in mkdir ~/newdir, and hit Enter. And verify with ls -l ~, and hit Enter again. We can see that we have newdir in the list. If we try to create an entire directory structure, and some of the path doesn't exist, mkdir will complain. For instance, type in mkdir ~/parent/child and hit Enter. The error message we get is cannot create directory/home/grant/parent/child : No such file or directory. The reason this give us an error is that the parent directory doesn't exist, so mkdir can't make child. mkdir does have an option to create parent directories though. Type in mkdir -p ~/parent/child and hit Enter. This time we did not get an error message. Let's verify using the Find command. Type in find ~/parent/child and hit Enter. And this should output the absolute path to the child directory. The reason we used find to verify in this case, is if we list ~/parent/child using ls, it won't give us a useful message since there's nothing in it. For instance, type in ls -l ~/parent/child and hit Enter, and the message we get is total space zero. You can use brace expansion outlined in another video of this course to create multiple files or directories at one time. Type in mkdir ~/{dir1,dir2,dir3} and hit Enter. Now we can verify with ls -d ~/dir? Hit Enter. And it lists out all three directories that we created. I used the dash d option to get ls to show the directory metadata, instead of showing the directory contents. Notice that I also used a question mark to (mumbles) for the last digit. I could've also used a set such as left square bracket, zero through nine, right square bracket.

Contents