Set up a safe place to practice

You need a terminal running a POSIX-compatible shell, such as sh or a compatible mode of bash or zsh, and the standard utilities used below. These examples work with commonly available versions on macOS, Linux, and comparable Unix-like environments; they do not require version-specific options. Windows PowerShell uses different commands, so use a POSIX-style environment if you want to follow along exactly.

Commands act on your current working directory unless you give another path. Start by running pwd to see where you are, then run mkdir file-practice to make a practice directory and cd file-practice to enter it. If a directory with that name already exists, choose a different name rather than working among files you did not intend to change. Run pwd again to confirm your location. You can use ls to list the directory’s contents at any point.

A path tells a command which file you mean. A name such as notes.txt refers to a file in the current directory. A path such as drafts/notes.txt refers to a file inside a drafts directory. If a name contains spaces, quote it: “project notes.txt” in the examples below would be typed with ordinary straight quotation marks, as in "project notes.txt".

Create a file

To create an empty file, run touch notes.txt. Check that it exists with ls. If notes.txt already exists, touch does not clear its contents; it updates its timestamps instead. That makes it useful for making a new empty file, but it is still worth checking the directory before you choose a name.

To create a file containing text, run printf '%s\n' 'Hello, world!' > greeting.txt. The printf command produces a line of text, and > sends that output into greeting.txt. Run cat greeting.txt to read it back. Be careful with >: if greeting.txt already exists, this command replaces its contents. In a real project, inspect an existing file before redirecting output to its name.

Copy files without losing track of the original

Use cp followed by a source path and a destination path. For example, cp greeting.txt greeting-copy.txt creates a second file while leaving greeting.txt in place. Run ls to see both names, then cat greeting-copy.txt to check the copy’s contents. If the destination exists, a plain cp command can replace it. You can use cp -i greeting.txt greeting-copy.txt to request a confirmation before overwriting an existing destination.

You can also copy a file into another directory. Run mkdir drafts, then cp notes.txt drafts/notes.txt. The destination path says both where the copy should go and what its name should be. The directory must exist before you copy a file into it. When checking results, ls drafts shows the files inside drafts, while ls shows the entries in your current directory.

Move or rename a file

The mv command changes a file’s location or name rather than making a second copy. Run mv greeting-copy.txt hello.txt to rename the copy you made earlier. Afterward, ls should show hello.txt, but not greeting-copy.txt. The original greeting.txt is unaffected because you moved only its copy.

To move a file into a directory, run mv hello.txt drafts/hello.txt. Check the result with ls drafts. As with cp, an existing destination can be replaced, so mv -i hello.txt drafts/hello.txt is a useful form when you want a prompt before an overwrite. Use quotes around paths with spaces, for example mv "project notes.txt" drafts/notes.txt. If you see a “No such file or directory” message, check your current directory with pwd and inspect the source and destination paths with ls.

Delete carefully and verify the result

Unlike moving a file, deleting one with rm does not place it in a trash folder. In this practice directory, run rm -i notes.txt and confirm the prompt if you want to remove the original empty file. Then run ls to verify that it is gone. The -i option asks before removal; it is especially helpful while learning, though you should still read the file name in the command before pressing Enter.

To remove an empty directory, use rmdir followed by its name. For example, after removing the files you placed in drafts, rmdir drafts will remove that directory. If it is not empty, rmdir refuses to remove it. Avoid using rm -r until you understand every path it will affect: that option removes a directory and its contents recursively. For routine practice, deleting individual files and then removing the empty directory makes each step easier to inspect and correct before you continue.

Back to category ↗