GX3 The Power of the Command Line Flashcards
3.1: Archiving Files on the Command Line
What command is used to create, extract, and manipulate archive files in Linux?
The tar command.
3.1: Archiving Files on the Command Line
How do you create a tar archive of a directory named mydir
tar -cvf mydir.tar mydir
3.1: Archiving Files on the Command Line
What do the common tar options mean?
-c → Create an archive
-v → Verbose (list files being processed)
-f → Specify archive file name
-x → Extract files
-t → List contents of an archive
3.1: Archiving Files on the Command Line
How do you compress a file using gzip?
gzip filename (creates filename.gz)
3.1: Archiving Files on the Command Line
How do you extract a .tar.gz archive?
tar -xvzf archive.tar.gz
3.1: Archiving Files on the Command Line
What commands are used to work with .zip files?
Create: zip archive.zip file1 file2
Extract: unzip archive.zip
3.2: Searching and Extracting Data from Files
How do you search for a specific word in a file?
Use grep. Example: grep “word” filename
3.2: Searching and Extracting Data from Files
What is the purpose of less?
It allows you to view a file one page at a time. Example: less filename
3.2: Searching and Extracting Data from Files
What command outputs the first 10 lines of a file?
head filename
3.2: Searching and Extracting Data from Files
What command outputs the last 10 lines of a file?
tail filename
3.2: Searching and Extracting Data from Files
How do you sort the lines of a file alphabetically?
Use sort. Example: sort filename
3.2: Searching and Extracting Data from Files
What command extracts specific columns from a file?
cut. Example: cut -d’,’ -f2 filename (extracts the second column using , as a delimiter)
3.2: Searching and Extracting Data from Files
How do you count the number of words, lines, or characters in a file?
Use wc. Example:
wc -l filename (line count)
wc -w filename (word count)
wc -c filename (character count)
3.2: Searching and Extracting Data from Files
What is I/O redirection, and how is it used?
> redirects output to a file (overwrite). Example: echo “Hello” > file.txt
> appends output to a file. Example: echo “World”»_space; file.txt
< takes input from a file. Example: sort < file.txt
3.2: Searching and Extracting Data from Files
What are pipes (|), and how are they used?
Pipes allow output of one command to be used as input for another. Example: cat file.txt | grep “keyword”
3.3: Turning Commands into a Script
What is a shell script?
A shell script is a file containing a series of commands that can be executed together.
3.3: Turning Commands into a Script
What is the shebang (#!)?
It defines the interpreter for a script. Example: #!/bin/bash
3.3: Turning Commands into a Script
How do you create a simple shell script?
1.Open a text editor (e.g., nano script.sh)
2.Add #!/bin/bash at the top
3.Write commands
4.Save and give execution permission (chmod +x script.sh)
5.Run it (./script.sh)
3.3: Turning Commands into a Script
How do you define a variable in a shell script?
MYVAR=”Hello” (No spaces around =)
3.3: Turning Commands into a Script
How do you use command-line arguments in a script?
$1, $2, etc., represent arguments. Example: echo “First argument: $1”