Skip to content

Phase 3 · Free

3.3 Moving Around and Managing Files

Everything you do in a file manager, you can do faster with a handful of commands. There is one big difference: the terminal has no recycle bin. rm means gone.

Phase 3. Lesson 3 of 7. About 25 minutes.

Plain entry

Everything you do in a file manager, you can do faster with a handful of commands. There is one big difference: the terminalThe window or screen where you type text commands and read their output, connected to a shell that runs those commands. Read more → has no recycle bin. rm means gone.

Terms

  • Working directory: the folder you are in now.
  • Wildcard (globA wildcard pattern such as *.log that the shell expands into a list of matching file names before running a command. Read more →): * matches anything, ? matches one character. *.txt means every .txt file.
  • Recursive (-r): apply to a folder and everything inside it.
  • Hidden file: a name starting with ., shown with ls -a.

Depth

pwd                         # where am I
cd ~ && mkdir -p lab/a/b    # -p makes parents too
cd lab && ls -la
touch a/one.txt a/two.txt   # create empty files
cp a/one.txt b-copy.txt     # copy a file
cp -r a a-backup            # copy a folder
mv b-copy.txt renamed.txt   # move or rename
rm renamed.txt              # delete, no undo
rm -r a-backup              # delete a folder
ls -l a

Expected output of the last line:

total 4
drwxrwxr-x 2 you you 4096 Nov  1 09:12 b
-rw-rw-r-- 1 you you    0 Nov  1 09:12 one.txt
-rw-rw-r-- 1 you you    0 Nov  1 09:12 two.txt

Reading ls -l: first character d is a directory, - a file. Then permissions (lesson 3.5 and Phase 6), owner, group, size, date, name.

Safety habits:

  • Use rm -i to be asked before each delete.
  • Before rm with a wildcard, run ls with the same wildcard first to see what it matches.
  • Never run sudo rm -r on a path you have not read twice.

Going further

cp -a preserves permissions, owners, and timestamps; use it for backups. mv within one filesystemThe structure an operating system uses to organize data on a disk into files and folders, tracking names, sizes, permissions, and where each piece is stored. Read more → is instant (it only renames); across filesystems it copies then deletes.

Why it works this way

Most file commands work relative to where you are standing. cp a/one.txt b-copy.txt means “from here.” That is why the number one cause of No such file or directory is being in the wrong folder, not a missing file. Run pwd first; it takes one second. rm has no undo because it removes the name, not a copy to a trash folder. The safety habits above exist for that reason.

Self-check

  1. What does mkdir -p do that mkdir alone does not?
  2. How do you see hidden files?
  3. What is the safe habit before rm *.log?

Answers: (1) Creates missing parent folders. (2) ls -a. (3) Run ls *.log first.

Sources