Linux Command Line Tutorial PDF

Summary

This document provides an overview of various Linux commands and concepts, including shell basics, echo commands, basic commands (ls, cd, cat, etc.), file management, directory navigation, date and calendar commands, working with history and arrow keys, creating and viewing files, expansions, processes and utilities and more. It explains usage and syntax with examples for various commands and concepts.

Full Transcript

Here's an explanation of each topic in relation to the Linux terminal: Shell Basics 1. Brace Expansion: Used to generate multiple strings. Example: echo {A,B,C} outputs A B C. 2. Parameter Expansion: Replaces variables with their values. Example: echo $HOME prints the home direc...

Here's an explanation of each topic in relation to the Linux terminal: Shell Basics 1. Brace Expansion: Used to generate multiple strings. Example: echo {A,B,C} outputs A B C. 2. Parameter Expansion: Replaces variables with their values. Example: echo $HOME prints the home directory. 3. Command Substitution: Embeds the output of one command into another. Example: echo "Today is $(date)". 4. Escape Characters: Characters like \ are used to escape special characters. Example: echo "Line1\nLine2" (with -e flag). 5. Double Quotes: Preserve spaces and special characters except $ and \. Example: echo "Hello $USER". 6. Single Quotes: Preserve literal value of all enclosed characters. Example: echo 'Hello $USER' outputs Hello $USER. Echo Command 1. Using echo to print simple strings: Example: echo "Hello, World!". 2. Using echo to display multi-line messages: Use -e and newline (\n). Example: 3. echo -e "Line1\nLine2" 4. Using echo to display messages with escape sequences: Example: 5. echo -e "Tab\tspace" Basic Commands 1. ls and cd: o ls lists files in a directory. Options: ▪ ls -l for detailed list ▪ ls -a for hidden files o cd changes directory. Example: cd /path/to/directory. 2. cat: Concatenates and displays file contents. Example: cat file.txt. 3. uniq: Filters out duplicate lines from sorted input. Example: sort file.txt | uniq. 4. wc: Counts lines, words, and characters in a file. Example: wc file.txt. 5. grep: Searches for patterns in files. Example: grep "pattern" file.txt. 6. head and tail: Display the beginning or end of a file. o Example: head -n 5 file.txt (first 5 lines). o Example: tail -n 5 file.txt (last 5 lines). File Management 1. mv: Moves or renames files. Example: mv oldfile.txt newfile.txt. 2. cp: Copies files. Example: cp file1.txt file2.txt. 3. ln: Creates links. Example: ln -s target link_name (symbolic link). 4. rm: Deletes files or directories. Example: rm file.txt. 5. file: Displays file type. Example: file file.txt. Directory Navigation 1. cd to navigate using relative paths: Example: cd../dir_name. 2. Understanding. and..: o. represents the current directory. o.. represents the parent directory. 3. pwd: Prints the current working directory. Date and Calendar 1. cal: Displays the calendar. Example: cal 2024. 2. date: Prints the current date and time. Example: date "+%Y-%m-%d %H:%M:%S". Working with History and Arrow Keys 1. Navigate bash history using arrow keys: Use ↑ and ↓ to browse command history. 2. Navigate left and right using arrow keys: Use ← and → to edit the current command. Creating and Viewing Files 1. mkdir: Creates directories. Example: mkdir new_folder. 2. less: Views file content one screen at a time. Example: less file.txt. Expansions 1. Wildcard Expansion: Matches filenames. Example: ls *.txt lists all.txt files. 2. Pathname Expansion: Expands directory paths. Example: echo /usr/*. 3. Tilde Expansion: Expands ~ to the home directory. Example: cd ~/Documents. 4. Arithmetic Expansion: Performs arithmetic operations. Example: echo $((2+3)). Processes and Utilities 1. Types of Processes: o Foreground: Runs interactively. o Background: Runs with &. o Daemons: Run continuously in the background. 2. VI Editor: A text editor. Basic commands: o i to insert text. o :w to save. o :q to quit. 3. Shell Scripting: Writing scripts to automate tasks. Example: 4. #!/bin/bash 5. echo "Hello, World!" 6. Virtual Terminal: A terminal emulator to access shell commands. File Transfer and Communication 1. FTP (File Transfer Protocol): Transfers files between computers. Example: ftp host. 2. Basic Environment Variables: Variables like $PATH or $HOME. 3. Communication: Tools like write and wall for messaging users. Regular Expressions Used for pattern matching with tools like grep. Example: grep '^A.*z$' file.txt matches lines starting with A and ending with z. Exit Commands 1. exit: Exits the terminal session. UNIX Administration and PERL Programming 1. Unix Administration: Managing users, permissions, and processes. 2. PERL Programming: A scripting language for text processing. Let me know which topic you’d like to dive deeper into! 1. What happens after the command? echo "I'd like "{green,blue,red}" color" a. This command uses brace expansion. It outputs three strings by substituting {green,blue,red} with each value in the set: 2. What happens after the command? mkdir {2020..2021}-{01..12} a. This command uses brace expansion to create a sequence of directories. It generates directories for all months of the years 2020 and 2021, resulting in: 2020-01, 2020-02,..., 2020-12, 2021-01,..., 2021-12 3. What happens after the command? ls *.jpg | sort a. Lists all.jpg files in the current directory and sorts them alphabetically. 4. What is the difference? echo $USER and echo \$USER a. echo $USER: Displays the username of the current user (e.g., john). b. echo \$USER: Displays the literal string $USER. 5. What happens after the command? echo "ls -l" a. Outputs the string ls -l without executing it. 6. What is the difference? echo ~ and echo "~" a. echo ~: Prints the current user's home directory (e.g., /home/username). b. echo "~": Prints the literal string ~. Linux Command Line Questions and Answers 1. What does the pipeline | do? The pipeline symbol '|' is used to pass the output of one command as input to another command. This is helpful for chaining commands together, allowing complex operations in a single line. 2. What does the grep command do? The 'grep' command searches for a specified pattern in a file or output and prints lines that match the pattern. It's commonly used to filter results based on keywords. 3. What does this command do? head -n 5 apple.txt This command displays the first 5 lines of the file 'apple.txt'. The '-n' option specifies the number of lines. 4. What does this command do? tail -n 3 apple.txt This command displays the last 3 lines of the file 'apple.txt'. The '-n' option specifies the number of lines from the end of the file. 5. Which lines of apple.txt are printed by the following compound command? head apple.txt -n 5 | tail -n 3 This command first takes the first 5 lines of 'apple.txt' and then outputs the last 3 of those lines, effectively printing lines 3 to 5. 6. What does the * joker character mean? e.g. ls *.jpg The '*' wildcard represents any number of characters in filenames. For example, 'ls *.jpg' lists all files with a '.jpg' extension in the current directory, regardless of their names. 1. What does the ~ sign mean? For example: Is~pictures a. In Linux, the ~ (tilde) symbol represents the home directory of the current user. This command attempts to list the contents of a folder named pictures inside the home directory. 2. Move all jpg files from the current(local) directory to the “pictures” subdirectory. a. To move all.jpg files from the current directory to the "pictures" subdirectory, you can use the mv command mv *.jpg pictures/ This command moves all files with the.jpg extension to the pictures folder, assuming that pictures is a subdirectory of the current directory. 3. What is the difference between mv and cp command? a. mv (move): Moves or renames files and directories. When you use mv, the original file no longer exists in its original location. For example, mv file1 /path/to/destination moves file1 to the specified destination. b. cp (copy): Copies files and directories, leaving the original intact. When you use cp, a duplicate is created. For example cp file1 /path/to/destination copies file1 to the destination, leaving the original in place. 4. Can you delete a directory with the rm command? a. Yes, you can delete a directory with the rm command, but you need to use the -r (recursive) option E.g rm -r directory_name 5. Which command do you use to create an empty file called "apple"? a. To create an empty file called "apple," use the touch command touch apple 1) What does the command print to the screen? echo $HOME a) The command echo $HOME prints the path to the current user's home directory. E.g /home/username 2) Remove files a) To remove files, you can use the “rm” command. rm filename Replace filename with the name of the file you want to delete. To remove multiple files, you can list them separated by spaces 3) How do you jump straight to your home directory a) To jump straight to your home directory, use the cd command without any arguments. “cd” or “cd ~” 4) Format and display help pages. a) To format and display help pages in the Linux terminal, use the “man” command, which stands for "manual." E.g man command_name 5) What is different between the absolute and relative path? a) Absolute Path: An absolute path specifies the full path from the root directory (/). It starts with a /, such as /home/user/documents. Absolute paths always provide the exact location in the filesystem, regardless of the current directory. b) Relative Path: A relative path specifies the path relative to the current working directory. It does not start with / and instead refers to a location in the context of where you currently are. For example, if you are in /home/user, a relative path to "documents" would be documents or./documents. 1. The default action is to show the current folder as an absolute path. Answer: The pwd (print working directory) command shows the full, absolute path of the current working directory 2. Write "I Like Linux" on the screen (write down the specific command with the appropriate parameter) Answer: echo "I Like Linux" The echo command outputs text to the terminal, making it simple to display messages or text. 3. Create new "pictures" folder (Write down the specific command with the appropriate parameter) Answer: mkdir pictures This command creates a new directory called "pictures" in the current directory. mkdir /path/to/location/pictures This will create the "pictures" folder in the specified location 4. What does the "ls" command do? Answer: The “ls” command in Linux is used to list files and directories in the current directory or a specified directory. ls -la This will show a detailed list of all files, including hidden ones, in the current directory. 5. Change the current working directory to a specific Folder (which command?) Answer: cd Replace with the path of the folder you want to navigate to 1) What is a "package" and what is it for? a) A package is a self-contained unit of software that includes all the files, configuration settings, and dependencies needed for an application to run. Software is installed in the form of packages. A package contains only the program itself. Any dependent components need to be downloaded separately. 2) What is the "FTP"? a) FTP(File Transfer Protocol) is a standard network protocol used to transfer files between a client and a server over a 3) What does the "cat" command do? a) The cat command stands for "concatenate" and is primarily used to display the contents of a file to the terminal 4) What does the "echo" command do? a) The echo command is used to display text or variables on the terminal. 5) What does the "ping" command do? a) The ping command is a network utility used to test the reachability of a host on an IP network. 1) What is the Virtual Box ? a) A virtual box is a software virtualization program that creates a Virtual Machine where the user can run another OS(linux) on an existing operatring system already installed on your computer. 2) What are the benefits of a virtual machine? a) It allows the user to have multiple OS installations at the same time. b) Virtual Machines give the option to try new distros without completely installing them. c) VM’s are cost-effective d) They’re open source nature provides constant security updates and choice of updates 3) What does the "sudo" command mean? a) This is a command that allows users to temporarily elevate their privileges to a root user to complete a certain task without logging in as the root user. 4) What it means ? "drwxrwxrwx"? a) This is a file permission mode that represents the permissions for a directory. Drwxrwxrwx indicates a directory with full read, write and execute permissions for owner, group and all other users. ‘d’: directory First ‘rwx’: permissions for directory owner Second ‘rwx’: permissions for the group the directory belongs to Third/final ‘rwx’: represents permission for all other users 5) What does mean in permission management the "u g o a" ? a) U means “user” b) G means “group” c) O means “Others” 1. Assignment to submit 1. What does the GPL mean? Answer> GPL Means General Public License 2. Who created the Linux operating system? Answer> The Linux Operating System was created by Linus Torvalds 3. Write the names of some Linux distributions. Answer> Ubuntu, POP OS, Arch Linux, Debian, Parrot OS etc. 4. What is your personal opinion on why Linux is secure? Answer> In my opinion Linux is secure because it is open source, and is on constant review. Hence, errors are easily found and corrected. Another reason for their notable security is the built-in security system in the Linux kernel.

Use Quizgecko on...
Browser
Browser