Command Line Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which component is the lowest level of user-mode software that allows users to start other programs?

  • Operating System
  • User Interface Program (Shell/GUI) (correct)
  • Hardware
  • Kernel

What role does the 'command interpreter' or 'shell' play in the command line environment?

  • It compiles user programs into executable code.
  • It reads and interprets commands from a terminal. (correct)
  • It directly interacts with the kernel to allocate memory.
  • It manages the hardware resources of the system.

After a user types a command like date in the shell, what is the shell's immediate next action?

  • The shell creates a child process to run the `date` program. (correct)
  • The shell terminates itself.
  • The shell directly executes the `date` command without creating a new process.
  • The shell updates its internal configuration files.

What character typically indicates that the shell is ready to accept a command?

<p>Dollar sign ($) (D)</p> Signup and view all the answers

By default, which type of files or directories does the ls command NOT display?

<p>Files that begin with a period (.) (D)</p> Signup and view all the answers

Which ls option displays all files, including hidden files?

<p><code>ls -a</code> (A)</p> Signup and view all the answers

To display files in the order of most recently modified to least recently modified, which ls option should be used?

<p><code>ls -t</code> (D)</p> Signup and view all the answers

How do you represent spaces in filenames when using the ls command?

<p>Enclosing the filename in single quotes (' ') or escaping spaces with a backslash () (A)</p> Signup and view all the answers

Which command allows you to browse through a text file, advancing one page at a time using the Spacebar?

<p><code>more</code> (C)</p> Signup and view all the answers

Which command is like more, but also allows backward movement and pattern searches within the file?

<p><code>less</code> (B)</p> Signup and view all the answers

Which command displays only the beginning portion of a file?

<p><code>head</code> (B)</p> Signup and view all the answers

Which command would you use to sort the contents of a file?

<p><code>sort</code> (B)</p> Signup and view all the answers

Which command displays the differences between two files in a side-by-side format, highlighting the discrepancies?

<p><code>sdiff</code> (B)</p> Signup and view all the answers

How can you redirect the standard output of a command to a file, overwriting the file if it already exists?

<p><code>command &gt; file</code> (A)</p> Signup and view all the answers

What does the >> operator do when redirecting output to a file?

<p>It appends the output to the end of the file. (B)</p> Signup and view all the answers

How can you use a file as standard input for a command?

<p><code>command &lt; file</code> (C)</p> Signup and view all the answers

What is the file descriptor number assigned to standard error?

<p>2 (C)</p> Signup and view all the answers

If you want to discard the error messages from a command and not display them, what redirection should you use?

<p><code>2&gt;/dev/null</code> (A)</p> Signup and view all the answers

What is a 'pipe' in the context of command-line operations?

<p>A pseudofile used to connect the output of one process to the input of another (B)</p> Signup and view all the answers

How do you connect the output of one command to the input of another command using a pipe?

<p>Using the <code>|</code> (pipe) operator (D)</p> Signup and view all the answers

Which command clears the terminal screen?

<p><code>clear</code> (C)</p> Signup and view all the answers

What command shows the available disk space on the system?

<p><code>df</code> (A)</p> Signup and view all the answers

What command displays the amount of free memory on the system?

<p><code>free</code> (C)</p> Signup and view all the answers

In UNIX file permission codes, what does 'rwx' represent?

<p>Read-Write-Execute permissions (B)</p> Signup and view all the answers

In the context of directory permissions, what does 'x' indicate?

<p>Search permission within the directory. (C)</p> Signup and view all the answers

How can you view the permission settings for a file in the command line?

<p><code>ls -l</code> (A)</p> Signup and view all the answers

What command is used to change the permissions of a file or directory?

<p><code>chmod</code> (B)</p> Signup and view all the answers

If you want to add write permission for the group to a file named myfile.txt, which command would you use?

<p><code>chmod g+w myfile.txt</code> (C)</p> Signup and view all the answers

What is the effect of the command chmod a= on a file?

<p>Removes all permissions for all users. (B)</p> Signup and view all the answers

In the context of command-line wildcards, what does the asterisk (*) represent?

<p>Matches any sequence of characters (D)</p> Signup and view all the answers

What is a symbolic link (symlink)?

<p>A pointer to another file or directory (C)</p> Signup and view all the answers

What is the purpose of the pwd command?

<p>Print working directory (B)</p> Signup and view all the answers

How do you navigate to the parent directory in the command line?

<p><code>cd ..</code> (C)</p> Signup and view all the answers

What command creates a new directory?

<p><code>mkdir</code> (A)</p> Signup and view all the answers

What command creates a new, empty file?

<p><code>touch</code> (B)</p> Signup and view all the answers

What command is used to rename a file or directory?

<p><code>mv</code> (D)</p> Signup and view all the answers

Which command is used to copy files?

<p><code>cp</code> (D)</p> Signup and view all the answers

How do you remove a non-empty directory using the command line?

<p><code>rm -r directory_name</code> (D)</p> Signup and view all the answers

Which command is used to locate files or directories on a Linux system based on various criteria?

<p><code>find</code> (C)</p> Signup and view all the answers

What is an environment variable?

<p>A name-value pair that can affect the behavior of running processes (A)</p> Signup and view all the answers

Which command is used to display all the environment variables?

<p><code>env</code> or <code>printenv</code> (B)</p> Signup and view all the answers

Which environment variable typically stores the user's home directory?

<p><code>HOME</code> (A)</p> Signup and view all the answers

What command lists the currently running processes?

<p><code>ps</code> (D)</p> Signup and view all the answers

What command is used to terminate a process, given its PID?

<p><code>kill</code> (C)</p> Signup and view all the answers

Flashcards

Command interpreter/Shell

A program that reads commands from a terminal.

Prompt

A character or symbol indicating the shell is ready.

ls Command

Lists files in current directory (or specified directory).

ls --help Command

Displays help/usage information for other commands.

Signup and view all the flashcards

ls -a Command

Shows all the files, including hidden ones, in a list.

Signup and view all the flashcards

ls -t Command

Sorts the ls command output by modification time.

Signup and view all the flashcards

cat Command

Displays file contents.

Signup and view all the flashcards

more Command

Browse through a file page by page.

Signup and view all the flashcards

less Command

Browse a file, allowing backward movement and searches.

Signup and view all the flashcards

head Command

Displays the beginning portion of a file.

Signup and view all the flashcards

tail Command

Displays the ending portion of a file

Signup and view all the flashcards

sort Command

Sorts the text in a file.

Signup and view all the flashcards

diff Command

Shows file differences.

Signup and view all the flashcards

sdiff Command

Shows files side-by-side, highlighting differences.

Signup and view all the flashcards

Operator

Overwrites standard output to file.

Signup and view all the flashcards

Operator

Appends/adds standard output to a file.

Signup and view all the flashcards

< Operator

Uses file contents as standard input to a command.

Signup and view all the flashcards

Pipe (|)

Connects the output of one command to the input of another.

Signup and view all the flashcards

clear Command

Clears the terminal screen.

Signup and view all the flashcards

df Command

Shows available disk space.

Signup and view all the flashcards

free Command

Shows the total amount of free memory.

Signup and view all the flashcards

ls -l Command

Used to see the permission settings of a file.

Signup and view all the flashcards

chmod Command

Command to change file permissions.

Signup and view all the flashcards

Asterisk (*)

Matches anything in a command's arguments.

Signup and view all the flashcards

Question Mark (?)

Matches a single character.

Signup and view all the flashcards

Symbolic Link (symlink)

A pointer to an actual file or directory.

Signup and view all the flashcards

pwd Command

Prints the current directory.

Signup and view all the flashcards

cd Command

Changes the current directory.

Signup and view all the flashcards

cd .. Command

Goes to the parent directory.

Signup and view all the flashcards

cd ~ Command

Goes to the user's home directory.

Signup and view all the flashcards

mkdir Command

Creates a new directory.

Signup and view all the flashcards

touch command

Creates a new empty file.

Signup and view all the flashcards

mv Command

Moves files and/or directories, can also rename.

Signup and view all the flashcards

cp Command

Copies files.

Signup and view all the flashcards

rmdir Command

Removes an empty directory.

Signup and view all the flashcards

rm -rf Command

Removes files or non-empty directories.

Signup and view all the flashcards

find command

Finds files and directories

Signup and view all the flashcards

man Command

Displays the online manual for specified command.

Signup and view all the flashcards

env Command

Displays all environment variables.

Signup and view all the flashcards

ps

Lists actively running processes.

Signup and view all the flashcards

Study Notes

Command Line Basics

  • The user interface program (shell or GUI) is the lowest level of user-mode software.
  • This level allows users to start other programs like web browsers or music players.

Command Interpreter (Shell)

  • A process called the command interpreter or shell reads commands from a terminal.
  • Once a user types a command (e.g., requesting program compilation), the shell creates a new process to run the compiler.
  • After the compilation finishes, the process executes a system call to terminate itself.

Shell Input and Output

  • The shell uses the terminal as standard input/output.
  • It displays a prompt (e.g., a dollar sign) to indicate it's ready for commands.
  • For instance, when a user types date, the shell creates a child process to run the date program.
  • The shell waits for the child process to terminate.
  • After the child process finishes, the shell displays the prompt again to read the next input line.

Basic Commands

  • ls: Lists files in the current directory.
  • ls --help: Displays help information on the use of the ls command.
  • ls /home: Lists files in the /home directory.
  • ls /: Lists files in the root directory.
  • ls Documents/ Downloads/: Lists the contents of the Documents and Downloads directories.

ls Command Options

  • By default, ls does not show files or directories starting with a period, as they are considered hidden in Linux.
  • ls -a: Displays all files, including hidden ones.
  • ls -t: Sorts the output of the ls command by time, showing the most recently modified items first.
  • ls -rt: Sorts the output of the ls command by time in reverse order.

Filenames with Spaces

  • ls -l 'my to do list.txt' or ls -l my\ to\ do\ list.txt: Correctly lists files with spaces in their names.

File Content Commands

  • cat: Concatenates or displays files.
  • more: Allows browsing through a text file.
    • Press Spacebar to advance to the next page.
    • Press Enter to advance to the next line.
    • Type q to quit viewing the file.
  • less: Similar to more, but allows backward movement and pattern searches.
  • head: Displays the beginning portion of a file.
  • tail: Displays the ending portion of a file.

Sorting and Comparing Files

  • diff: Displays the differences between files.
  • sdiff: Displays two files side-by-side, highlighting the differences.

Standard Input/Output Redirection

  • date > date.txt: Redirects standard output to a file named date.txt.
  • >>: Used to append output to a file.
  • sort < fruits.txt: Standard input is redirected.
  • sort < fruits.txt > outfile.txt
  • ls info.txt nofile.txt 2> errors.txt:
  • ls info.txt nofile.txt 1> out.txt 2> errors.txt:
  • ls info.txt nofile.txt > out.txt 2>&1: Redirects one file descriptor to another.
  • ls info.txt nofile.txt 2> /dev/null: Redirects output to the null device, preventing it from displaying or saving.

Pipes

  • A pipe is a pseudofile that connects two processes.
  • Processes communicate through a pipe.
  • Process A writes data to process B.
  • Process B reads data in UNIX, like ordinary file reads/writes.
  • cat fruits.txt vegetables.txt | sort: Connects cat and sort with a pipe.
  • cat fruits.txt vegetables.txt | sort > out.txt: Connects cat and sort with a pipe that redirects to out.txt

Screen, Disk Space, and Memory Commands

  • clear: Clears the contents of the screen.
  • cat fruits.txt \ vegetables.txt | sort > out.txt:
  • df: Shows available disk space.
  • free: Shows the amount of free memory.

File Permissions

  • Before reading or writing a file, it must be opened, and permissions are checked.
  • UNIX protects files with a 9-bit binary protection code.
  • The code contains three 3-bit fields.
    • Owner.
    • Owner's group.
    • Everyone else.
  • Each has read, write, and execute access bits.
  • The three bits are known as rwx bits
  • Example: rwxr-x--x
    • The owner can read/write/execute the file.
    • Group members can read/execute.
    • Everyone else can execute.
  • For a directory, x gives search permission.
  • A dash means that the permission is absent.
  • ls -l: Shows permission settings for a file.

File Type

  • First character is the type of the file.

Changing Permissions

  • Permissions are also known as modes.
  • chmod (change mode) changes permissions.
  • chmod g+w somefile.txt: Adds write permission for the group.
  • chmod g-w somefile.txt: Removes write permission for the group.
  • chmod g+wx somefile.txt: Adds write and execute permissions for the group.
  • chmod ug+wx somefile.txt: Adds write and execute permissions for the user and group.
  • chmod u=rwx,g+x somefile.txt: Sets read, write, and execute permissions for the file owner and adds execute permission for the group.
  • chmod a=r somefile.txt: Sets the file to be readable by everyone.
  • The equal sign (=) replaces current permissions.
  • chmod a=r,a=w somefile.txt produces different results to chmod a=w,a=r somefile.txt
  • chmod a= somefile.txt: Removes permissions following the equal sign.

Wildcards

  • ls m*: Lists files starting with m.
  • ls *.txt: Lists files with .txt extension.
  • The asterisk matches anything, while the question mark matches a single character.
  • ls ??????.txt
  • A symlink points to an actual file or directory.
  • The symlink acts as a pointer.
  • Symbolic links create shortcuts to long names or paths.
  • Another common use for symlinks helps point to an application's current version.

Working Directory

  • Each process at every instance has a current working directory.
  • Path names not beginning with a slash are looked for.
  • pwd: Print working directory
  • System call changes working directory using the: cd /usr/share/doc
  • Change directory (absolute path): cd doc
  • Go to parent directory: cd ..
  • Go home: cd ~
  • The tilde represents the current user's home directory

Directory and File Manipulation

  • mkdir test: Creates a directory called test.
  • cd test: Navigates to the test directory.
  • touch newfile.txt: Creates a new empty file.
  • mv newfile.txt ..: Moves or renames file or directories to the parent directory.
  • mv newfile.txt test: Moves file back to the directory test.
  • ls test: Checks that the file is back in the directory test.
  • mv newfile.txt mynewfile.txt: Renames the file.
  • mv test mydir: Renames the directory.
  • cp mynewfile.txt myoldfile.txt: Copies files.
  • cp info.txt Documents: Copies info.txt into the Documents directory.

Directory Operations

  • cp -a mydir Documents: Copies a whole directory.
  • rmdir mydir: Removes an empty directory.
  • rm -rf mydir: Removes a non-empty directory.

Finding Files

  • find /etc -name l*: example of finding files

Find Command

  • Locates files or directories on a Linux system.

Man Pages

  • The man command: displays the online manual for a given command.

Environment Variables

  • An environment variable is a name-value pair.
  • Programs determine their actions.
  • env: To display all the environment variables

Processes

  • ps: To Lists the currently running processes.
  • ps -e | less: To see every process running on the system.
  • kill 2994: To kill the process, using it's PID.
  • kill -9 2994: Kills the process if it's still running.

Superuser

  • su: Become the superuser.
  • su -c pwd: Execute a command as a superuser.
  • su -c 'ls -l /': Quoted command to execute if the command is more than one word in length.
  • whoami: To show what user you are currently working as

Sudo Command

  • Allows you to run a command with the security privileges of another user.
  • Sudo uses the superuser in no username is specified.
  • sudo ls: example to "run ls as root user".
  • sudo -l: List commands to execute with sudo
  • sudo -u someuser command: Allows you to Run command as another user
  • sudo su: Switch to the superuser account

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Module 5: Command Line Skills
30 questions

Module 5: Command Line Skills

PanoramicMountainPeak6704 avatar
PanoramicMountainPeak6704
Linux Command Line Basics Quiz
91 questions
Use Quizgecko on...
Browser
Browser