We often have to work with the files whether to write, read, copy, rename, move, or something else. With the help of Terminal, we can do such tasks easily without opening any files at all. This article helps you to learn and understand Linux File Commands in the terminal for the complete beginner.
But before going through this article, we suggest reading this article: basic terminal commands for navigation so that you can easily learn these commands without any problems.
So let’s begin with opening a terminal. You can do that by any of the following way:
Ctrl + Alt + T
Alt + F2and type gnome-terminal
- Press Window Key and search for Terminal
touch command is a standard command in the Linux system to create an empty file. This command cannot be used to write content in the file. However, we can use
touch command to change the timestamp of a file even if it is not empty.
$ touch new.txt
We created a file ‘new.txt’ using
touch command in the Desktop directory. This file is an empty file.
We can use
touch command to create many files at a same time. Let’s see that in action.
$ touch new1.txt new2.txt new3.txt
Note-1: Remember to put space between command and file name and also between different file names.
All the above files that we created are empty ones.
Now let’s use
touch command in the existing file and see what does it change in the file.
If you see the highlighted text, you can see the difference in the creation of time. The file ‘new.txt’ was created at 11:13 but when we use
touch command once again, the creation time is changed to 11:35.
cat command (or concatenate) is a widely used command in the Linux system to view the full content of a file. But
cat command has many other functionalities like creating a file and writing it, copying the content of a file, and many more.
let’s create a file and write some text in it.
cat > fileName
$ cat > file1.txt .............write your text..... ^C $
When you type
cat > file1.txt you are prompted to write in that file. After you complete writing, Press Enter and press
Ctrl + C to finish writing.
Now let’s use the same
cat command to read the file. When we use the
cat command with the existing file, it shows the content of the file as an output in the terminal. This is the best command to read the full content of any file.
Note-2: Remember: Use > to create and write in a file and Don’t use > to read a file.
We can also use
cat command to append the content from one file to the end of other file. Look at the example below in the image, where we append content from ‘file1.txt’ to the end of a content of ‘file2.txt’.
head or tail command
head and tail command, both are useful for viewing the content of a file. But the only difference between them is: head command shows the first 10 lines whereas the tail command shows the last 10 lines of a file by default.
head fileName or tail fileName
For example, I have created a file name ‘new.dat’ in the Desktop directory. This file contains 200 lines as 1 in the first line, 2 in the second line, 100 in the 100th line, and 200 in the 200th line. Let’s use the head and tail command on that file and see the output.
As you can see, the
head command shows the first 10 lines, and the tail command shows the last 10 lines.
There are some options too that can be used with
tail commands. If you want control over the number of lines to display, you can use
-n value option to change that.
$ head -n 20 new.dat
Same option can also be used with
more or less command
less command is also a very good command for viewing the content of the file. These commands are better than head or tail or cat command in viewing the content. Using
less command we can view the whole content navigating through page or line at a time.
more command, first, it shows 10% of the content. Then pressing the
Enter key or
Space key, we get more lines. If you want to stop navigating, press
q for quit.
less command is faster than
more command in its use. With
less command, we can navigate lines or pages as per our need. First,
less command also shows the output similar to more command but then pressing the
Space key, we can navigate page by page.
After you press
Space, the colon ‘:’ will appear at the bottom. You can put number to view the content after that much line.
Command Line Text Editor
If you are tired of the text editor that offers a graphical interface and want to try a command-based text editor, then there are many choices on Linux. Using Command Line-based text editor, you can read, write, edit, and more all through a terminal. The simplest command line text editor for beginners is the
nano text editor. But if you want to do more and want advanced features then we suggest using a
vim text editor.
nano in Ubuntu:
$ sudo apt-get install nano
nano, all you have to type is
nano then you will get a text-editor inside a terminal.
If you want to give a file name at the beginning then type
Ctrl + X to exit out. If you want help with commands, Press
Ctrl + G for help.
If you want to use vim text editor then, install vim in Ubuntu using this command.
$ sudo apt-get install vim
There are many tutorials out there in the internet to teach you how to be pro in vim. Just go and search and start becoming great in vim.
cp command means ‘copy’. This command helps to copy content from one file to another file and also to another directory. This command can also be used to create a file with copying contents from another file.
Let’s copy content of one file and paste to new file
$ cp existed-file newFile
So we had a file name ‘new.dat’ then
cp new.dat file.dat created a ‘file.dat’ with content from ‘new.dat’.
As we can see we have two files in Desktop. Now, let’s create a directory inside Desktop and copy those files to that directory.
cp fileName directoryName
So we create a directory named ‘new-folder’ inside Desktop then using command
cp file.dat new.dat new-folder, we copy our two files inside that directory. As you can see those two files inside that ‘new-folder’ using
But you cannot cut using
mv command means ‘move’. This command helps to move one or many files from one directory to another directory just like cut and paste. But it also can perform one special function i.e to rename files or directories(folders).
Let’s rename our file ‘new.dat’ to ‘file.dat’ using a
$ mv new.dat file.dat
As you can see from the image above, the file ‘new.dat’ is renamed to ‘file.dat’ using a
Now let’s move that file ‘file.dat’ into new directory.
So first we created a directory named ‘new-folder’ then we moved our file ‘file.dat’ to that directory which is confirmed by
Learning these Linux File Commands will help you to do everything about the files and directories. But don’t read about these commands, you should practice these commands time and again to be memorized and clearly understand. Once you get clear of these file handling Linux terminal commands, We suggest you learning other commands also.