Introduction to Shell for Data Science Flashcards
https://www.datacamp.com/courses/introduction-to-shell-for-data-science
Which command lets you find out where you are in the filesystem?
pwd
Which command lets you list the content of your directory?
ls
Which command lets you list the content of a specific directory?
ls /path/to/file
What is the conceptual difference between asbolute and relative paths?
An absolute path is like a latitude and longitude: it has the same value no matter where you are.
A relative path, specifies a location starting from where you are: it’s like saying “20 kilometers north”.
How does the shell decide if a path is relative or absolute?
An absolute path starts with a /.
A relative path doesn’t.
Which command lets you move around the filesystem?
cd
What is the parent of a directory?
The parent of a directory is the directory above it. For example, /home is the parent of /home/repl, and /home/repl is the parent of /home/repl/seasonal.
Which special path corresponds to the one above the one you’re currently in?
..
Which path is the root directory?
/
Which path corresponds to the current directory?
.
Which path corresponds to the home directory?
~
Which command lets you copy files?
cp path/original.txt path/duplicate.txt
How do you copy several files to a directory?
cp original1.txt original2.txt path/to/directory
Which command lets you move files from on directory to another?
mv path/to/file1.txt path/to/file2.txt path/to/directory
Which command lets you rename a file?
mv old_name.txt new_name.txt
Which command lets you remove files?
rm file/to/file1.txt file/to/file2.txt
What happens if you move or copy to an existing filename?
The original file with that filename is overwritten.
Which command lets you remove an empty directory?
rmdir directory_name
Which command lets you create a directory?
mkdir directory_name
Where should you store files and programs you only need briefly?
/temp
Which command lets you look at the content of files?
cat file1.txt file2.txt
When printing the content of a file, how do you go to the next page?
Press the spacebar
When printing the content of a file, how do you quit?
Press q
When printing the content of several files, how do you move to the next file?
Type :n
When printing the content of several files, how do you move to the previous file?
Type :p
When printing the content of several files, how do you quit?
Type :q
Which command lets you print the first 10 lines of a csv file?
head file.csv
What are command-line flags?
They are a way to specify options for command-line programs. For example, head -n 5 file.csv specifies head should print 5 lines instead of the default 10.
By convention, where should command-line flags be placed?
By convention, command-line flags should be placed before any filenames.
How do you display everything that is underneath directory?
ls -R
When listing files, how do you make it clear which ones are directory names and which ones are runnable programs?
ls -F
Which command lets you obtain documentation on a specific command?
man command
Which command lets you display columns?
cut
When selecting columns, which flag lets you specify the column delimiter?
-d ,
(for columns separated by commas)
When selecting columns, which flag lets you specify the columns or range of columns to select?
-f 1-4 7
(columns 1 to 4 and 7)
When displaying the content of a file, how do you skip the first x lines?
-n +x
Which command lets you display a list of commands you have run recently?
history
How do you rerun your most recent use of a command?
!command
How do you rerun the xth command in your history?
!x
How do you display lines of files containing a specific piece of text?
grep keyword path/to/file
With grep, which flag lets you print a count of matching lines rather than the lines themselves?
grep -c
With grep, which flag lets you not print the names of the files when searching multiple files?
grep -h
With grep, which flag lets you ignore case?
grep -i
With grep, which flag lets you print the names of the files that contain the matches, rather than the matches themselves?
grep -l
With grep, which flag lets you print only lines that don’t match?
grep -v
Which command lets you combine files?
paste file1.csv file2.csv
How do you save a command’s output?
Use redirection:
tail -n 5 file.csv > last5.csv
Which command lets you print the number of characters, words, and lines in a file?
wc
Which wc flag lets you print the number of characters only?
wc -c
Which wc flag lets you print the number of words only?
wc -w
Which wc flag lets you print the number of lines only?
wc -l
What is a wildcard and why are they useful?
A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.
Which wildcard matches 0 or more characters?
*
Which wildcard matches a single character?
?
Which wildcard matches any one of the characters passed?
[characters]
Which wildcard matches any of the comma-separated patterns passed?
{pattern1, pattern2}
Which command lets you order values?
sort
Which sort flag lets you sort in numerical order instead of the default alphabetical order?
-n
Which sort flag lets you sort in descending order instead of the default ascending order?
-r
Which sort flag lets you ignore leading blanks?
-b
Which sort flag lets you fold case (be case-insensitive)?
-f
Which command lets you remove adjacent duplicated lines?
uniq
Which uniq flag lets you display the count of duplicated lines?
-c
In a command expression, where should the command writing to a file appear?
At the very beginning or at the very end.
How do you interrupt a runing command?
Ctrl + C
Which environment variable corresponds to the user’s home directory?
HOME
Which path corresponds to HOME?
/home/repl
Which environment variable corresponds to the present working directory?
PWD
Which environment variable corresponds to the shell program being used?
SHELL
Which path corresponds to SHELL?
/bin/bash
Which environement variable corresponds to the user’s ID?
USER
Which command lets you print the value of an environment variable?
printenv ENVIRONMENT_VARIABLE
or
echo $ENVIRONMENT_VARIABLE
Which command lets you display a list of all environment variables?
set
How do you create a shell variable?
variable=value
without spaces before or after =
How do you print the value of a variable?
echo $variable
How do you define a for loop?
for variable in list ; do command ; done
How do you indicate a variable name instead of the name you have typed.
$
(use a dollar in front of the variable, eg echo $variable)
If you have filenames with spaces, how do you work around that so your command interprets filenames correctly?
Wrap filenames in single or double quotes (‘’ or “”).
How do you separate several commands in the body of a for loop?
;
How do you open a file with nano?
nano filename
How do you delete a line with nano?
Ctrl + K
How do you undelete a line with nano?
Ctrl + U
How do you save the file with nano?
Ctrl + O
How do you exit the editor with nano?
Ctrl + X
How do you create a file with nano?
nano filename
How do you store commands so you can reuse them later?
Write commands to a .sh file.
Although scripts don’t have to have names ending in .sh, it’s good practice as it helps keep track of which files are scripts.
Which command lets you execute commands contained in a .sh file?
bash name.sh
How do you call a file full of shell commands?
A shell script.
How do you provide a placeholder to a command in a script?
$@
How do you execute a shell script with a placeholder?
bash file.sh placeholder
How do you provide several placeholders to a shell script?
Use $1, $2…
How are several placeholders passed to a shell script?
In the order in which they appear in the bash command calling the script.
You need to pay extra attention to the order of the placeholders in the script and in your commands call, to ensure you’re passing the appropriate value at the appropriate placeholder.
How do you write loops in scripts?
Use semi-colons, or declare them on several lines without semi colon following this format:
for value in variable
do
command1
command2
done
Commands do not have to be indented, but doing so makes things clearer.
How do you add comments to a script?
Use # at the beginning of the line.