Working with files
Files live in folders, and folders form one tree that starts at /, the root. Your shell always stands in one folder, the working directory. Follow one short session in a home folder: look around with pwd, ls and cat, then change things with mkdir, cp, cd, mv, touch and rm, and finish with the difference between an absolute path and a relative one.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01You open a terminal and want to know which folder your shell is in. Which command prints it?
pwd prints the working directory: the full path of the folder the shell stands in, such as /home/sam. ls lists what is inside it.
02In /home/sam you run cp hello.txt notes/ (notes is a folder). What do you get?
cp makes a separate copy. When the destination is a folder, the copy keeps its name inside it. Changing one copy later leaves the other alone.
03Inside /home/sam/notes you run touch ../plan.txt. Where does the empty file appear?
.. means the parent folder, one level up. From /home/sam/notes that is /home/sam, so the file is /home/sam/plan.txt.
04What does mv hello.txt todo.txt do when both names are in the same folder?
A move inside one file system is a rename: the folder's entry changes, but no bytes are copied. Careful: if todo.txt already exists, mv replaces it.
05Which is true about rm in the terminal?
rm skips any Trash or Bin: the file is gone. rm -i prompts before each removal. By default rm does not remove directories; rm -r does, with everything inside, so read the command twice.
Glossary
- Directory
- Linux's word for a folder: a list of names, each pointing to a file or to another directory.
- Root ( / )
- The top of the one folder tree. Every absolute path starts here.
- Home folder
- Your own directory, usually /home/yourname. cd with nothing after it takes you there; ~ is short for it.
- Working directory
- The folder the shell is standing in right now. pwd prints it, cd changes it, and relative paths start from it.
- Absolute path
- A path that starts at / and names every folder on the way, such as /home/sam/notes/todo.txt. It works from anywhere.
- Relative path
- A path that starts from the working directory, such as todo.txt or ../plan.txt.
- .. (dot dot)
- The parent folder, one level up. A single . means this folder.
Notes and sources
- The figure's computer belongs to a user named sam, so the home folder is /home/sam. On your machine pwd prints your own home folder, and the prompt usually shows more (your name, the computer and the folder) before the $.
- mv renames in place only when the old and new names are on the same file system (the same disk partition). Across file systems it copies the bytes and then deletes the original.
- Some desktops have a Trash or Bin, but that belongs to the file manager. rm in the terminal skips it: the file is gone. rm -r deletes a folder and everything inside it, so read the command twice before you press Enter.
- touch was made to update a file's timestamps. Making an empty file when the name does not exist yet is a side effect that everyone uses.
- cd is built into the shell itself, not a separate program: a program cannot change the folder its shell stands in.
- Linux man-pages: pwd(1)
- Linux man-pages: ls(1)
- Linux man-pages: cat(1)
- Linux man-pages: mkdir(1)
- Linux man-pages: cp(1)
- Linux man-pages: mv(1)
- Linux man-pages: rename(2), and EXDEV across file systems
- Linux man-pages: touch(1)
- Linux man-pages: rm(1)
- Linux man-pages: path_resolution(7), absolute and relative paths
- Linux man-pages: hier(7), the file system hierarchy (/home)
- Linux man-pages: inode(7), file names and inodes
- GNU Bash Reference Manual: Bourne Shell Builtins (cd)
- GNU Coreutils manual
- Filesystem Hierarchy Standard 3.0: /home
Quiz Unlocks after the last step.