Directory file in linux terminal

How to change directory in Linux terminal

M y Dell Laptop came preinstalled with Ubuntu Linux, and I am a new Linux desktop user. How do I change directories in the Linux terminal?

Introduction – On Linux the cd command allows you to change directories when using the terminal application. This page shows how to change directory in Linux terminal using the cd command.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Est. reading time 3 minutes

How to change directory in Linux terminal

  1. To return to the home directory immediately, use cd

OR cd

  • To change into the root directory of Linux file system, use cd / .
  • To go into the root user directory, run cd /root/ as root user.
  • To navigate up one directory level up, use cd ..
  • To go back to the previous directory, use cd —
  • Let us see all examples and usage for terminal in details.

    How to use the Linux command line to change directory or folder

    The directory in which the user is currently working is called the current working directory (CDW).

    How to print the current working directory in Linux

    To display the name of the current/working directory, type the following pwd command:
    pwd

    cd command in Linux termianl

    The syntax is:
    cd
    cd ..
    cd /path/to/dir
    When cd command used without stipulating any directory name, cd command returns to the home directory. Let us change the directory to /usr/sbin/, run:
    cd /usr/sbin/
    Verify it:
    pwd
    Want to list the files in the /usb/sbin/ directory? Try the ls command:
    ls
    ls -l
    Let us go back to user’s home directory, run:
    cd
    Again verify it:
    pwd

    • No ads and tracking
    • In-depth guides for developers and sysadmins at Opensourceflare✨
    • Join my Patreon to support independent content creators and start reading latest guides:
      • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
      • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
      • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
      • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
      • How to protect Linux against rogue USB devices using USBGuard

    Join Patreon

    Absolute vs Relative pathname

    The cd command changes the current directory when a directory name provided by the user. The name can be written as an absolute pathname (e.g. cd /etc/httpd/ ) or as local pathname relative to the root directory (e.g. cd conf.d/ ). For example:
    cd /etc/httpd/
    pwd
    ls
    cd conf.d/
    pwd
    ls

    The absolute vs. relative pathname for cd command in Linux

    Understanding . and .. directories

    On Linux the current directory is represented by a single dot ( . ) and two consecutive dots represent its parent directory ( .. ). Thus, to change to the parent of the current directory, run cd . .. For example:
    ls
    pwd
    cd ..
    pwd
    ls

    How can I return directly to my home directory when using the Linux terminal?

    How do I change directories in the Linux terminal and return to the previous directory?

    Simply pass the — option to the cd:
    cd —
    Verify it:
    pwd

    The -P option instructs cd to use the physical directory structure instead of following symbolic links:
    cd -P LinkDir
    pwd
    The -L option forces cd to follow symbolic links:
    cd -L LinkDir
    pwd

    Linux cd command cheat sheet

    Table 1: cd command under Linux
    Command Description
    cd Returns you to your login directory
    cd

    Also returns you to your login directory cd — Returns you to your previous working directory cd / Takes you to the entire system’s root directory. cd /root Takes you to the home directory of the root user. You must be the root user to access this directory. cd /home Takes you to the home directory, where user login directories are usually stored cd .. Takes you to the directory one level up. cd

    tom Takes you to tom’s home directory, if tom user has granted you permission cd /path/to/dir/ Take you to the /path/to/dir/ directory cd dir* Use a wildcard to change the directory name

    Conclusion

    The cd command is used to change the current directory in both Linux and other Unix-like systems. See Cd command wiki page.

    🐧 Get the latest tutorials on Linux, Open Source & DevOps via

    Источник

    More Linux resources

    You probably learned how to interact with a computer using a GUI, and you’re probably very good at it. You may be surprised to learn, then, that there’s a more direct way to use a computer: a terminal, or shell, which provides a direct interface between you and the operating system. Because of this direct communication without the intervention of additional applications, using a terminal also makes it easy to script repetitive tasks, and design workflows unique to your own needs.

    There’s a catch, however. As with any new tool, you have to learn the shell before you can do anything useful with it.

    This article compares navigating a computer desktop without the desktop. That is, this article demonstrates how to use a terminal to move around and browse your computer as you would on a desktop, but from a terminal instead.

    While the terminal may seem mysterious and intimidating at first, it’s easy to learn once you realize that a terminal uses the same information as all of your usual applications. There are direct analogs for everything you do in a GUI to most of the everyday activities you do in a terminal. So instead of starting your journey with the shell by learning terminal commands, begin with everyday tasks that you’re already familiar with.

    View file lists

    To list the files on your computer or device, you generally open a file manager application, whether it’s called Explorer (Windows), Finder (Mac), Nautilus (GNOME), Amaze (Android), or anything else.

    The ls (list) command lists all files in the current directory.

    The pwd (print working directory) command tells you what directory you’re currently in. From there, the ls (list) command shows you what’s in that (or any other) directory:

    The first items listed are dots. The single dot is a meta-location, meaning the folder you are currently in.

    The double dot is an indicator that you can move back from this location. That is, you’re in a folder inside of another folder. Once you start moving around within your computer, you can use that information for reference.

    You may also notice that it’s hard to tell a file from a folder. Some Linux distributions have colors pre-programmed so that folders are blue, files are white, binary files are green, and so on. If you don’t see those colors, you can use ls —color to try and activate that feature. Colors don’t always transmit over remote connections to distant servers, though, so a common and generic method to make it clear what are files and what are folders is the —classify ( -F ) switch:

    Folders are given a trailing slash ( / ) to denote that they are directories. Binary entities, like ZIP files and executable programs, are indicated with an asterisk ( * ). Plain text files are listed without additional notation.

    If you’re used to the dir command from Windows, you can use that on Linux as well. It works exactly the same as ls .

    Open a folder

    Double-click on a folder. When it opens, you are «in» that folder.

    The cd (change directory) command opens a folder and makes it your new current working directory.

    To open—or enter—a folder on the command line, use the cd (change directory) command as follows:

    Close a folder

    Close the desktop window you’re in, or press the Back button in your file manager to leave the folder.

    You don’t so much close a folder on the command line, as you leave it.

    On a desktop, you judge your current location by what window you have open. For instance, when you open a window and click on the Documents folder icon, you think of yourself as being in your Documents folder.

    In a terminal, the closest thing to this concept is the shell prompt. In most shells, your prompt is a dollar sign ( $ ), and its location within the computer can change depending on where you tell your terminal to go. You can always learn your current location with the pwd (print working directory) command:

    If you’re in one location because you used the cd command, you can «close» that location by going back to your home directory. This directory is, more or less, your terminal’s desktop—it’s the place you find yourself staring at when you first open the terminal.

    The command for returning home is the cd command with no location specified (shorthand for cd

    Open a window, double-click on a folder, and then double-click on a sub-folder. Use the Back button to backtrack.

    The cd (change directory) command moves you into a different directory. To move out of that directory, use cd along with the path to some other location, or use double dots to backtrack, or return home to navigate from there.

    Navigating a Linux computer is like navigating the internet. The very concept of a URL is pulled directly from UNIX. When you navigate to a specific page on a website, like https://www.redhat.com/en/topics/linux, you’re actually changing directory to /var/www/redhat.com/en/topics/linux (this isn’t exactly true for pages built by PHP and other dynamic languages, but even they are essentially building a virtual file system).

    To go back a page in this example, delete the linux part of the URL. You’re taken to a new location, the parent directory, containing a different file for you to view. Because this happens inside your web browser, you probably don’t think of it as navigating a computer, but you use the same principle in a Linux terminal.

    Think of your computer as the internet (or the internet as a computer, more appropriately). If you start in your home folder, then all of your personal files can be expressed using your home as the starting point. Think of your home folder as a web URL’s domain. Instead of a URL, the term directory path or file path is used. Here are some example paths:

    • /home/seth/bin
    • /home/seth/despacer.sh
    • /home/seth/documentation.zip*
    • /home/seth/people

    Because you return home often, your home directory can be abbreviated as

    To navigate directly to the people folder, use the cd command along with the entire directory path:

    Suppose that inside the people folder, there are the directories developers and marketing .

    Now that you’re inside the people directory, you can move out of it in one of three different ways.

    One option is to navigate into a different directory from where you are now. This method uses a dot as your starting point.

    Remember that a dot is a meta-location, meaning «where I am right now.» This method is akin to, for instance, manually adding a level in a URL, such as changing https://www.redhat.com/en/topics to https://www.redhat.com/en/topics/linux. So, to change to the developers directory from your current location, do the following:

    You could move through all of your directories this way: change directory to one folder, list its contents, and then move into the next one, and so on. However, if you know the path of where you want to go, you can transport yourself there instantly all in one command. To reach the to /home/seth/people/developers directory instantly from anywhere, instantly:

    Once in a directory, you always have the option to backtrack out of your current location using the meta-location .. to tell cd to take you up one folder:

    You can keep using this trick until you have nowhere left to go:

    You can also always return to your home directory instantly using this shortcut:

    Because users go home often, most shells are set to go back home should you type cd with no destination:

    Absolute paths

    File paths technically start at the very root of your computer’s file tree. Even your home directory starts at the very bottom of the tree. This fact is significant because system administrators deal with lots of data that exists outside of their own home directory.

    When you go as far back in a file path you can go, you reach the root directory, represented by a single slash ( / ). You see the root directory at the beginning of all absolute paths:

    When in doubt, you can always use the absolute path to any location:

    To find where you want to go, use the ls command to «open» a directory and look inside:

    Conclusion

    Try navigating through your system using the terminal. As long as you restrict yourself to the cd , ls , and pwd commands, you can’t do any harm, and the practice will help you get comfortable with the process. On most systems, the Tab key auto-completes file paths as you type, so if you’re changing to

    /people/marketing , then all you need to type is cd

    /people/m and then press Tab. If Tab is unable to complete the path, you know that you either have the wrong path or there are several directories with similar names, so your shell is unable to choose which to use for auto-completion.

    Navigating in the terminal takes practice, but it is far faster than opening and closing windows, and clicking on Back buttons and folder icons, especially when you already know where you want to go. Give it a try!

    Источник

    Читайте также:  Dwg viewer для mac os free
    Оцените статью