Linux File Commands for Beginners

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:

  • Press Ctrl + Alt + T
  • Press Alt + F2 and type gnome-terminal
  • Press Window Key and search for Terminal

touch command

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.

Usage:

touch fileName

e.g

$ touch new.txt
touch command linux

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
touch command linux for multiple file creation

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.

touch command changes the timestamp of a 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

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.

Usage:

cat > fileName

e.g

$ 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.

cat command to creat a file and write

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.

cat command to read

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’.

cat command to append content from one file to another

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.

Usage

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.

head command usage in Linux
tail command use in Linux

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 head and tail commands. If you want control over the number of lines to display, you can use -n value option to change that.

for e.g

$ head -n 20 new.dat
more command with -n option in Linux

Same option can also be used with tail command.

tail command with -n option

more or less command

more and 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 more or less command we can view the whole content navigating through page or line at a time.

With 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.

more command use

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.

less command uses

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.

To install nano in Ubuntu:

$ sudo apt-get install nano

After installing 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 nano fileName.

nano text editor
nano text editor interface

Press 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

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
cp command to copy content to new file

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.

Usage:

cp fileName directoryName
cp command to copy to directory

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 ls command.

But you cannot cut using cp command.

mv command

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 command.

$ mv new.dat file.dat
mv command to rename

As you can see from the image above, the file ‘new.dat’ is renamed to ‘file.dat’ using a mv command.

Now let’s move that file ‘file.dat’ into new directory.

mv command to move files to directory

So first we created a directory named ‘new-folder’ then we moved our file ‘file.dat’ to that directory which is confirmed by ls command.

Conclusion

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.

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *