What command will move you from the current directory to the one above it?

Use a single command to navigate multiple steps in your directory structure, including moving backwards (one level up).

  • Perform operations on files in directories outside your working directory.

  • Work with hidden directories and hidden files.

  • Interconvert between absolute and relative paths.

  • Employ navigational shortcuts to move around your file system.

  • Moving around the file system

    We’ve learned how to use

    $ cd ..
    
    0 to find our current location within our file system. We’ve also learned how to use
    $ cd ..
    
    1 to change locations and
    $ cd ..
    
    2 to list the contents of a directory. Now we’re going to learn some additional commands for moving around within our file system.

    Use the commands we’ve learned so far to navigate to the

    $ cd ..
    
    3 directory, if you’re not already there.

    $ cd
    $ cd shell_data
    $ cd untrimmed_fastq
    

    What if we want to move back up and out of this directory and to our top level directory? Can we type

    $ cd ..
    
    4? Try it and see what happens.

    $ cd shell_data
    

    -bash: cd: shell_data: No such file or directory
    

    Your computer looked for a directory or file called

    $ cd ..
    
    5 within the directory you were already in. It didn’t know you wanted to look at a directory level above the one you were located in.

    We have a special command to tell the computer to move us back or up one directory level.

    $ cd ..
    

    Now we can use

    $ cd ..
    
    0 to make sure that we are in the directory we intended to navigate to, and
    $ cd ..
    
    2 to check that the contents of the directory are correct.

    $ pwd
    

    /home/dcuser/shell_data
    

    $ ls
    

    sra_metadata  untrimmed_fastq
    

    From this output, we can see that

    $ cd ..
    
    8 did indeed take us back one level in our file system.

    You can chain these together like so:

    $ ls ../../
    

    prints the contents of

    $ cd ..
    
    9.

    Finding hidden directories

    First navigate to the

    $ cd ..
    
    5 directory. There is a hidden directory within this directory. Explore the options for
    $ cd ..
    
    2 to find out how to see hidden directories. List the contents of the directory and identify the name of the text file in that directory.

    Hint: hidden files and folders in Unix start with

    $ pwd
    
    2, for example
    $ pwd
    
    3

    Solution

    First use the

    $ pwd
    
    4 command to look at the options for
    $ cd ..
    
    2.

    $ man ls
    

    The

    $ pwd
    
    6 option is short for
    $ pwd
    
    7 and says that it causes
    $ cd ..
    
    2 to “not ignore entries starting with .” This is the option we want.

    $ cd shell_data
    
    0

    $ cd shell_data
    
    1

    The name of the hidden directory is

    $ pwd
    
    9. We can navigate to that directory using
    $ cd ..
    
    1.

    $ cd shell_data
    
    2

    And then list the contents of the directory using

    $ cd ..
    
    2.

    $ ls
    

    $ cd shell_data
    
    4

    The name of the text file is

    /home/dcuser/shell_data
    
    2.

    In most commands the flags can be combined together in no particular order to obtain the desired results/output.

    $ cd shell_data
    
    5

    Examining the contents of other directories

    By default, the

    $ cd ..
    
    2 commands lists the contents of the working directory (i.e. the directory you are in). You can always find the directory you are in using the
    $ cd ..
    
    0 command. However, you can also give
    $ cd ..
    
    2 the names of other directories to view. Navigate to your home directory if you are not already there.

    $ cd shell_data
    
    6

    Then enter the command:

    $ cd shell_data
    
    7

    sra_metadata  untrimmed_fastq
    

    This will list the contents of the

    $ cd ..
    
    5 directory without you needing to navigate there.

    The

    $ cd ..
    
    1 command works in a similar way.

    Try entering:

    $ cd shell_data
    
    9

    This will take you to the

    /home/dcuser/shell_data
    
    8 directory without having to go through the intermediate directory.

    Navigate to your home directory. From there, list the contents of the

    /home/dcuser/shell_data
    
    8 directory.

    Solution

    -bash: cd: shell_data: No such file or directory
    
    0

    -bash: cd: shell_data: No such file or directory
    
    1

    Full vs. Relative Paths

    The

    $ cd ..
    
    1 command takes an argument which is a directory name. Directories can be specified using either a relative path or a full absolute path. The directories on the computer are arranged into a hierarchy. The full path tells you where a directory is in that hierarchy. Navigate to the home directory, then enter the
    $ cd ..
    
    0 command.

    -bash: cd: shell_data: No such file or directory
    
    2

    You will see:

    -bash: cd: shell_data: No such file or directory
    
    3

    This is the full name of your home directory. This tells you that you are in a directory called

    $ ls
    
    2, which sits inside a directory called
    $ ls
    
    3 which sits inside the very top directory in the hierarchy. The very top of the hierarchy is a directory called
    $ ls
    
    4 which is usually referred to as the root directory. So, to summarize:
    $ ls
    
    2 is a directory in
    $ ls
    
    3 which is a directory in
    $ ls
    
    4. More on
    $ ls
    
    8 and
    $ ls
    
    3 in the next section.

    Now enter the following command:

    -bash: cd: shell_data: No such file or directory
    
    4

    This jumps forward multiple levels to the

    $ pwd
    
    9 directory. Now go back to the home directory.

    $ cd shell_data
    
    6

    You can also navigate to the

    $ pwd
    
    9 directory using:

    -bash: cd: shell_data: No such file or directory
    
    6

    These two commands have the same effect, they both take us to the

    $ pwd
    
    9 directory. The first uses the absolute path, giving the full address from the home directory. The second uses a relative path, giving only the address from the working directory. A full path always starts with a
    $ ls
    
    4. A relative path does not.

    A relative path is like getting directions from someone on the street. They tell you to “go right at the stop sign, and then turn left on Main Street”. That works great if you’re standing there together, but not so well if you’re trying to tell someone how to get there from another country. A full path is like GPS coordinates. It tells you exactly where something is no matter where you are right now.

    You can usually use either a full path or a relative path depending on what is most convenient. If we are in the home directory, it is more convenient to enter the full path. If we are in the working directory, it is more convenient to enter the relative path since it involves less typing.

    Over time, it will become easier for you to keep a mental note of the structure of the directories that you are using and how to quickly navigate amongst them.

    Relative path resolution

    Using the filesystem diagram below, if

    $ cd ..
    
    0 displays
    sra_metadata  untrimmed_fastq
    
    5, what will
    sra_metadata  untrimmed_fastq
    
    6 display?

    1. sra_metadata  untrimmed_fastq
      
      7
    2. sra_metadata  untrimmed_fastq
      
      8
    3. sra_metadata  untrimmed_fastq
      
      9
    4. $ ls ../../
      
      0

    What command will move you from the current directory to the one above it?

    Solution

    1. No: there is a directory
      $ ls ../../
      
      1 in
      $ ls ../../
      
      2.
    2. No: this is the content of
      $ ls ../../
      
      3, but with
      $ cd ..
      
      8 we asked for one level further up.
    3. No: see previous explanation. Also, we did not specify
      $ ls ../../
      
      5 to display
      $ ls
      
      4 at the end of the directory names.
    4. Yes:
      $ ls ../../
      
      7 refers to
      $ ls ../../
      
      8.

    The root directory is the highest level directory in your file system and contains files that are important for your computer to perform its daily work. While you will be using the root (

    $ ls
    
    4) at the beginning of your absolute paths, it is important that you avoid working with data in these higher-level directories, as your commands can permanently alter files that the operating system needs to function. In many cases, trying to run commands in
    $ ls
    
    8 directories will require special permissions which are not discussed here, so it’s best to avoid them and work within your home directory. Dealing with the
    $ ls
    
    3 directory is very common. The tilde character,
    $ man ls
    
    2, is a shortcut for your home directory. In our case, the
    $ ls
    
    8 directory is two levels above our
    $ ls
    
    3 directory, so
    $ cd ..
    
    1 or
    $ man ls
    
    6 will take you to
    $ man ls
    
    7 and
    $ man ls
    
    8 will take you to
    $ ls
    
    4. Navigate to the
    $ cd ..
    
    5 directory:

    -bash: cd: shell_data: No such file or directory
    
    7

    Then enter the command:

    -bash: cd: shell_data: No such file or directory
    
    8

    -bash: cd: shell_data: No such file or directory
    
    9

    This prints the contents of your home directory, without you needing to type the full path.

    The commands

    $ cd ..
    
    1, and
    $ man ls
    
    6 are very useful for quickly navigating back to your home directory. We will be using the
    $ man ls
    
    2 character in later lessons to specify our home directory.

    Key Points

    • The

      $ ls
      
      4,
      $ man ls
      
      2, and
      $ cd ..
      
      8 characters represent important navigational shortcuts.

    • Hidden files and directories start with

      $ pwd
      
      2 and can be viewed using
      $ cd shell_data
      
      08.

    • Relative paths specify a location starting from the current location, while absolute paths specify a location from the root of the file system.

      What is the command to move to one directory above?

      To go up one level of the directory tree, type the following: cd .. The special file name, dot dot ( .. ), refers to the directory immediately above the current directory, its parent directory.

      What command would you use to move from directory to directory?

      The mv command moves files and directories from one directory to another, or renames a file or directory. If you move a file or directory to a new directory, it retains the base file name.

      Which command will let you move to the parent directory move up one directory?

      You need to use the mv command that moves one or more files or directories from one place to another. You must have have write permission for the directories which the file will move between. The syntax is as follows to move /home/apache2/www/html directory up one level at /home/apache2/www/ directory.

      What is the command if you will change the directory from the current directory to the root directory C drive )?

      The cd command can be used to change into a subdirectory, move back into the parent directory, move all the way back to the root directory or move to any given directory.