Skip to main content
Chemistry LibreTexts

5: Directory (Folder) Commands

  • Page ID
    469634
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Directory (Folder) Commands

    (transclusion link)

    cd

    cd <path> directs to a specific directory in the tree

    note, there must be a space after the cd

    cd switches

    the following switches work on cd

    cd /home/pi
    cd   #by itself takes you home
    cd ~  navigate to home directory
    cd /  navigate to root directory
    cd .. go up one level
    cd - go back one level
    

     

     

    cp 

    cp - copy directory

    We can copy a directory to a new location by using the command: 

    • cp    directory_name    destination :  Copies directory (or file) to a new location

    Note

    To include the subfolders of a directory, we need to add -r (recursive)

    cp /tmp/test/subdir2 /tmp/test/subdir1 -r
    cd subdir1
    ls
    
    Output:

    clipboard_ea7bee6c7c6ce96222fece9e41c1365cc.png

    Relative path

    The previous examples that we worked on used some relative paths. Working with a relative path means that the place where you go depends on your current working directory.

    The "etc" directory is a file that is located directly inside the root of the file system. If you were to try and cd to the "etc" folder while you are in the root directory, it will work.

    cd /
    pwd
    cd etc 
    pwd      
    

    However, if you are not in your root directory, that won't work.

    cd
    pwd
    cd etc
    pwd     
    

    You will get an error saying "No such file or directory". 

    Changing the working directory will affect where you start. The path of the directory you want to access only makes sense while it is relative to your working directory.

     

    Absolute path

    Absolute commands have the same effect no matter what your current working directory is. From our previous examples, the cd command is an absolute one. When you run it on its own, you go straight to your home directory. Another previous example is the cd / command. It directly switches you to the root directory when you run it on its own.

    Note: Any path starting with / is an absolute path. When you want to switch to a directory and start your path with the /, it is the equivalent of "going to the root directory and then to the folder that comes after the slash"

    cd
    pwd
    cd /etc     
    pwd
    

     

    find 

    find works on both directories and files.

    ls

    With the last command, we created two new subdirectories. We can look at the list with the command:

    • ls : list
    ls
    
    Output:

    clipboard_e986d6180174dbd1645e69e184e73bac0.png

     

    ls switches

    ls
    ls /home/pi
    ls -1     (one, to make a single column)
    ls -l      (long, to list info)
    ls -l -a   (long and all, to include hidden files
    ls ../   (reaches up one level)
    

    mkdir

    the following commands show you where you are, make a new directory, show you the new directory and move you to the new directory

    pwd
    mkdir new_directory
    ls
    cd new_directory
    ls
    

     

    rm - Remove Directory

    for empty directories

    rm switches

    rm -d directory_name
    rm -r recursive and removes subfolders
    rm -rf recursive and forced
    

    for directories with files in them (-r is recursive, and removes contents of directory)

    rm -r directory-name
    

    The -r is a switch making the command recursive

    to do it without asking for confirmation 

    rm -rf directory_name 
    

    The -f switch "forces" the action

    BE VERY CAREFUL WHEN USING  rm -r or rm -rf as you delete everything, including subfolders.

    pwd

    Identifies Current Directory

     rebelford@raspberrypi:~ $ pwd

    pwd
    
    Output:

    clipboard_edeeefe933216cfa93174ffe3959c36de.png

     

    tree

    tree

    This shows the file structure from where you are 


    5: Directory (Folder) Commands is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?