TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 2 of 12 · By Ethan Cole · Updated November 21, 2026

Create, Copy, Move and Delete Files in the Terminal

Once you can move between folders, the next step is handling files. Five commands cover most daily work, and they all follow the same pattern: command, options, then one or more file names.

Creating and reading

  • touch notes.txt creates an empty file (or updates the timestamp of an existing one).
  • echo "Buy milk" > notes.txt writes a line into a file, replacing what was there. The > sign is a redirection, covered properly in the next chapter.
  • cat notes.txt prints a file's contents. The name comes from "concatenate": cat a.txt b.txt prints both one after the other.

For long files, less notes.txt lets you scroll and search, and q quits.

Copying: cp

cp source destination copies a file. If the destination is an existing folder, the copy keeps its name and goes inside it. To copy a whole folder and everything in it, add -r (recursive): cp -r photos backup.

Moving and renaming: mv

There is no separate rename command. mv old.txt new.txt renames, and mv new.txt archive/ moves the file into a folder. Like cp, mv silently replaces a destination file with the same name; mv -i asks first.

Deleting: rm

rm notes.txt deletes a file. rm -r folder deletes a folder and all its contents. There is no trash can: deleted files are gone. Two safety habits:

  • Run ls with the same pattern first to see exactly what would be removed.
  • Use rm -i to be asked about each file, especially with wildcards.

rmdir folder removes a folder only if it is empty, which makes it a safe choice for cleanup.

Terminal · bash + git · copy and run locally
touch empty.txt
echo "Buy milk" > notes.txt
cat notes.txt
cp notes.txt backup.txt
mkdir archive
mv backup.txt archive/old-notes.txt
rm notes.txt
ls
ls -R
cat archive/old-notes.txt
rmdir archive
rm archive/old-notes.txt
rmdir archive
ls

Transcript (verified by our test suite in a fresh temp folder)

$ touch empty.txt
$ echo "Buy milk" > notes.txt
$ cat notes.txt
Buy milk
$ cp notes.txt backup.txt
$ mkdir archive
$ mv backup.txt archive/old-notes.txt
$ rm notes.txt
$ ls
archive
empty.txt
$ ls -R
archive
empty.txt

./archive:
old-notes.txt
$ cat archive/old-notes.txt
Buy milk
$ rmdir archive
rmdir: archive: Directory not empty
$ rm archive/old-notes.txt
$ rmdir archive
$ ls
empty.txt

The transcript creates a file, copies it, renames the copy into a new folder and deletes the original. The final ls -R lists every folder recursively, so you can see where each file ended up.

Wildcards

The shell expands * to matching names before the command runs: ls *.txt lists every file ending in .txt, and cp *.jpg photos/ copies all JPEGs. This is powerful and is exactly why rm * deserves a second look before you press Enter.

Common mistakes

  • cp folder backup without -r refuses to copy a directory.
  • Overwriting a file with > when you meant to add to it with >>.
  • Running rm -r from the wrong directory. Check with pwd first.

Summary

  • touch and echo ... > create files; cat shows them.
  • cp copies (-r for folders); mv moves and renames.
  • rm deletes permanently; check first and consider -i.

Quiz: Working with files

1. How do you rename draft.txt to final.txt?

mv both moves and renames files.

2. Which command copies a folder and everything inside it?

The -r flag makes cp copy directories recursively.

3. What happens to a file deleted with rm?

rm does not use a trash can, so deletions cannot be undone from the shell.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.