The Linux command line lets students control a computer by typing commands into a terminal. This cheat sheet covers the core commands used to move through folders, inspect files, manage files, and get help. It is useful for programming, cybersecurity, web development, data science, and working with remote servers.
Students need these commands because many professional computing tools are built around the terminal.
Key Facts
- pwd prints the full path of the current working directory.
- ls lists files and folders, and ls -la shows hidden files plus detailed permissions, owners, sizes, and dates.
- cd foldername changes into a folder, cd .. moves up one level, and cd ~ returns to the home directory.
- mkdir name creates a directory, touch file.txt creates an empty file, cp source destination copies, mv source destination moves or renames, and rm file deletes a file.
- cat file.txt prints a file to the terminal, less file.txt opens a scrollable view, and head -n 10 file.txt shows the first 10 lines.
- command > file writes output to a file, command >> file appends output, and command < file uses a file as input.
- command1 | command2 sends the output of command1 into command2, such as cat names.txt | sort.
- chmod permissions file changes file permissions, such as chmod u+x script.sh to give the owner execute permission.
Vocabulary
- Terminal
- A text-based program where users type commands to control the operating system.
- Shell
- A command interpreter that reads typed commands and runs programs, with Bash being a common Linux shell.
- Directory
- A folder in the file system that can contain files and other directories.
- Path
- The location of a file or directory, such as /home/student/projects.
- Option
- A modifier added to a command, often starting with -, that changes how the command works.
- Permission
- A rule that controls whether a user can read, write, or execute a file or directory.
Common Mistakes to Avoid
- Using rm without checking the filename is dangerous because deleted files are often not moved to a trash folder and may be hard to recover.
- Forgetting spaces between a command and its arguments is wrong because the shell reads cdprojects as one command instead of cd projects.
- Confusing > and >> can destroy data because > overwrites a file while >> adds new output to the end.
- Running commands from the wrong directory causes errors because relative paths depend on the current working directory shown by pwd.
- Using sudo when it is not needed is risky because administrator privileges can change or delete important system files.
Practice Questions
- 1 You are in /home/student and need to enter /home/student/projects. What command should you type?
- 2 Write a command that creates a directory named labs and then creates an empty file named notes.txt inside it.
- 3 A file named data.txt has 50 lines. What command shows only the first 10 lines?
- 4 Explain why a programmer might use a pipe, such as ls | sort, instead of running two completely separate commands.
Understanding Linux Command Line Reference
A terminal works from a current location inside a tree of directories. The top of that tree is called the root directory. A path beginning with a slash is an absolute path, meaning it names a location from the root.
A relative path starts from the location where the shell is currently working. This difference matters when a command succeeds in one folder but fails in another. File names can contain spaces or symbols, though such names need careful quoting when typed.
Names beginning with a dot are usually hidden configuration files. Students should learn to notice the dot because changing one of these files can affect how a program behaves.
The shell is the program that reads a command line before another program runs. It separates words, interprets options, expands shortcuts, then starts the requested command. Quotation marks tell the shell to keep characters together as one argument.
Wildcards can match many names at once. For example, a pattern ending in .txt can select every text file in a directory. This saves time, but it can select more files than expected.
A safe habit is to inspect a pattern with a listing command before using it in a command that changes or removes files. Command options often begin with one or two hyphens. They change the behavior of a command, so reading the manual entry is more reliable than guessing what an option does.
Programs usually communicate through three standard streams. Standard input carries data into a program. Standard output carries normal results out.
Standard error carries warnings and failure messages. Redirection and pipes are useful because they connect these streams without needing a separate program for every step. A pipe supports a workflow where one tool produces data, another tool filters or rearranges it, and a final tool saves the result.
This is common when processing server logs, survey data, program output, or lists of names. Output redirection can replace an existing file, while appending preserves earlier content. Students should check which action they are using before running a command, especially when the destination is an important file.
Permissions are a basic safety system in multiuser computers. Each file and directory has rules for its owner, a group, and everyone else. Read permission allows viewing file contents.
Write permission allows changes. Execute permission allows a file to run as a program or script. Directories have a related meaning.
Read permission permits listing names, write permission permits creating or deleting entries, and execute permission permits entering the directory or accessing items through it. A script may contain correct code yet refuse to run because its execute permission is missing. On school machines and remote servers, students should avoid changing permissions broadly.
Grant only the access that is needed. Many deletion commands do not use a recycle bin, so work slowly, verify the current directory, and make copies of important work before experimenting.
Good command line work is less about memorizing a long list and more about building a checking routine. Read the command before pressing Enter. Confirm the source and destination when copying or moving.
Use a small test file before applying a command to a whole project. Check a command's exit status when something seems wrong, since a zero status usually means success and a nonzero status signals a problem.
Help pages can be long, but the first lines normally show the command form and common options. This habit becomes especially important on remote computers, where a typo can change files that are not easily recovered.