3  Navigating File System

4 Navigating File System

4.1 Introduction

In this chapter, we will learn commands that will help us

  • navigate between different folders/directories
  • return current working directory
  • list all the files & folders in a directory
  • create and delete directories
Command Description
pwd Print working directory
ls List directory contents
cd Change current working directory
mkdir Create directory
rmdir Remove/delete directory

pwd displays the name of the present working directory.

pwd
/home/runner/work/bash-intro/bash-intro/cline

ls displays information about files and directories in the current directory along with their associated metadata such as

  • size
  • ownership
  • modification date

With no options, it will list the files and directories in the current directory, sorted alphabetically.

ls
analysis.R
bash.R
bash.Rmd
bash.sh
cran_log_sample.csv.gz
imports_blorr.txt
imports_olsrr.txt
lorem-ipsum.txt
main_project.zip
myfiles
mypackage
myproject
package_names.txt
pkg_names.txt
r
release_names.tar
release_names.tar.gz
release_names.txt
release_names_18.txt
release_names_19.txt
urls.txt
zip_example.zip

cd (change directory) changes the current working directory. It is among the most used commands as it allows the user to move around the file system.

cd r
pwd
/home/runner/work/bash-intro/bash-intro/cline/r

mkdir will create new directory. It will allow you to set file mode (permissions associated with the directory) i.e. who can open/modify/delete the directory.

mkdir rfiles
ls
analysis.R
bash.R
bash.Rmd
bash.sh
cran_log_sample.csv.gz
imports_blorr.txt
imports_olsrr.txt
lorem-ipsum.txt
main_project.zip
myfiles
mypackage
myproject
package_names.txt
pkg_names.txt
r
release_names.tar
release_names.tar.gz
release_names.txt
release_names_18.txt
release_names_19.txt
rfiles
urls.txt
zip_example.zip

rmdir will remove empty directories from the file system. It can be used to remove multiple empty directories as well. If the directory is not empty, rmdir will not remove it and instead display a warning that the directory is not empty.

rmdir rfiles
ls
analysis.R
bash.R
bash.Rmd
bash.sh
cran_log_sample.csv.gz
imports_blorr.txt
imports_olsrr.txt
lorem-ipsum.txt
main_project.zip
myfiles
mypackage
myproject
package_names.txt
pkg_names.txt
r
release_names.tar
release_names.tar.gz
release_names.txt
release_names_18.txt
release_names_19.txt
urls.txt
zip_example.zip

4.2 Change Working Directory

Let us focus a bit more on changing working directory. The below table shows commands for changing working directory to

  • up one level
  • previous working directory
  • home directory
  • and root directory
Command Description
cd . Navigate into directory
cd .. Go up one level
cd - Go to previous working directory
cd ~ Change directory to home directory
cd / Change directory to root directory

All files and directories stem from one main directory, the root directory. All the other directories in the system are sub-directories of the root directory and the root directory has no parent directory. It is represented by a single slash (/). cd / will change the current working directory to the root directory.

cd /
pwd
/

The parent directory i.e. the directory one level up from the current directory which contains the directory we are in now is represented by two dots (..). cd .. will change us into the parent directory of the current directory.

cd ..
pwd
/home/runner/work/bash-intro/bash-intro

The home directory is the directory we are placed in, by default, when we launch a new terminal session. It is represented by the tilde (~).

cd ~
pwd
/home/runner

To change into the previous working directory, use cd -.

cd -
pwd
bash: line 1: cd: OLDPWD not set
/home/runner/work/bash-intro/bash-intro/cline

The current working directory is represented by a single dot (.). cd . will change us into the current directory i.e. it will do nothing.

4.3 List Directory Contents

ls will list the contents of a directory. Using different arguments, we can

  • list hidden files
  • view file permissions, ownership, size & modification date
  • sort by size & modification date
Command Description
ls List directory contents
ls -l List files one per line
ls -a List all files including hidden files
ls -la Display file permissions, ownership, size & modification date
ls -lh Long format list with size displayed in human readable format
ls -lS Long format list sorted by size
ls -ltr Long format list sorted by modification date

4.3.1 List files one per line

ls -l
total 200
-rw-r--r-- 1 runner runner    12 Jun  2  2021 analysis.R
-rw-r--r-- 1 runner runner   430 Jun  2  2021 bash.R
-rw-r--r-- 1 runner runner   145 Jun  2  2021 bash.Rmd
-rw-r--r-- 1 runner runner 16242 Jun  2  2021 bash.sh
-rw-r--r-- 1 runner runner 48185 Sep 25 10:55 cran_log_sample.csv.gz
-rw-r--r-- 1 runner runner    35 Jun  2  2021 imports_blorr.txt
-rw-r--r-- 1 runner runner    34 Jun  2  2021 imports_olsrr.txt
-rw-r--r-- 1 runner runner 39501 Jun  2  2021 lorem-ipsum.txt
-rw-r--r-- 1 runner runner  9291 Jun  2  2021 main_project.zip
drwxr-xr-x 2 runner runner  4096 Jun  2  2021 myfiles
drwxr-xr-x 9 runner runner  4096 Jun  2  2021 mypackage
drwxr-xr-x 5 runner runner  4096 Jun  2  2021 myproject
-rw-r--r-- 1 runner runner  1498 Jun  2  2021 package_names.txt
-rw-r--r-- 1 runner runner  1082 Jun  2  2021 pkg_names.txt
drwxr-xr-x 2 runner runner  4096 Jun  2  2021 r
-rw-r--r-- 1 runner runner 10240 Jun  2  2021 release_names.tar
-rw-r--r-- 1 runner runner   632 Jun  2  2021 release_names.tar.gz
-rw-r--r-- 1 runner runner   546 Jun  2  2021 release_names.txt
-rw-r--r-- 1 runner runner    65 Jun  2  2021 release_names_18.txt
-rw-r--r-- 1 runner runner    53 Jun  2  2021 release_names_19.txt
-rw-r--r-- 1 runner runner   157 Jun  2  2021 urls.txt
-rw-r--r-- 1 runner runner  4398 Jun  2  2021 zip_example.zip

4.3.2 List all files including hidden files

ls -a
.
..
analysis.R
bash.R
bash.Rmd
bash.sh
cran_log_sample.csv.gz
imports_blorr.txt
imports_olsrr.txt
lorem-ipsum.txt
main_project.zip
myfiles
mypackage
myproject
package_names.txt
pkg_names.txt
r
release_names.tar
release_names.tar.gz
release_names.txt
release_names_18.txt
release_names_19.txt
urls.txt
zip_example.zip

4.3.3 Display file permissions, ownership, size & modification date

ls -la
total 208
drwxr-xr-x  6 runner runner  4096 Sep 29 17:00 .
drwxr-xr-x 16 runner runner  4096 Sep 29 17:00 ..
-rw-r--r--  1 runner runner    12 Jun  2  2021 analysis.R
-rw-r--r--  1 runner runner   430 Jun  2  2021 bash.R
-rw-r--r--  1 runner runner   145 Jun  2  2021 bash.Rmd
-rw-r--r--  1 runner runner 16242 Jun  2  2021 bash.sh
-rw-r--r--  1 runner runner 48185 Sep 25 10:55 cran_log_sample.csv.gz
-rw-r--r--  1 runner runner    35 Jun  2  2021 imports_blorr.txt
-rw-r--r--  1 runner runner    34 Jun  2  2021 imports_olsrr.txt
-rw-r--r--  1 runner runner 39501 Jun  2  2021 lorem-ipsum.txt
-rw-r--r--  1 runner runner  9291 Jun  2  2021 main_project.zip
drwxr-xr-x  2 runner runner  4096 Jun  2  2021 myfiles
drwxr-xr-x  9 runner runner  4096 Jun  2  2021 mypackage
drwxr-xr-x  5 runner runner  4096 Jun  2  2021 myproject
-rw-r--r--  1 runner runner  1498 Jun  2  2021 package_names.txt
-rw-r--r--  1 runner runner  1082 Jun  2  2021 pkg_names.txt
drwxr-xr-x  2 runner runner  4096 Jun  2  2021 r
-rw-r--r--  1 runner runner 10240 Jun  2  2021 release_names.tar
-rw-r--r--  1 runner runner   632 Jun  2  2021 release_names.tar.gz
-rw-r--r--  1 runner runner   546 Jun  2  2021 release_names.txt
-rw-r--r--  1 runner runner    65 Jun  2  2021 release_names_18.txt
-rw-r--r--  1 runner runner    53 Jun  2  2021 release_names_19.txt
-rw-r--r--  1 runner runner   157 Jun  2  2021 urls.txt
-rw-r--r--  1 runner runner  4398 Jun  2  2021 zip_example.zip

4.3.4 Display size in human readable format

ls -lh
total 200K
-rw-r--r-- 1 runner runner   12 Jun  2  2021 analysis.R
-rw-r--r-- 1 runner runner  430 Jun  2  2021 bash.R
-rw-r--r-- 1 runner runner  145 Jun  2  2021 bash.Rmd
-rw-r--r-- 1 runner runner  16K Jun  2  2021 bash.sh
-rw-r--r-- 1 runner runner  48K Sep 25 10:55 cran_log_sample.csv.gz
-rw-r--r-- 1 runner runner   35 Jun  2  2021 imports_blorr.txt
-rw-r--r-- 1 runner runner   34 Jun  2  2021 imports_olsrr.txt
-rw-r--r-- 1 runner runner  39K Jun  2  2021 lorem-ipsum.txt
-rw-r--r-- 1 runner runner 9.1K Jun  2  2021 main_project.zip
drwxr-xr-x 2 runner runner 4.0K Jun  2  2021 myfiles
drwxr-xr-x 9 runner runner 4.0K Jun  2  2021 mypackage
drwxr-xr-x 5 runner runner 4.0K Jun  2  2021 myproject
-rw-r--r-- 1 runner runner 1.5K Jun  2  2021 package_names.txt
-rw-r--r-- 1 runner runner 1.1K Jun  2  2021 pkg_names.txt
drwxr-xr-x 2 runner runner 4.0K Jun  2  2021 r
-rw-r--r-- 1 runner runner  10K Jun  2  2021 release_names.tar
-rw-r--r-- 1 runner runner  632 Jun  2  2021 release_names.tar.gz
-rw-r--r-- 1 runner runner  546 Jun  2  2021 release_names.txt
-rw-r--r-- 1 runner runner   65 Jun  2  2021 release_names_18.txt
-rw-r--r-- 1 runner runner   53 Jun  2  2021 release_names_19.txt
-rw-r--r-- 1 runner runner  157 Jun  2  2021 urls.txt
-rw-r--r-- 1 runner runner 4.3K Jun  2  2021 zip_example.zip

4.3.5 Sort list by size

ls -lS
total 200
-rw-r--r-- 1 runner runner 48185 Sep 25 10:55 cran_log_sample.csv.gz
-rw-r--r-- 1 runner runner 39501 Jun  2  2021 lorem-ipsum.txt
-rw-r--r-- 1 runner runner 16242 Jun  2  2021 bash.sh
-rw-r--r-- 1 runner runner 10240 Jun  2  2021 release_names.tar
-rw-r--r-- 1 runner runner  9291 Jun  2  2021 main_project.zip
-rw-r--r-- 1 runner runner  4398 Jun  2  2021 zip_example.zip
drwxr-xr-x 2 runner runner  4096 Jun  2  2021 myfiles
drwxr-xr-x 9 runner runner  4096 Jun  2  2021 mypackage
drwxr-xr-x 5 runner runner  4096 Jun  2  2021 myproject
drwxr-xr-x 2 runner runner  4096 Jun  2  2021 r
-rw-r--r-- 1 runner runner  1498 Jun  2  2021 package_names.txt
-rw-r--r-- 1 runner runner  1082 Jun  2  2021 pkg_names.txt
-rw-r--r-- 1 runner runner   632 Jun  2  2021 release_names.tar.gz
-rw-r--r-- 1 runner runner   546 Jun  2  2021 release_names.txt
-rw-r--r-- 1 runner runner   430 Jun  2  2021 bash.R
-rw-r--r-- 1 runner runner   157 Jun  2  2021 urls.txt
-rw-r--r-- 1 runner runner   145 Jun  2  2021 bash.Rmd
-rw-r--r-- 1 runner runner    65 Jun  2  2021 release_names_18.txt
-rw-r--r-- 1 runner runner    53 Jun  2  2021 release_names_19.txt
-rw-r--r-- 1 runner runner    35 Jun  2  2021 imports_blorr.txt
-rw-r--r-- 1 runner runner    34 Jun  2  2021 imports_olsrr.txt
-rw-r--r-- 1 runner runner    12 Jun  2  2021 analysis.R

4.3.6 Sort list by modification date

ls -ltr
total 200
-rw-r--r-- 1 runner runner  4398 Jun  2  2021 zip_example.zip
-rw-r--r-- 1 runner runner   157 Jun  2  2021 urls.txt
-rw-r--r-- 1 runner runner    53 Jun  2  2021 release_names_19.txt
-rw-r--r-- 1 runner runner    65 Jun  2  2021 release_names_18.txt
-rw-r--r-- 1 runner runner   546 Jun  2  2021 release_names.txt
-rw-r--r-- 1 runner runner   632 Jun  2  2021 release_names.tar.gz
-rw-r--r-- 1 runner runner 10240 Jun  2  2021 release_names.tar
drwxr-xr-x 2 runner runner  4096 Jun  2  2021 r
-rw-r--r-- 1 runner runner  1498 Jun  2  2021 package_names.txt
drwxr-xr-x 2 runner runner  4096 Jun  2  2021 myfiles
-rw-r--r-- 1 runner runner  9291 Jun  2  2021 main_project.zip
-rw-r--r-- 1 runner runner    34 Jun  2  2021 imports_olsrr.txt
-rw-r--r-- 1 runner runner    35 Jun  2  2021 imports_blorr.txt
-rw-r--r-- 1 runner runner  1082 Jun  2  2021 pkg_names.txt
drwxr-xr-x 5 runner runner  4096 Jun  2  2021 myproject
drwxr-xr-x 9 runner runner  4096 Jun  2  2021 mypackage
-rw-r--r-- 1 runner runner 39501 Jun  2  2021 lorem-ipsum.txt
-rw-r--r-- 1 runner runner 16242 Jun  2  2021 bash.sh
-rw-r--r-- 1 runner runner   145 Jun  2  2021 bash.Rmd
-rw-r--r-- 1 runner runner   430 Jun  2  2021 bash.R
-rw-r--r-- 1 runner runner    12 Jun  2  2021 analysis.R
-rw-r--r-- 1 runner runner 48185 Sep 25 10:55 cran_log_sample.csv.gz

4.4 R Functions

In R, getwd() will return the current working directory. You can use here() from the here package as well. To change the current working directory, use setwd(). The fs package provides useful functions for file operations.

Command R
pwd getwd() / here::here()
ls dir() / list.files() / list.dirs() / fs::dir_ls() / dir_info()
cd setwd()
mkdir dir.create() / fs::dir_create()
rmdir fs::dir_delete()

4.5 Exercises

  1. From inside the book’s cline/ folder, run pwd, then mkdir practice_nav followed by ls — you should see practice_nav appear. Then run rmdir practice_nav followed by ls and confirm it is gone.
  2. Still in cline/, run cd r, then pwd (path should end in /r). Then cd .. and pwd (back in cline/), then cd - and pwd. Finish with cd ~, pwd (home directory), and cd -, pwd to return. Write down the directory shown after each pwd.
  3. In cline/, run ls -l, then ls -la, then ls -lh, then ls -lS, then ls -ltr. Note what changes each time: hidden entries (starting with .) appear with -a, human-readable sizes with -h, and different ordering with -lS (by size) versus -ltr (by time, oldest first). Which variant would you use to find the most recently modified file?