Open CheatSheet Back To List Next Tutorial

File System (Video Tutorial)

File System

Intro

This is the second in a series of tutorials on how to use Unix for software developers.

This tutorial goes over the file system. How to make, move, copy, rename, and delete files and directories from the command line, and how to navigate the file system.

It is recommended to open your system's shell program and follow along with the topics covered.

Read it, watch it, do it, review it:
  • There is both a written and accompanying video version of this tutorial. There are timestamps under the headings that align with the video version of the topic.
    • Read it: For each topic heading, First read the topic. 
    • Watch it: Then watch the associated section of the video.
    • Do it: Then follow the instructions to replicate the steps in your Unix Shell program.
  • The Unix CheatSheet has a major category that aligns with this tutorial.
    • Review it: When you are done with the whole tutorial, open the CheatSheet and review the File System category from this tutorial. Make sure you understand everything, and can refer back to the CheatSheet when you are working on your own projects in the future. 



Directories

[0:00 Video timestamp]

We'll start with directories. 

Paths
  • The Unix file system is like an upside down tree with the root at the top and branches going down. 
    • / Root directory.

  • Paths can be absolute or relative:
    • Absolute path: starts from the root directory /.
    • Relative path: starts from your present working directory. 

  • In Unix, you must use the forward slash to indicate directories / not the back slash \.
  • If a folder has a space in it, you must put it in quotes: /"program files"/directoryname
  • For Windows, including c: when referencing the root directory is optional.
  • Case sensitivity: Unix is case sensitive, but case sensitivity for directory and file names depends on the operating system's file system. MacOS's file system, for instance, is not case sensitive.



Change Directories
[3:35 Video timestamp]

When you open your Terminal application, by default it will open to your home directory which is at:
/Users/username

pwd pwd is a utility command that returns the absolute pathname of the present working directory (i.e., the directory you are in).

To navigate through the file system, use the Change Directories command:
cd path Change directory to the absolute or relative path argument you provide.

Relative paths:
  • ./ is the current directory.
  • cd .. Two dots mean go up one directory.
  • cd ../.. Go up two directories.
  • cd directory_name Go down one directory.
  • cd path/to/directory_name Go down three directories.
  • cd ../../path/to/directory_name Go up two directories then down three.

Absolute paths:
  • cd / Takes you to the root directory.
  • cd /User/username/Documents Takes you to the Documents directory.

Home directory:
Unix has a home directory for the current user's files and directories.
The home directory on a Mac is at /User/username
  • cd /User/username Takes you to your home directory.

Shortcuts to the home directory:
  • cd with no argument will take you to your home directory.
  • cd ~ The tilde is a shortcut for your home directory.
  • cd $HOME Unix has a default environmental variable HOME for the user's home directory.


You can use your home directory in an absolute path:
  • cd ~/Documents will take you to your Documents directory.

cd - will take you to the previous directory you were at.



List Files and Directories

[8:22 Video timestamp]

Now let's talk about listing files and directories with the ls command.

  • ls Lists all files and directories in the current directory.

  • To list the content of a different directory you can either cd into it, or add a path argument:
ls path List all files & directories in the given path.
ls / Lists the files & directories in the root directory.
ls ~ Lists the files & directories in your home directory.
ls ~/Documents Lists the files & directories in your Documents directory.

ls options: There are a number of options available to the list command.
  • ls -pa is a useful option combination. It distinguishes directories from files and shows hidden files.
  • ls -p The -p option distinguishes directories from files by appending a / after directory names.
  • ls -a | ls -A Shows all files & directories including hidden files. Unix system files have a dot in front of them, and are hidden by default.
  • ls -S Sort by file size highest to lowest.
  • ls -1 Show 1 file or directory per line.

Long version options: Use the -l, long version option to get more information on each file and directory, including permissions, ownership, size, and modified date.
  • ls -l Long version.
  • ls -lSh If you are focused on file size, this option combination will give you: -l Long version, -S sort by size, -h human readable. The -h human readable option makes the file sizes easier to read by appending B for bytes, K for kilobytes, or M for megabytes to the size.
  • ls -l filename Get long version info for a specific file or directory.

Examples:
  • ls -pa / See all the files and directories in the root directory.
  • ls -pa ~ See all the files and directories in the home directory.
  • ls -pa ~/Documents See all the files and directories in the Documents directory.
  • ls -la ~ See the long version of all the files and directories in the home directory.



Make, Move, Copy, Rename, and Remove Directories

Make Directories:
[1:02 Video timestamp]

To create a new directory, use the mkdir command.
mkdir directory_name Creates a directory 

To create a two or more level directory, use the -p option:
mkdir -p newdir1/newdir2/newdir3  Creates a whole directory path.

Example: 
Let's go to the Desktop directory (or whichever directory you prefer). 
The Mac command is: cd ~/Desktop

Make four directories named temp moveme copyme renameme : 
mkdir temp moveme copyme renameme
Add a file to the copyme directory:
touch copyme/file.txt

The resulting directories are:
temp/
moveme/ copyme/file.txt renameme/

Move Directory (and all its subdirectories and files):
[1:34 Video timestamp]

To move a directory, use the mv command with two arguments, the absolute or relative path for the directory to move, and the absolute or relative path for the new location:
mv directory_name new_location 

Example: move the moveme directory inside temp:
mv moveme temp

Now the directories are:
temp/moveme/
copyme/file.txt
renameme/

Copy Directory:
[1:54 Video timestamp]

To copy an empty directory use the cp command.
cp directory_name new_location 

To copy a directory and all its subdirectories and files, use the -r recursive option:
cp -r directory_name new_location 

Example: copy the copyme directory and it's contents to the temp directory:
cp -r copyme temp

Now the directories are:
temp/moveme/
temp/copyme/file.txt
copyme/file.txt
renameme/

Rename Directory:
[2:33 Video timestamp]

To rename a directory use the mv command, but move it to the same location with a different name.
mv old_dirname new_dirname 

Example:
rename renameme to removeme 
mv renameme removeme

Now the directories are:
temp/moveme/
temp/copyme/file.txt
copyme/file.txt
removeme/

Remove Directories: BE CAREFUL!
[2:47 Video timestamp]

The below commands only work on empty directories:
To remove an empty directory use one of the following commands:
rmdir directory_name | rm -d directory_name
For multiple directories, list them separated by spaces.
rmdir directory1 directory2 
To remove a whole path:
rmdir -p subdir/subsubdir 

To remove a directory and it's contents, use the -r recursive option. Be very careful because there is no trash. Once a directory and its contents are deleted, they cannot be retrieved.
rm -r directory_name removes the directory and all its files and subdirectories recursively.

For safety, you can add the interactive option along with the recursive option. Interactive requires confirmation on every file removed.
rm -ri directory_name 

Example:
Remove the empty directories: removeme and temp/moveme

rmdir removeme temp/moveme

Remove the directories with content: temp/copyme and copyme
rm -r temp/copyme copyme

Now all the example directories should be gone, except the temp directory. We will use the temp directory in the next section.



Files

[5:40 Video timestamp]

You can work with files from the command line.

Create a file
touch filename The touch command creates a new file. If the file already exists, it changes the last updated date.
Example: touch testfile.txt

Move a file
mv filename subdirectory  Down one directory.
mv filename ..  Up one directory.

Example:
We didn't delete the temp directory that we created earlier so we'll use that now. 
Let's move the testfile into the temp directory:
mv testfile.txt temp

Move it back into the current directory. ./ is the current directory. 
mv temp/testfile.txt ./

Copy a file
cp filename different_filename Copy a file to same directory with a different name.
cp filename sub_directory Copy to directory one level down.

Example: Create a copy of testfile.txt in the same directory, named testfile2.txt
cp testfile.txt testfile2.txt

Rename a file
Use the mv command to rename a file by moving it to the same directory with a different name.
mv old_filename new_filename

Example: Rename testfile2 to removeme.
mv testfile2.txt removeme.txt

Remove a file
rm filename 
rm file1 file2 Multiple files.

Example: Remove the testfile.txt and removeme.txt files.
rm testfile.txt removeme.txt

Add a file to the temp directory. 
touch temp/newfile.txt
Delete the temp directory with the recursive option so it deletes all files in it. Add the interactive option to require confirmation before deleting each file and directory.
rm -ri temp



Find

Find a file in or below the provided path:
find <path> -name <filename> 


Open CheatSheet Back To List Next Tutorial