CSSE2310 - Final Exam - Bash commands Flashcards
How do you handle a question like this ….
“Get the first line of file.c which contains the word “car”. “
- start with getting the first line
“head -1 file.c”
- Pipe this into grep to search for “car”
“head -1 file.c | grep “car”
Answer: “head -1 file.c | grep “car”
OR
cat file.c | head -1 | grep “car”
How do you handle a question like this …
“Get the last 4 lines in file.c and print them alphabetically”
- start with getting the last four lines
“tail -4 file.c”
- Pipe this into sort to make them alphabetical
“tail -4 file.c | sort
How do you handle a question like this …
“Get the lines in file.c that contain both cat and dog”
- start with searching the file for cat
grep -n “cat” file.c
- Pipe this into a grep to get the lines that also contain dog
grep -n “cat” file.c | grep “dog”
OR
cat file.c | grep “cat” | grep “dog”
How do you handle a question like this …
Get the lines in file.c that contain cat not case sensitive
- start with searching the file for lines that contain cat
grep -n “cat” file.c
- Use dash i to include not be case-sensitive
grep -i -n “cat” file.c
How do you handle a question like this …
Get all the lines in the current directory that contain the word cat
- start with searching the current directory that contain cat
grep -r cat .
How do you handle a question like this …
Count the number of lines in the file “file.c” that contain the word “dog”
- Use -c with grep
grep -c “dog” file.c
OR
grep “dog” file.c | wc -l
OR
cat file.c | grep “dog” | wc -l
How do you handle a question like this …
Get the files that contain “dog” in the current directory?
- Use -l with grep
grep -r -l “dog” .
OR
grep -l -r “dog” .
How do you handle a question like this …
Get the 2nd and 4th column of the file.c, delimited by tabs.
- Use cut to get the columns
cut -f 2,4 file.c
How do you handle a question like this …
Get the 2nd to 5th column of the file.c, delimited by tabs.
- Use cut to get the columns
cut -f 2-5 file.c
How do you handle a question like this …
Get the 5, 7 th column of the file.c delimited by “,”
- Use cut to get the columns and -d for the delimiter
cut -f 5,7 -d “,” file.c
How do you handle a question like this …
Get all the lines of the file.c where “cat dog” appears EXACTLY.
- Use grep -c to count the number of occurrences
grep -w “cat dog” file.c
How do you handle a question like this …
Get the first 4 lines of the file file.c
- Use head command and -n
head -4 file.c
How do you handle a question like this …
Get the lines in file.c that contain cat OR dog
- use grep command and \ |
grep “cat|dog” file.c
OR
grep -n “cat|dog” file.c
How do you handle a question like this …
Get the lines in file.c that contain cat AND dog
- use grep command and .*
grep “cat.*dog” file.c
How do you handle a question like this …
Get the fifth line of the file file.c
- Step 1. use the cat command to show the contents on screen.
cat file.c
- pipe into the head command to say what line you want to read
cat file.c | head -5
- pipe into the tail to say how many lines you want to read from the head command
cat file.c | head -5 | tail -1
OR
head -5 file.c | tail -1
How do you handle a question like this …
Get the number of times dog appears in the first column of file.c
Cut the columns first and then count. So…
cut -f 1 file.c
Then pipe into grep and count the number times dog appears
cut -f 1 file.c | grep -c dog
OR
cat file.c | cut -f 1 | grep “dog” | wc -l
How do you handle a question like this …
Show all instances of zazu which you are running on the system (Your computer)
First off, get the processes on the system which are
ps -u $USER
- Pipe this into grep and look for zazu
ps -u $USER | grep zazu
How do you handle a question like this …
Get all the files which have are four characters or longer
- Use ls -d to list with a delimiter
ls -d
- Use wildcards to select 4 or more
ls -d ????*
How do you handle a question like this …
Add /yo/yo/ma to the list of directories
Gotta reassign the path, using PATH=$PATH
PATH=$PATH
Append to the path
PATH=$PATH:yo/yo/ma
How do you handle a question like this …
Get the first two lines of the file bob.c
Use the head command
head -2 bob.c
How do you handle a question like this …
Show the commands which will be run from this directory
Use the echo command
echo $PATH
How do you handle a question like this …
Show the first column of the file bob.c (columns are separated by commas).
- Use the cut command and -f to specify the column to read
cut -f 1
- Add the -d as a delimiter
cut -f 1 -d “,” bob.c
OR
cat bob.c | cut -f 1 -d “,”
How do you handle a question like this …
Show all instances of vim running on the system (not just you)
- Use the ps -A to get all the processes
ps -A
- Pipe into grep
ps -A | grep vim
How do you handle a question like this …
Count how many files contain at least five characters
- Get all the files that have at least 5 characters
ls -d ?????*
- Pipe in and count how many files there are (use wc -l)
ls -d ?????* | wc -l