Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Linux Command Line File and Directory Management

Hello! Last time, you opened a Linux project folder in VS Code and checked that its terminal was running Ubuntu. You also used pwd to see where that terminal was working. Today you will use the same terminal to navigate, create, copy, move, and remove files and directories. These are the operations you will need to organize source data, intermediate files, and pipeline code.

Plan on about 40 minutes: a short video, a focused reading, and a guided session in a folder you can safely experiment in.


Find your place before changing anything

A directory is a folder; a file is an item stored in a directory. Your shell always has a current working directory. A command such as ls acts there unless you give it a path to somewhere else.

Watch the beginning of Basic Linux Navigation by Nathan Backman. Notice how the prompt changes as the presenter enters a directory, and how ls lets you check the result of mkdir.

Basic Linux Navigation

Watch the opening of Basic Linux Navigation for a visual introduction to the terminal's current directory.

Watch the navigation demo, from the explanation of the prompt and home directory through creating a folder and entering it with cd. Focus on which directory each command acts in; the presenter's folder names are only examples.

In your WSL-connected VS Code terminal, return to the project folder from last time:

cd ~/projects/de-foundations
pwd
ls -F

Your pwd output should end in /projects/de-foundations. The ~ at the start of a path means your Linux home directory. In ls -F, a trailing / marks a directory; a name without that marker is generally a file.

Paths answer two different questions:

  • A relative path, such as raw/orders.csv, starts from your current working directory.
  • An absolute path starts with /, the root of the Linux file system. The full path printed by pwd is an example.

The shortcuts . and .. mean “this directory” and “its parent directory.” For example, cd .. goes up one level, while cd with no argument returns you to your home directory. If a command says “No such file or directory,” check pwd and the spelling and case of the path before trying again. raw and Raw are different names in Linux.


Learn the operations before using them

Read the selected parts of Ubuntu’s The Linux command line for beginners. Its example workspace is /tmp/tutorial; read the examples, but use the separate project-folder walkthrough below rather than copying its commands into your terminal. You can skip its section on creating files with redirection—we will cover redirection next lesson.

The Linux command line for beginners

Read Ubuntu's explanations of directory creation, copying, moving, and deletion. Pay particular attention to the distinction between an empty directory and one containing files.

In section 4, "Creating folders and files," start just after the mkdir /tmp/tutorial example, at creating directories. Stop after the explanation of the -p option, before the examples about names with spaces. In section 5, "Moving and manipulating files," begin with the mv combined.txt dir1 example. Continue through copying and moving; stop before the series of folder-renaming commands. Then read the subsection "Deleting files and folders," from deletion and safety, including its warning about rm -r.

The commands share a useful pattern: a command name is followed by options, such as -p or -r, and arguments, such as source and destination paths. For cp and mv, read two paths as “the item to act on” and “where it should go.” If the destination is an existing directory, the item keeps its name inside that directory. If the destination is a new filename, the operation creates or changes to that name.


Build and inspect a small workspace

We will use shell-lab inside your existing project folder. The commands below assume this is your first run; if shell-lab already exists, inspect it rather than running the removal commands against files you may want to keep.

First, confirm your location, then create and enter the lab:

cd ~/projects/de-foundations
pwd
mkdir shell-lab
cd shell-lab
pwd

mkdir makes a directory. Your final pwd should end in /projects/de-foundations/shell-lab. Now create a few places where a batch pipeline might keep incoming files, working files, and copies:

mkdir -p raw/incoming staging archive
ls -F
ls -F raw

The -p option lets mkdir create a missing parent directory—in this case, raw as well as raw/incoming. ls -F raw inspects raw without changing your current directory. You should see incoming/.

Create an empty placeholder file and inspect its location:

touch raw/incoming/orders.csv
ls -F raw/incoming

touch creates an empty file if the named file does not exist. It does not add CSV data; orders.csv is only a practice filename here. The path is relative to shell-lab. In future work, you will need to be equally precise about which folder contains an actual input file.


Copy, move, and rename

Suppose you want to preserve an incoming file before moving the working version. Copy it first:

cp raw/incoming/orders.csv archive/orders-original.csv
ls -F raw/incoming
ls -F archive

cp leaves the source in place and creates another file at the destination. Here the copy has a different name. Next, move the original into staging:

mv raw/incoming/orders.csv staging/orders.csv
ls -F raw/incoming
ls -F staging

Unlike cp, mv does not leave the source at its old path. You can use the same command to rename a file without changing its directory:

mv staging/orders.csv staging/orders-2027-01-01.csv
ls -F staging

A new filename does not change a file’s contents or format. It is still the same empty practice file. mv can move directories too, and cp -r can copy a directory with its contents. Make a directory copy, inspect it, then move it into archive under a new name:

cp -r staging staging-backup
ls -F staging-backup
mv staging-backup archive/staging-snapshot
ls -F archive
ls -F archive/staging-snapshot

You should now have the working file in staging and a separate directory copy under archive/staging-snapshot. Notice that you performed these operations from shell-lab; you did not need to cd into every directory. The paths supplied to each command identified the items to act on.

Before copying or moving into an existing location, use ls to check what is already there. On Ubuntu, cp and mv can replace an existing destination file. When you want a confirmation prompt before such a replacement, use cp -i or mv -i.


Remove only what you intend to remove

Deletion from the terminal is not a move to the Windows Recycle Bin. In this lab, use exact paths—not * wildcards—and check your location and targets first:

pwd
ls -F archive
ls -F raw/incoming

Confirm that pwd ends in /projects/de-foundations/shell-lab. The incoming directory is empty because you moved its file. You can now remove the extra file copy and that empty directory:

rm archive/orders-original.csv
rmdir raw/incoming
ls -F archive
ls -F raw

rm removes a file. rmdir removes a directory only if it is empty. The snapshot directory, by contrast, still contains a file. If you deliberately want to remove the entire practice copy, inspect it immediately before using recursive removal:

ls -F archive/staging-snapshot
rm -r archive/staging-snapshot
ls -F archive
ls -F staging

The -r option means recursive: rm removes the directory and what is inside it. Use it cautiously; the surviving staging directory and its file should still appear when you inspect them. If you are unsure about a deletion, stop and check pwd and ls again. rm -i can also ask for confirmation before removing a file.


Key takeaways

pwd, ls, and cd keep you oriented; relative and absolute paths tell commands where to act. mkdir and touch create directories and empty files. cp preserves a source, while mv relocates or renames it. For deletion, prefer rmdir for an empty directory and treat rm -r as a deliberate, carefully checked operation.

Next, you will inspect text inside files and use pipes, redirection, and grep to work with command output.

Can't find a good explanation? Sign up and we'll make it for you

Sign up