Chapter 2 Navigating File System
2.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.
## /mnt/j/R/ebooks/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.
## analysis.R
## bash-tutorial
## bash.R
## bash.Rmd
## bash.sh
## imports_blorr.txt
## imports_olsrr.txt
## lorem-ipsum.txt
## main_project.zip
## myfiles
## mypackage
## myproject
## myproject.zip
## myproject1
## myproject2
## myproject3
## myproject4
## package_names.txt
## packproj.zip
## pkg_names.tar
## pkg_names.txt
## r
## r2
## r_releases
## release_names.tar
## release_names.tar.gz
## release_names.txt
## release_names_18.txt
## release_names_18_19.txt
## release_names_19.txt
## releases.txt.gz
## sept_15.csv.gz
## 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.
## /mnt/j/R/ebooks/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.
## analysis.R
## bash-tutorial
## bash.R
## bash.Rmd
## bash.sh
## imports_blorr.txt
## imports_olsrr.txt
## lorem-ipsum.txt
## main_project.zip
## myfiles
## mypackage
## myproject
## myproject.zip
## myproject1
## myproject2
## myproject3
## myproject4
## package_names.txt
## packproj.zip
## pkg_names.tar
## pkg_names.txt
## r
## r2
## r_releases
## release_names.tar
## release_names.tar.gz
## release_names.txt
## release_names_18.txt
## release_names_18_19.txt
## release_names_19.txt
## releases.txt.gz
## rfiles
## sept_15.csv.gz
## 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.
## analysis.R
## bash-tutorial
## bash.R
## bash.Rmd
## bash.sh
## imports_blorr.txt
## imports_olsrr.txt
## lorem-ipsum.txt
## main_project.zip
## myfiles
## mypackage
## myproject
## myproject.zip
## myproject1
## myproject2
## myproject3
## myproject4
## package_names.txt
## packproj.zip
## pkg_names.tar
## pkg_names.txt
## r
## r2
## r_releases
## release_names.tar
## release_names.tar.gz
## release_names.txt
## release_names_18.txt
## release_names_18_19.txt
## release_names_19.txt
## releases.txt.gz
## sept_15.csv.gz
## urls.txt
## zip_example.zip
2.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.
## /
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.
## /mnt/j/R/ebooks/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 (~
).
## /home/aravind
To change into the previous working directory, use cd -
.
## /mnt/j/R/ebooks/bash-intro
## /mnt/j/R/ebooks/bash-intro
The current working directory is represented by a single dot (.
). cd .
will change us into the current directory i.e. it will do nothing.
2.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 |
2.3.1 List files one per line
## total 31132
## -rwxrwxrwx 1 aravind aravind 12 Oct 19 2019 analysis.R
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 bash-tutorial
## -rwxrwxrwx 1 aravind aravind 430 Oct 19 2019 bash.R
## -rwxrwxrwx 1 aravind aravind 145 Oct 19 2019 bash.Rmd
## -rwxrwxrwx 1 aravind aravind 16242 Oct 22 2019 bash.sh
## -rwxrwxrwx 1 aravind aravind 35 Oct 16 2019 imports_blorr.txt
## -rwxrwxrwx 1 aravind aravind 34 Oct 16 2019 imports_olsrr.txt
## -rwxrwxrwx 1 aravind aravind 39501 Oct 19 2019 lorem-ipsum.txt
## -rwxrwxrwx 1 aravind aravind 9291 Oct 19 2019 main_project.zip
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myfiles
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 mypackage
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject
## -rwxrwxrwx 1 aravind aravind 5200 Jun 27 2020 myproject.zip
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject1
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject2
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject3
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject4
## -rwxrwxrwx 1 aravind aravind 1498 Oct 19 2019 package_names.txt
## -rwxrwxrwx 1 aravind aravind 3937 Jun 27 2020 packproj.zip
## -rwxrwxrwx 1 aravind aravind 10240 Jun 27 2020 pkg_names.tar
## -rwxrwxrwx 1 aravind aravind 1082 Oct 19 2019 pkg_names.txt
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 r
## drwxrwxrwx 1 aravind aravind 4096 Jun 20 2020 r2
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 r_releases
## -rwxrwxrwx 1 aravind aravind 10240 Oct 19 2019 release_names.tar
## -rwxrwxrwx 1 aravind aravind 632 Oct 19 2019 release_names.tar.gz
## -rwxrwxrwx 1 aravind aravind 546 Oct 19 2019 release_names.txt
## -rwxrwxrwx 1 aravind aravind 65 Oct 19 2019 release_names_18.txt
## -rwxrwxrwx 1 aravind aravind 118 Jun 27 2020 release_names_18_19.txt
## -rwxrwxrwx 1 aravind aravind 53 Oct 19 2019 release_names_19.txt
## -rwxrwxrwx 1 aravind aravind 383 Jun 27 2020 releases.txt.gz
## -rwxrwxrwx 1 aravind aravind 31754998 Oct 19 2019 sept_15.csv.gz
## -rwxrwxrwx 1 aravind aravind 157 Oct 19 2019 urls.txt
## -rwxrwxrwx 1 aravind aravind 4398 Oct 19 2019 zip_example.zip
2.3.2 List all files including hidden files
## .
## ..
## analysis.R
## bash-tutorial
## bash.R
## bash.Rmd
## bash.sh
## imports_blorr.txt
## imports_olsrr.txt
## lorem-ipsum.txt
## main_project.zip
## myfiles
## mypackage
## myproject
## myproject.zip
## myproject1
## myproject2
## myproject3
## myproject4
## package_names.txt
## packproj.zip
## pkg_names.tar
## pkg_names.txt
## r
## r2
## r_releases
## release_names.tar
## release_names.tar.gz
## release_names.txt
## release_names_18.txt
## release_names_18_19.txt
## release_names_19.txt
## releases.txt.gz
## sept_15.csv.gz
## urls.txt
## zip_example.zip
2.3.3 Display file permissions, ownership, size & modification date
## total 31132
## drwxrwxrwx 1 aravind aravind 4096 Jun 2 17:13 .
## drwxrwxrwx 1 aravind aravind 4096 Jun 2 17:13 ..
## -rwxrwxrwx 1 aravind aravind 12 Oct 19 2019 analysis.R
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 bash-tutorial
## -rwxrwxrwx 1 aravind aravind 430 Oct 19 2019 bash.R
## -rwxrwxrwx 1 aravind aravind 145 Oct 19 2019 bash.Rmd
## -rwxrwxrwx 1 aravind aravind 16242 Oct 22 2019 bash.sh
## -rwxrwxrwx 1 aravind aravind 35 Oct 16 2019 imports_blorr.txt
## -rwxrwxrwx 1 aravind aravind 34 Oct 16 2019 imports_olsrr.txt
## -rwxrwxrwx 1 aravind aravind 39501 Oct 19 2019 lorem-ipsum.txt
## -rwxrwxrwx 1 aravind aravind 9291 Oct 19 2019 main_project.zip
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myfiles
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 mypackage
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject
## -rwxrwxrwx 1 aravind aravind 5200 Jun 27 2020 myproject.zip
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject1
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject2
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject3
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject4
## -rwxrwxrwx 1 aravind aravind 1498 Oct 19 2019 package_names.txt
## -rwxrwxrwx 1 aravind aravind 3937 Jun 27 2020 packproj.zip
## -rwxrwxrwx 1 aravind aravind 10240 Jun 27 2020 pkg_names.tar
## -rwxrwxrwx 1 aravind aravind 1082 Oct 19 2019 pkg_names.txt
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 r
## drwxrwxrwx 1 aravind aravind 4096 Jun 20 2020 r2
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 r_releases
## -rwxrwxrwx 1 aravind aravind 10240 Oct 19 2019 release_names.tar
## -rwxrwxrwx 1 aravind aravind 632 Oct 19 2019 release_names.tar.gz
## -rwxrwxrwx 1 aravind aravind 546 Oct 19 2019 release_names.txt
## -rwxrwxrwx 1 aravind aravind 65 Oct 19 2019 release_names_18.txt
## -rwxrwxrwx 1 aravind aravind 118 Jun 27 2020 release_names_18_19.txt
## -rwxrwxrwx 1 aravind aravind 53 Oct 19 2019 release_names_19.txt
## -rwxrwxrwx 1 aravind aravind 383 Jun 27 2020 releases.txt.gz
## -rwxrwxrwx 1 aravind aravind 31754998 Oct 19 2019 sept_15.csv.gz
## -rwxrwxrwx 1 aravind aravind 157 Oct 19 2019 urls.txt
## -rwxrwxrwx 1 aravind aravind 4398 Oct 19 2019 zip_example.zip
2.3.4 Display size in human readable format
## total 31M
## -rwxrwxrwx 1 aravind aravind 12 Oct 19 2019 analysis.R
## drwxrwxrwx 1 aravind aravind 4.0K Jun 27 2020 bash-tutorial
## -rwxrwxrwx 1 aravind aravind 430 Oct 19 2019 bash.R
## -rwxrwxrwx 1 aravind aravind 145 Oct 19 2019 bash.Rmd
## -rwxrwxrwx 1 aravind aravind 16K Oct 22 2019 bash.sh
## -rwxrwxrwx 1 aravind aravind 35 Oct 16 2019 imports_blorr.txt
## -rwxrwxrwx 1 aravind aravind 34 Oct 16 2019 imports_olsrr.txt
## -rwxrwxrwx 1 aravind aravind 39K Oct 19 2019 lorem-ipsum.txt
## -rwxrwxrwx 1 aravind aravind 9.1K Oct 19 2019 main_project.zip
## drwxrwxrwx 1 aravind aravind 4.0K Jun 27 2020 myfiles
## drwxrwxrwx 1 aravind aravind 4.0K Oct 22 2019 mypackage
## drwxrwxrwx 1 aravind aravind 4.0K Oct 22 2019 myproject
## -rwxrwxrwx 1 aravind aravind 5.1K Jun 27 2020 myproject.zip
## drwxrwxrwx 1 aravind aravind 4.0K Jun 27 2020 myproject1
## drwxrwxrwx 1 aravind aravind 4.0K Jun 27 2020 myproject2
## drwxrwxrwx 1 aravind aravind 4.0K Oct 22 2019 myproject3
## drwxrwxrwx 1 aravind aravind 4.0K Oct 22 2019 myproject4
## -rwxrwxrwx 1 aravind aravind 1.5K Oct 19 2019 package_names.txt
## -rwxrwxrwx 1 aravind aravind 3.9K Jun 27 2020 packproj.zip
## -rwxrwxrwx 1 aravind aravind 10K Jun 27 2020 pkg_names.tar
## -rwxrwxrwx 1 aravind aravind 1.1K Oct 19 2019 pkg_names.txt
## drwxrwxrwx 1 aravind aravind 4.0K Oct 22 2019 r
## drwxrwxrwx 1 aravind aravind 4.0K Jun 20 2020 r2
## drwxrwxrwx 1 aravind aravind 4.0K Jun 27 2020 r_releases
## -rwxrwxrwx 1 aravind aravind 10K Oct 19 2019 release_names.tar
## -rwxrwxrwx 1 aravind aravind 632 Oct 19 2019 release_names.tar.gz
## -rwxrwxrwx 1 aravind aravind 546 Oct 19 2019 release_names.txt
## -rwxrwxrwx 1 aravind aravind 65 Oct 19 2019 release_names_18.txt
## -rwxrwxrwx 1 aravind aravind 118 Jun 27 2020 release_names_18_19.txt
## -rwxrwxrwx 1 aravind aravind 53 Oct 19 2019 release_names_19.txt
## -rwxrwxrwx 1 aravind aravind 383 Jun 27 2020 releases.txt.gz
## -rwxrwxrwx 1 aravind aravind 31M Oct 19 2019 sept_15.csv.gz
## -rwxrwxrwx 1 aravind aravind 157 Oct 19 2019 urls.txt
## -rwxrwxrwx 1 aravind aravind 4.3K Oct 19 2019 zip_example.zip
2.3.5 Sort list by size
## total 31132
## -rwxrwxrwx 1 aravind aravind 31754998 Oct 19 2019 sept_15.csv.gz
## -rwxrwxrwx 1 aravind aravind 39501 Oct 19 2019 lorem-ipsum.txt
## -rwxrwxrwx 1 aravind aravind 16242 Oct 22 2019 bash.sh
## -rwxrwxrwx 1 aravind aravind 10240 Jun 27 2020 pkg_names.tar
## -rwxrwxrwx 1 aravind aravind 10240 Oct 19 2019 release_names.tar
## -rwxrwxrwx 1 aravind aravind 9291 Oct 19 2019 main_project.zip
## -rwxrwxrwx 1 aravind aravind 5200 Jun 27 2020 myproject.zip
## -rwxrwxrwx 1 aravind aravind 4398 Oct 19 2019 zip_example.zip
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 bash-tutorial
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myfiles
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 mypackage
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject1
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject2
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject3
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject4
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 r
## drwxrwxrwx 1 aravind aravind 4096 Jun 20 2020 r2
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 r_releases
## -rwxrwxrwx 1 aravind aravind 3937 Jun 27 2020 packproj.zip
## -rwxrwxrwx 1 aravind aravind 1498 Oct 19 2019 package_names.txt
## -rwxrwxrwx 1 aravind aravind 1082 Oct 19 2019 pkg_names.txt
## -rwxrwxrwx 1 aravind aravind 632 Oct 19 2019 release_names.tar.gz
## -rwxrwxrwx 1 aravind aravind 546 Oct 19 2019 release_names.txt
## -rwxrwxrwx 1 aravind aravind 430 Oct 19 2019 bash.R
## -rwxrwxrwx 1 aravind aravind 383 Jun 27 2020 releases.txt.gz
## -rwxrwxrwx 1 aravind aravind 157 Oct 19 2019 urls.txt
## -rwxrwxrwx 1 aravind aravind 145 Oct 19 2019 bash.Rmd
## -rwxrwxrwx 1 aravind aravind 118 Jun 27 2020 release_names_18_19.txt
## -rwxrwxrwx 1 aravind aravind 65 Oct 19 2019 release_names_18.txt
## -rwxrwxrwx 1 aravind aravind 53 Oct 19 2019 release_names_19.txt
## -rwxrwxrwx 1 aravind aravind 35 Oct 16 2019 imports_blorr.txt
## -rwxrwxrwx 1 aravind aravind 34 Oct 16 2019 imports_olsrr.txt
## -rwxrwxrwx 1 aravind aravind 12 Oct 19 2019 analysis.R
2.3.6 Sort list by modification date
## total 31132
## -rwxrwxrwx 1 aravind aravind 35 Oct 16 2019 imports_blorr.txt
## -rwxrwxrwx 1 aravind aravind 34 Oct 16 2019 imports_olsrr.txt
## -rwxrwxrwx 1 aravind aravind 145 Oct 19 2019 bash.Rmd
## -rwxrwxrwx 1 aravind aravind 12 Oct 19 2019 analysis.R
## -rwxrwxrwx 1 aravind aravind 39501 Oct 19 2019 lorem-ipsum.txt
## -rwxrwxrwx 1 aravind aravind 430 Oct 19 2019 bash.R
## -rwxrwxrwx 1 aravind aravind 546 Oct 19 2019 release_names.txt
## -rwxrwxrwx 1 aravind aravind 10240 Oct 19 2019 release_names.tar
## -rwxrwxrwx 1 aravind aravind 4398 Oct 19 2019 zip_example.zip
## -rwxrwxrwx 1 aravind aravind 9291 Oct 19 2019 main_project.zip
## -rwxrwxrwx 1 aravind aravind 632 Oct 19 2019 release_names.tar.gz
## -rwxrwxrwx 1 aravind aravind 157 Oct 19 2019 urls.txt
## -rwxrwxrwx 1 aravind aravind 53 Oct 19 2019 release_names_19.txt
## -rwxrwxrwx 1 aravind aravind 65 Oct 19 2019 release_names_18.txt
## -rwxrwxrwx 1 aravind aravind 1498 Oct 19 2019 package_names.txt
## -rwxrwxrwx 1 aravind aravind 1082 Oct 19 2019 pkg_names.txt
## -rwxrwxrwx 1 aravind aravind 31754998 Oct 19 2019 sept_15.csv.gz
## -rwxrwxrwx 1 aravind aravind 16242 Oct 22 2019 bash.sh
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 r
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject3
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 mypackage
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject
## drwxrwxrwx 1 aravind aravind 4096 Oct 22 2019 myproject4
## drwxrwxrwx 1 aravind aravind 4096 Jun 20 2020 r2
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 r_releases
## -rwxrwxrwx 1 aravind aravind 118 Jun 27 2020 release_names_18_19.txt
## -rwxrwxrwx 1 aravind aravind 10240 Jun 27 2020 pkg_names.tar
## -rwxrwxrwx 1 aravind aravind 383 Jun 27 2020 releases.txt.gz
## -rwxrwxrwx 1 aravind aravind 3937 Jun 27 2020 packproj.zip
## -rwxrwxrwx 1 aravind aravind 5200 Jun 27 2020 myproject.zip
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 bash-tutorial
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myfiles
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject1
## drwxrwxrwx 1 aravind aravind 4096 Jun 27 2020 myproject2
2.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()
|