Lesson 3 - Working with Directories
Directories (also called folders) help you organize your files. In this lesson you’ll navigate between directories and create new ones.
The Current Directory
The pwd command (print working directory) shows where you are:
pwdChanging Directories
The cd command (change directory) moves you to a different location:
cd /tmp
pwdYou can use cd .. to go up one level to the parent directory:
cd /tmp
cd ..
pwdCreating Directories
The mkdir command creates a new directory. Create a project folder:
cd /tmp
mkdir myprojectUse the ls -l command to view the contents of the current directory to see the new myproject directory you created.
ls -lThe ls command shows directory contents, the -l flag shows a long listing of files and directories with timestamps.
You can create nested directories with the -p flag:
cd /tmp/myproject
mkdir -p src/componentsNow list the contents of the src directory using the ls -l command:
ls -l srcSpecifying ls without a directory gives you a listing of the current working directory. Specifying a directory gives you a listing of that directory.
Navigating Your New Directories
Now move into the directories we created and explore:
cd /tmp/myproject/src/components
pwdYour Turn
Move to the /tmp directory.
Then create a directory structure for a web project with css and js folders inside a public directory:
cd /tmp
mkdir -p webproject/public/css
mkdir -p webproject/public/js
ls -l webproject/publicRemoving Directories
Use rmdir to remove an empty directory, or rm -r to remove a directory and its contents:
cd /tmp
mkdir empty_folder
rmdir empty_folder
ls -l | grep emptyUseful Directory Commands
| Command | Purpose |
|---|---|
pwd | Print current directory |
cd | Change directory |
cd .. | Go up one level |
cd ~ | Go to home directory |
mkdir | Create a directory |
mkdir -p | Create nested directories |
rmdir | Remove empty directory |
rm -r | Remove directory and contents |
What You Learned
pwdshows your current locationcdchanges directoriesmkdircreates directories, with-pfor nested pathsrmdirremoves empty directories