We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsHosting ServicesCloud Servers

Basic Linux Navigation and File Management

Basic Linux Navigation and File Management

In case you are not an experienced user of Linux command line, you will definitely have problems while navigating through your hard disk. This tutorial will explain you the basics of file management and will help you to improve your orientation in the system. It may not cover full management and navigation procedures inside your file system, but this tutorial will build a strong basis for understanding the main principles of the system. For your own sake, do not hesitate to read it twice in case something remains unclear.
First, log in to your Linux VPS server. If you are not sure how to start working with you server, you should read the introduction to Linux terminal, which explains the very basics of Linux terminal.

In this part of the tutorial you will learn how to move around your file system and gather information about files and folders that are around you.

Knowing where you are

After you log in to your VPS server using PuTTY or Terminal, you will automatically be dropped in to your user’s home directory – /home/USERNAME or /root if you are logged in as root. Type pwd (present working directory) command in order to see the actual position of yours:

root@server:~# pwd

As you can see from the example above, you already are in the /root folder, which is located in the top level directory. The slash “/” sign marks top-level directory (“root”), or default mount point, of your VPS hard disk.

Gathering information

Now you know where exactly you are, so the next step would be to get information about the files that are around you. For this, use ls command, as it is designed for listing files and folders:

root@server:~ # ls
dir1 file1 file2

You can see that there are three files in the current directory, but this command does not show any additional information about file type, creation date, size or etc. You need to use an additional option –l (for “long” output) in order to gather such information. Type ls –l:

root@server:~ # ls -l
total 12
drwxr-xr-x 2 root root 4096 Oct 12 11:55 dir1
-rw-r–r– 1 root root 6 Oct 12 11:55 file1
-rw-r–r– 1 root root 9 Oct 12 11:55 file2

Now you can see all the information about files you may need to know. The first sign/letter means the type of file. For example, the first letter for dir1 is d (drwxr-xr-x) which shows us that this is not a single file, but a whole directory. Other information about files and folders, such as permissions (rwxr-xr-x), owner (root root), file size (4096), creation/modification date (Oct 12 11:55) and file name (dir1) are also visible using –l option.
You can also add –a option, which allows you to see all files, including dot (hidden) files. Type ls –la in order to see all files:

root@server:~ # ls -la
total 20
drwxr-xr-x 3 root root 4096 Oct 12 12:03 .
drwx—— 6 root root 4096 Oct 12 11:55 ..
-rw-r–r– 1 root root 0 Oct 12 12:03 .hidden_file
drwxr-xr-x 2 root root 4096 Oct 12 11:55 dir1
-rw-r–r– 1 root root 6 Oct 12 11:55 file1
-rw-r–r– 1 root root 9 Oct 12 11:55 file2

You can get familiar with file types, permission types, groups and ownership by reading the tutorial about Linux permissions.

Navigating around

Since you know what is surrounding your home directory, it is time to check what files are on other directories. You will have to change the folder of your current positions using cd (change directory) command. The command ls must be used with additional arguments in order to change the path. First, move to your top level directory (/):

cd /

Check what files and folders are located here:

root@server:/# ls -la
total 104
drwxr-xr-x 23 root root 4096 Sep 24 10:54 .
drwxr-xr-x 23 root root 4096 Sep 24 10:54 ..
drwxr-xr-x 91 root root 4096 Sep 30 10:36 etc
-rw-r–r– 1 root root 0 Jun 15 12:43 fastboot
drwxr-xr-x 2 root root 4096 Jun 15 12:43 home
drwxr-xr-x 2 root root 4096 Jun 15 12:43 opt
drwx—— 6 root root 4096 Oct 12 11:55 root
drwxr-xr-x 12 root root 4096 Jun 15 12:45 var

There are many files and folders in the top-level directory and you can change your current directory to any of these. Go to /var/log directory:

cd /var/log

There are two types of paths used in Linux systems – absolute and relative paths. The absolute path has been given for changing directory to /var/log. The absolute path is defined as specifying the location of a file or directory from the root directory (/). Relative path is a path which is given from the present working directory (pwd).
For example, you are in /var/log folder and you want to change your working directory to apache2 directory. You can use relative path for moving by typing cd apache2 or absolute pathcd /var/log/apache2. In both ways you will get to the same directory, but different types of paths will be used.
Also, if you want to go to the parent directory of the current location, you have to use cd command with a special double dot indicator. For example, you are now in the /var/log/apache2 directory, so cd .. will navigate you to /var/log directory.
Check current path:

root@server:/var/log/apache2# pwd

Type cd .. and check the path afterwards:

root@server:/var/log/apache2# cd ..
root@server:/var/log# pwd

In order to get back to your home directory (~), this command can be used alone, without additional arguments.

Reading File Content

You have already learned how to navigate through the file system and retrieve information about files and folders, now it is time to find out what is written in the files. There are plenty of different commands to read file content, but in this tutorial you will get familiar only with a few of them.
The command cat is the most popular command for reading files in Linux operating system. It dumps all the content of the file to the command line. It is the easiest way to display the contents of a file at the command line, however this command cannot be used for reading big data files as it is not convenient for the user.
Usage of the command:

cat /var/log/messages

As it was mentioned before, using cat is not convenient while reading big files, so there are commands – less and more – which cut bigger files into parts if it does not fit to the screen all at once. Actually these two commands are the same, except that the command more allows the user to move only down, but does not support moving up. It is recommend to use less command for reading bigger files because it allows you to move in both directions and has many powerful features.
Usage of less command:

less /var/log/messages

Once the command is initiated, you will see the part of text that fits to your screen. You can scroll down and up line by line using arrow keys on your keyboard or scroll down the whole “page” using space bar, “Page Down” or CTRL + F button combination. “Page Up” and CTRL + B will scroll up the page.
Less command also has a feature for searching for particular text in the whole document. It can be done by typing “/” sign and the text you want to search for. For example, if you want to search for “kernel”, you should write:
Hit “Enter” button on your keyboard. The program will search for the text that matches your request through the whole document and will stop at the first result. If there is more than one result, you can go through results by typing lower-case “n” letter (the capital “N” letter is used to move back to the previous result).
You can quit from the document by typing “q” letter. Also do not forget that by typing “h” or “?” you will get a summary of all available commands.
In this tutorial you have learned how to use cat and less commands for reading file content. However, there are more programs, which may be even more useful for you:
Head – Displays first 10 lines of the file.
Tail – Displays the last 10 lines of the file, the command tailf may be used to track changes on the log files.

Since you are familiar with the files and folders that surround you, it is time to start learning how to create, edit, move, copy and remove files and folders.

Editing Files

Reading file content is not enough, so you may also want to edit the files in your Linux system. The most popular text editor is vi, which is a built-in text editor in all Linux distributions. Vi is an advanced text editor, which requires a little bit of knowledge before opening it – many newbies in Linux system are struggling with this text editor.
Open the file using vi text editor:

vi file1

By default, you will not be allowed to edit the file. INSERT button on your keyboard enables file editing. Edit the file and click ESC button after you are done – it will disable editing. One of the hardest parts for beginners is to exit vi text editor. It can be done by hitting SHIFT + Z twice.
NOTE: Do not forget to disable editing mode before trying to close the editor.
New users can find it difficult to use vi text editor. However, it is not the problem since you have root access to your VPS server. We suggest you to install nano text editor that does not require additional knowledge for using it. Installation of nano:

apt-get install nano


yum install nano

Open the file using nano text editor:

nano file1

Sample output:

GNU nano 2.2.6 File: file1
Text Text Text
Text Text Text
[ Read 75 lines ]
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell

On the top of the screen you will see the version of nano text editor and filename of the file you are editing. On the bottom of the screen you will see short usage examples of main commands. The Sign ^ stands for CTRL button, so you will have to hit CTRL+V (^V) if you want to go to the next page.
If you have made any changes on the file, you can save the file by hitting CTRL+O (^G), or you simply will be asked if you want to save the changes when you try to exit the text editor – CTRL+X (^X):

Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES) ?
Y Yes
N No ^C Cancel

In order to see full command list of this text editor, try to hit CTRL+G (^G).
In this tutorial you have learned the basics of using vi and nano text editors. However, do not forget that there are many other text editors, such as jed, pico, gedit or vim, that may be more comfortable for you to work with.

Creating new files and folders

In previous sections you have already learned how to read and edit an existing file. In this section you will learn how to create new files and folders in Linux operating systems.
Directories in Linux operating systems can be easily created using mkdir command:

mkdir my_new_directory

You can also create more directories using one command. However, you must tell the system that it must create any directories necessary to construct a given directory path. It can be done by using –p option:

mkdir –p my_new_directory/new_directory_inside/

Go to your newly created directory and create a new file in it. The most basic method of creating a new file is to use touch command:

cd my_new_directory/new_directory_inside/
touch my_new_file

Check your newly created file:

root@server:~# ls -la
total 3
-rw-r–r– 1 root root 0 Oct 13 14:42 my_new_file

This command creates an empty file with 644 permissions and file ownership of the creation in the present working directory.
You can alternatively create a new file just by editing it, for example:

nano my_another_file

This will also create a new file, however, you cannot save it without entering any data and saving the changes.

Moving and copying files and folders

To copy and move files and folders to other places is very easy, but also requires responsibility as you can overwrite an existing file and lose important data permanently.
Files can be moved from one to another location using mv command, for example:

mv my_new_file my_new_directory/new_directory_inside/

This will move my_new_file to my_new_directory/new_directory_inside/ and so on. Folders, differently than files, must be moved using an additional command: -r, which means recursively. This additional option lets the system know that it must move all the files that are inside the directory:

mv -R my_new_directory/ /home/

This will move the whole my_new_directory with all the directories and files inside it to the /home folder.
Mv command is also used for renaming files and directories. If you want to rename my_new_file to my_old_file, you have to execute the following command:

mv my_new_file my_old_file

The same command also works for renaming directories.
Copying files from one to another location works in the same way as removing, you just have to use a different command. The command cp stands for copying in Linux systems. The file my_old_file can be easily copied to a different directory as follows:

cp my_old_file /home/my_new_directory/

The command above will copy my_old_file to /home/my_new_directory folder. The whole directory can be copied using the following command:

cp –R /home/my_new_directory/ /my_new_folder/

The directory my_new_directory will be copied with all its content to /my_new_folder/ directory.
Removing files and folders
Unnecessary files can be removed using rm command, while removing directories needs rmdir to be used.
Regular file can be removed as follows:

rm my_old file

The directories can be removed with rmdir. However, they must be empty, otherwise you will not be allowed to do that. You must execute the following command in order to remove an empty directory:

rmdir my_empty_folder/

If you want to delete the directory which have files inside of it, you have to use rm command with additional option –r (recursively):

rm –r my_not_empty_folder/

NOTE: You should be very careful while using this command because removed files and folders cannot be recovered.

After reading this tutorial you should be able to navigate around your Linux system easily, gather the information about files and folders that are around you and see their content. Also, you have learned how to manage files and folders and edit them using two powerful text editors – nano and vi.

Rate this Tutorial:
No CommentsLeave a Comment

Other (3)

Popular Keywords