How to navigate in linux

A beginner’s guide to navigating the Linux filesystem

As a new Linux user, one of the first skills that you need to master is navigating the Linux filesystem. These basic navigation commands will get you up to speed.

Navigating the Linux file system

The basics

Great Linux resources

Before we get into commands, let’s talk about important special characters. The dot ( . ) , dot-dot ( .. ) , forward slash ( / ), and tilde ( ~ ), all have special functionality in the Linux filesystem:

  • The dot ( . ) represents the current directory in the filesystem.
  • The dot-dot ( .. ) represents one level above the current directory.
  • The forward slash ( / ) represents the «root» of the filesystem. (Every directory/file in the Linux filesystem is nested under the root / directory.)
  • The tilde ( ~ ) represents the home directory of the currently logged in user.

I believe that the best way to understand any concept is by putting it into practice. This navigation command overview will help you to better understand how all of this works.

The pwd command prints the current/working directory, telling where you are currently located in the filesystem. This command comes to your rescue when you get lost in the filesystem, and always prints out the absolute path.

What is an absolute path? An absolute path is the full path to a file or directory. It is relative to the root directory ( / ). Note that it is a best practice to use absolute paths when you use file paths inside of scripts. For example, the absolute path to the ls command is: /usr/bin/ls .

The results of typing pwd in your terminal window.

If it’s not absolute, then it’s a relative path. The relative path is relative to your present working directory. If you are in your home directory, for example, the ls command’s relative path is: . ./../usr/bin/ls .

Change directory ( cd )

The cd command lets you change to a different directory. When you log into a Linux machine or fire up a terminal emulator, by default your working directory is your home directory. My home directory is /home/kc . In your case, it is probably /home/ .

Absolute and relative paths make more sense when we look at examples for the cd command. If you need to move one level up from your working directory, in this case /home , we can do this couple of ways. One way is to issue a cd command relative to your pwd :

Читайте также:  Editor for programming linux

Note: Remember, .. represents the directory one level above the working directory.

The other way is to provide the absolute path to the directory:

Either way, we are now inside /home . You can verify this by issuing pwd command. Then, you can move to the filesystem’s root ( / ) by issuing this command:

If your working directory is deeply nested inside the filesystem and you need to return to your home directory, this is where the ~ comes in with the cd command. Let’s put this into action and see how cool this command can be. It helps you save a ton of time while navigating in the filesystem.

My present working directory is root ( / ). If you’re following along, yours should be same, or you can find out yours by issuing pwd . Let’s cd to another directory:

$ cd /home/kc/0/1/2/3/4/5/6/7/8/9 $ pwd /home/kc/0/1/2/3/4/5/6/7/8/9 

To navigate back to your home directory, simply issue ~ with the cd command:

Again, check your present working directory with the pwd command:

The dash ( — ) navigates back to the previous working directory, similar to how you can navigate to your user home directory with ~ . If you need to go back to our deeply nested directory 9 under your user home directory (this was my previous working directory), you would issue this command:

The output of using the tilde (~) as a filesystem navigation shortcut.

Wrapping up

These commands are your navigation tools inside the Linux filesystem. With what you learned here, you can always find your way to the home (~) directory. If you want to learn more and master the command line, check out 10 basic Linux commands.

Want to try out Red Hat Enterprise Linux? Download it now for free.

Источник

How do I Navigate in Linux Terminal?

The command terminal on Linux is a CLI (Command-Line Interface), where you type the commands that would otherwise take time with the GUI. It is equivalent to the Command Prompt (CMD) provided in Windows. The terminal is the perfect way a user can truly feel that they are communicating with their system. It feels invigorating when you type commands on the terminal, and the computer performs exactly what you instructed it to do. The terminal is also great for people who want to know what exactly is going on deep inside the computer. You can understand the pathways and the steps taken by the computer to perform even the very basic tasks. In the end, it all comes down to preference.

To truly master the terminal, you first need to learn the basic navigation on it. We will guide you through the commands used to navigate the files and directories present on your Linux system. So, let’s get started.

Читайте также:  Обновить firefox в линуксе

The “pwd” Command

The Print Working Directory (pwd) command is one of the basic and primary commands users need to learn about. When you open the terminal, you are usually present in the home directory. If you execute any commands, you likely end up in a directory different from the one you started at. To know which directory you are currently in, you can use the pwd command.

The pwd command tells you about the directory you have ended up in. Knowing your whereabouts is crucial in a CLI environment. Hence pwd is important. The syntax for the command is simple; you just need to type:

You should get your directory in output.

The “cd” command

As mentioned earlier, you start in the home directory when you open the terminal. To move around the system to access files and folders, you must continuously change directories. For this purpose, we have the “Change Directory” or the “Cd” command. The name of the command explains its purpose. It is used for shifting from one directory to another.

The basic syntax for this command is:

For example, you must type the following if you want to access the “Downloads” folder.

Or you could access it with the following.

To go to the home directory, use this command:

To go to the previous directory, do this:

To go upwards in a directory, we use “../” with the command. For example, it works that you are currently in the “home/user/Downloads” directory. To move to “home/user” you need to type this:

For getting into the home directory, add another “../”

These are the basic variety of commands associated with Cd.

The “ls” Command

Now, as you have learned to move from one directory to another, the next step is to view the contents of the directory you are in. The ls command is used to view all the files and folders present in the current directory.

The syntax for ls is as follows.

There are different options available with ls. For example, to view all the hidden files in the current directory, we use -a in tandem with ls.

If you prefer to see the contents of the directory in an extended form.

“l” over here signifies “long”.

To view files arranged according to time of modification, type the following.

The “mkdir” Command

To create new directories, we use the “Make Directory” or the “mkdir” command. The syntax for this command is given below.

For example, to make a directory named “wire” in your home directory.

Make sure you are in your home folder.

To make an extended directory, use -p with mkdir.

Читайте также:  Аналог kali linux android

The “touch” Command

You have learned how to make a directory. Now it is time to learn how we can make files inside a directory. Step up the “touch” command. The syntax for touch is given below.

For example, just use the following to create a file named “doc1” inside any directory.

To create multiple files at once in the same directory, type the following.

You can also do this by using the absolute address.

We can use the touch command to create files in any directory if we use their absolute addresses.

The “mv” Command

So far, we have learned how to make directories and files, move within directories, tell which directory we are in, and see the contents of a directory. Now we move on to moving files within directories and to and from a directory. “Move” or “mv” is used for this purpose, and the syntax for this command is given below.

For example, we type the following to move a file named “ball” to a directory named “wire”.

If you want to move the file “ball” to your home directory, use the “.” operator at the end.

Make sure you are in the home directory before you try and execute this command.

The “cp” Command

To copy files and directories into others, we use the “cp” command. “cp” stands for “Copy” and its syntax is:

For example, to copy the file named ball into another file named ball1, type.

Cp created a file named ball 1 and copied the contents of the ball into ball1.

To copy directories, we use “-r” in addition to cp. It stands for “recursive”. The syntax for this is:

The “rm” and “rmdir” Commands

We use “rm” and “rmdir” for removing files and directories, respectively. The syntax for these two commands is as follows.

Let’s remove the file “ball” and the directory “wire” we created previously. Make sure you are present in the directory containing both items.

Conclusion

This article went through the ways you can navigate through the terminal in Linux. The commands that were gone through in this guide are the basic ones everyone needs to know about. If you master these commands first, the transition of doing much more complex tasks using the terminal should become easier. You should practice these commands continuously, and you should also try to play with them a bit. It should enable you to learn new tricks making it easier to do certain tasks on the terminal.

About the author

Zeeman Memon

Hi there! I’m a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.

Источник

Оцените статью
Adblock
detector