Skip to main content
Chemistry LibreTexts

8: Directory Commands

  • Page ID
    433296
  • \( \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}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    These commands allow you to change read-write and access permissions for files, and change their locations in Directories (Folders)

    Before diving into the linux commands lets look at the directory structure from the file manager on the Pi desktop

    clipboard_e24ba4a22254bd0dd0576d7c6f6c2e663.pngFigure \(\PageIndex{1}\):Directory structure as seen by file manager. (Belford cc 0.0)

     

    In this activity we are going to assume you have started your Pi and are in your user directory

    pwd - Print Working Directory

    Identifies Current Directory

     rebelford@raspberrypi:~ $ pwd

    pwd
    
    Output:

    clipboard_edeeefe933216cfa93174ffe3959c36de.png

     

    ls - List Directory Content

    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 - Create Directory

    In this section, we will learn how to create new files to work with. To avoid any accidental changes that we can make to our existing files, we will start by creating a new directory, not located in the home folder. 

    To create a new directory named "tmp" use the command:

    • mkdir make directory

    First we will list what is in the directory, then make the new directory and then print it out to see that it is th

    We can start create multiple directories at once

    mkdir subdir1 subdir2
    

     

    ls
    mkdir tmp
    ls
    
    Output:

    clipboard_e87af53dbf725e7f8fb75ef04df246b18.png

    • We will now make three new subdirectories, and then list the content
    mkdir sub1 sub2 sub3
    
    Output:

    clipboard_e3f88d1f38282827a84353ba146b722d4.png

     

    cd - Change Directory

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

    note, there must be a space after the cd

     

    We will first list the files where we are, move to tmp and then print the current (working) directory

    ls
    cd tmp
    pwd
    
    Output:

    clipboard_e87030adcb5ff1849f7475f5e08730b28.png

     

    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
    

     

     

    Lets go to the root directory and list the content. We note there is another directory called root there, so lets list its content.  What we notice is we have two directories with the same name, but in different place in the file tree.

    ls
    cd /
    ls
    cd tmp ls
    
    Output:

    clipboard_e40cade907399e0936ce79884bc2bef90.png

    Lets go home

     

    you can always return to the home directory with the ~ switch. Here show we are in the tmp in root, go home, and then move to the tmp we just created and show we are there

    pwd
    cd ~ 
    pwd
    cd tmp
    pwd
    
    Output:

    clipboard_ebb1e21d67aa02907a475051390b76400.png

     

    rm - Remove Directory

    for empty directories

    rm -d directory_name
    

    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.

     

    In this activity we will go back to our user directory and remove the tmp folder we made, and then show it is gone

    cd ..
    ls
    rm -r tmp
    ls
    
    Output:

    clipboard_e1521b543be1802966c62ce414370cc3e.png

     

     

    In this activity we list the files in the user directory, then in one command remove the three sub directories we had earlier made, and then show they are gone

    ls
    rm -r sub1 sub2 sub3
    ls
    
    Output:

    clipboard_e1c43472f8954495c7d63fa555d0f22c4.png

     

    The mkdir can not be used on its own (like the previous command cd or pwd). It is always followed by parameters

    The mkdir command expects to have at least one parameter

    The cd command can use zero or one parameter at maximum

     

    cp - Copying directories

    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 and Absolute paths

    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.

    tree

     


    This page titled 8: Directory Commands is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?