Around Go: command line · 3 MIN READ

Bash essentials: navigate, inspect and copy files

Use pwd, cd, ls, mkdir, cp and mv in a small disposable workspace.

A shell starts in a working directory, and relative paths are resolved there. Before building or moving files, establish that location. These exercises use a new scratch directory on your own Mac or Linux machine; no administrator permissions are needed.

01

Locate the project before changing it

pwd prints the working directory; ls -la includes hidden entries and metadata. cd changes this shell, not every terminal. The home shortcut is expanded by the shell, while a quoted path can contain spaces.

bash
pwd
ls -la
cd "$HOME"
pwd
Read the code

Compare the two pwd results. ls -la may show .git, but that alone does not establish a repository root. Quoting "$HOME" passes a single path, even if it contains spaces.

02

Create and copy a small fixture

mktemp -d makes a unique scratch directory. mkdir -p creates missing directory components. cp copies bytes; mv changes the path. Keep the original until you have inspected the destination rather than experimenting on project files.

bash
scratch_dir=$(mktemp -d)
mkdir -p "$scratch_dir/input" "$scratch_dir/output"
printf "%s\n" "hello Go" > "$scratch_dir/input/note.txt"
cp "$scratch_dir/input/note.txt" "$scratch_dir/output/copy.txt"
mv "$scratch_dir/output/copy.txt" "$scratch_dir/output/result.txt"
cat "$scratch_dir/output/result.txt"
Read the code

The final line prints hello Go. The > operator creates or truncates the named file; it is appropriate here because the fixture is new. The scratch path stays in scratch_dir for the next command.

03

Check the result and remove only the fixture

wc -l counts newline-terminated lines, and cmp compares the copied content. rm removes the specifically named fixture files; rmdir removes only empty directories, making an unexpected leftover visible instead of recursively erasing it.

bash
wc -l "$scratch_dir/output/result.txt"
cmp "$scratch_dir/input/note.txt" "$scratch_dir/output/result.txt"
rm "$scratch_dir/input/note.txt" "$scratch_dir/output/result.txt"
rmdir "$scratch_dir/input" "$scratch_dir/output" "$scratch_dir"
Read the code

wc should report one line. cmp is silent with status zero for identical files. The cleanup names only files this exercise created; rmdir fails if another file remains, so inspect rather than adding recursive deletion.

TAKE THIS WITH YOU

Start by checking the working directory, quote paths, and verify copied bytes before removing anything. Practice filesystem changes in a named scratch workspace, not in your only project copy.

Check your understanding

What does pwd show?

Go to the source

Progress stays in this browser. No account needed.