How to manipulate files using the command line on Linux

Estimated reading: 6 minutes 740 views

If it’s just working with files, then there’s not much to say. Currently, there are many graphic tools that effectively support this. Simply with a few mouse clicks, drag, drag, select, and drop and then everything is done. But that’s when manipulating the graphical interface, all of which are intuitive and vivid, and everything is easy to see. With a Linux environment, especially with a server environment, usually, we will not have such a graphical interface. Most server systems have to operate through the command line window, black screen, and white text. At that time, mouse operations were too expensive. We only have a screen, and a keyboard, working directly with the command line. For those who do not often work on the command line system, this is a nightmare, the commands are too hard to remember and too difficult to use. Therefore, this article of mine was born with the purpose of helping people familiarize themselves with some basic commands to manage files in the Linux system.

1. Navigation and exploration commands

  • Locate with pwd command

Usually, when we access a certain Linux server, we will be in the Home directory of the account we access. However, after a while of working, sometimes we cannot remember exactly which folder we are in. And at times like these, we need the pwd command. How to use this command is also very simple, just type: pwd

The result that appears is the current working directory. As shown, my current directory is root located in the root directory /

  • Identify directories with ls command

The ls command is a command used to help us know which files and subdirectories a certain directory contains. When working on the command line, we see nothing but the command prompt. So this command is very useful, it tells us that we can manipulate the exact file or directory that exists. To use this command, simply type ls [target]. target can be a file (used when you want to see basic information about a file), a directory (used when you want to know what files and subdirectories are in that directory), or empty (similar to a directory but in In this case we want to see the information of the current directory)


An example to view the files and subdirectories of the Greencloud folders. The ls command can have the following options:

a: see both hidden files and folders

l: view detailed information including ACL (access control list), size, update date, owner....

p: add slash (/) to mark directories

R: view the whole directory tree
  • Move the directory with cd command

There is nothing to say about this command except its name cd (change directory) is the command used to change the current working directory to another working directory.

2. View the content of a file

  • Create and display entire file contents with Cat

Create a file with Cat Command

Cat helps you quickly create files and write content for them. To do so, use the > sign to pass in the contents of the file.

# cat > filename.txt


The file has been created, now you just need to enter the content and type Enter for each line. Once done, press CTRL+D to exit the file.
To check if the file is created you can use the ls list command:

# ls -l
View the contents of a file with Cat Command

This is the most basic use of the ” cat ” command. No need to set any options, it will open the file and display the content for you right away:

# cat filename

To limit the file call from being too large, you can use | more to output fewer results:

# cat filename | more
  • Pagination with less

The “ less ” command is a necessary addition to cat when viewing the contents of a file. With data pagination, less can help us view the entire contents of a log file. The syntax to use is just less [filename], quite similar to cat


You can use the up, down, or f (scroll forward) and b (scroll backward) navigation keys for page navigation. Also, to exit the view with ” less“, we use q

  • Head and tail to show part of text files

If you only want to see certain parts of the text file in the cat-styled display, use the head and tail commands.

-By default, the head command displays the first 10 lines of a file.

# head filename

But you can modify it to show the first n lines as well.

# head -n filename

-The tail command displays the last 10 lines by default.

# tail filename

But you can modify it to show n lines from the bottom.

# tail -n filename

3. Operations with files and folders

  • Create a directory with mkdir command

The mkdir command is used to create a folder file or use the -p option to create an entire folder tree

As in the picture, we have created 1 folder “Greencloud” and a folder tree “Greencloud1/Greencloud2”

  • Create files with touch

Although there are many commands that can be used to create files, the most basic command is still touch, which creates a file with the specified name and path. The basic syntax is touch [filename_with_path]. If there is only a filename, the default file will be created in the current directory. You can create multiple files at once with “touch”

– Create a file named VPS1.txt located in the folder Greencloud

-Create a file named VPS2.txt located in the current /root folder

-Create multiple files at once

  • Move or rename files and folders with mv

The mv command has two effects: move (move a file or folder to another folder) or rename (rename the file or folder). The general syntax is mv file_or_dir1[, file_or_dir2, …] new_location
-Move 4 files VPS2,3,4,5.txt to the Greencloud folder


-Move the VPS1.txt file in the Greencloud folder to the Greencloud1 folder

  • Copy files or folders with cp

The cp command is used to copy a file or directory, or in other words, make a copy of a certain file or directory. Syntax to use: cp file_or_dir target_location. If you want to copy the entire directory tree, you need to use the -r option.
-Copy Greencloud folder to Greencloud1 folder

  • Delete files or folders

To delete a file, use the rm command, and to delete a directory, use the rmdir command. However, rmdir has a limitation that it can only delete empty directories. To delete an entire directory tree, we need to use rm with the -r option.

Note: If you are familiar with using Linux, there is a command listed as dangerous commands that absolutely should not be manipulated, which is rm -rf /. When executing this command, all directory tree from root will be deleted (-r), and not asked again (-f). If you're not sure, it's best not to use the -r option, or maybe use -i (will ask before deleting to be sure).

Good Luck!

Leave a Comment

Share this Doc

How to manipulate files using the command line on Linux

Or copy link

CONTENTS