The command line
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A terminal is a window for typing commands, and the shell inside it reads each line, runs it and prints the answer. Watch the shell stand in one folder of the file system tree while commands look around (pwd, ls), move (cd), and make, copy, move and delete files. Then join commands with pipes and redirects, see how PATH finds programs, and learn where to get help.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01You are in /home/sam. Which command takes you into /home/sam/photos using a relative path?
A relative path starts from where you are now, so cd photos walks from /home/sam into photos. /photos is absolute: it would look for a folder called photos at the root.
02You want a second copy of notes.txt called notes-old.txt. Which command does it?
cp and mv both take the source first, then the destination. cp notes.txt notes-old.txt makes a copy and keeps the original; mv would rename it, so only one file would be left. The first option would overwrite notes.txt.
03You ran rm todo.txt by mistake. What happens?
rm deletes the file's name straight away. It does not use a Trash or Recycle Bin and the shell has no undo. rm -i asks before each file, which helps you avoid this.
04What does ls | wc -l do?
A pipe | connects one command's output to the next command's input. Into a pipe, ls writes one name per line, and wc -l counts lines.
05You type ls and press Enter. How does the shell find the ls program?
PATH is an environment variable holding a list of folders separated by colons, such as /usr/bin:/bin. The shell tries each one in turn; here it finds /usr/bin/ls.
المسرد
- Terminal
- A window for typing text commands and reading what comes back.
- Shell
- The program inside the terminal that reads each command line, runs it and prints the result. bash and zsh are shells.
- Working directory
- The folder the shell is in right now. Relative paths start from here; pwd prints it and cd changes it.
- Path
- Where a file is in the folder tree, as folder names joined by /. An absolute path starts at the root, /; a relative path starts from the working directory.
- Option (flag)
- A word starting with a dash that changes what a command does, such as -a in ls -a.
- Pipe
- The | symbol: it sends one command's output into the next command's input.
- Redirect
- The > symbol: it sends a command's output into a file instead of the screen, replacing what the file held.
- Environment variable
- A named setting that programs the shell starts receive, such as HOME (your home folder) or PATH (where to look for commands).
الملاحظات والمصادر
- The tree is cut down to the folders this lesson needs. A real Linux system has many more at the top, such as /bin, /etc and /tmp (Filesystem Hierarchy Standard), and a home folder holds hidden files whose names start with a dot.
- The outputs are what bash and the GNU tools print on Linux; this page computes them with a tiny model of the shell. macOS uses zsh by default and BSD versions of ls, wc and others, so some options and spacing differ.
- Strictly, rm removes a name (a directory entry), and the space is freed once nothing else uses the file. Recovery tools sometimes find old data, but treat rm as permanent. Desktop file managers move files to a Trash; rm does not.
- With >, the shell creates the file, or empties it, before the command even runs. >> adds to the end instead of replacing.
- which is not part of the POSIX standard but comes with most Linux and macOS systems; command -v ls is the standard way to ask the shell.
- ls --help prints a long guide; the figure shows only its first line. Not every command accepts --help (the ls on macOS does not), but most have a manual page.
- MIT, The Missing Semester of Your CS Education: Lecture 1, Course overview + the shell
- GNU Bash Reference Manual
- GNU Coreutils manual (ls, cp, mv, rm, mkdir, pwd, cat, wc)
- POSIX.1-2017 Base Definitions, 4.13 Pathname Resolution
- POSIX.1-2017 Base Definitions, 8 Environment Variables
- POSIX.1-2017 Shell Command Language (pipelines, redirection)
- less(1), Linux manual page
- man(1), Linux manual page
- Filesystem Hierarchy Standard 3.0
اختبار يتاح بعد الخطوة الأخيرة.