CSSE2310 - Final Exam - Bash commands Flashcards

1
Q

How do you handle a question like this ….

“Get the first line of file.c which contains the word “car”. “

A
  1. start with getting the first line

“head -1 file.c”

  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you handle a question like this …

“Get the last 4 lines in file.c and print them alphabetically”

A
  1. start with getting the last four lines

“tail -4 file.c”

  1. Pipe this into sort to make them alphabetical

“tail -4 file.c | sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you handle a question like this …

“Get the lines in file.c that contain both cat and dog”

A
  1. start with searching the file for cat

grep -n “cat” file.c

  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you handle a question like this …

Get the lines in file.c that contain cat not case sensitive

A
  1. start with searching the file for lines that contain cat

grep -n “cat” file.c

  1. Use dash i to include not be case-sensitive

grep -i -n “cat” file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you handle a question like this …

Get all the lines in the current directory that contain the word cat

A
  1. start with searching the current directory that contain cat

grep -r cat .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle a question like this …

Count the number of lines in the file “file.c” that contain the word “dog”

A
  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you handle a question like this …

Get the files that contain “dog” in the current directory?

A
  1. Use -l with grep

grep -r -l “dog” .

OR

grep -l -r “dog” .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you handle a question like this …

Get the 2nd and 4th column of the file.c, delimited by tabs.

A
  1. Use cut to get the columns

cut -f 2,4 file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you handle a question like this …

Get the 2nd to 5th column of the file.c, delimited by tabs.

A
  1. Use cut to get the columns

cut -f 2-5 file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you handle a question like this …

Get the 5, 7 th column of the file.c delimited by “,”

A
  1. Use cut to get the columns and -d for the delimiter

cut -f 5,7 -d “,” file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you handle a question like this …

Get all the lines of the file.c where “cat dog” appears EXACTLY.

A
  1. Use grep -c to count the number of occurrences

grep -w “cat dog” file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you handle a question like this …

Get the first 4 lines of the file file.c

A
  1. Use head command and -n

head -4 file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you handle a question like this …

Get the lines in file.c that contain cat OR dog

A
  1. use grep command and \ |

grep “cat|dog” file.c

OR

grep -n “cat|dog” file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you handle a question like this …

Get the lines in file.c that contain cat AND dog

A
  1. use grep command and .*

grep “cat.*dog” file.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you handle a question like this …

Get the fifth line of the file file.c

A
  1. Step 1. use the cat command to show the contents on screen.

cat file.c

  1. pipe into the head command to say what line you want to read

cat file.c | head -5

  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you handle a question like this …

Get the number of times dog appears in the first column of file.c

A

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

17
Q

How do you handle a question like this …

Show all instances of zazu which you are running on the system (Your computer)

A

First off, get the processes on the system which are

ps -u $USER

  1. Pipe this into grep and look for zazu

ps -u $USER | grep zazu

18
Q

How do you handle a question like this …

Get all the files which have are four characters or longer

A
  1. Use ls -d to list with a delimiter

ls -d

  1. Use wildcards to select 4 or more

ls -d ????*

19
Q

How do you handle a question like this …

Add /yo/yo/ma to the list of directories

A

Gotta reassign the path, using PATH=$PATH

PATH=$PATH

Append to the path

PATH=$PATH:yo/yo/ma

20
Q

How do you handle a question like this …

Get the first two lines of the file bob.c

A

Use the head command

head -2 bob.c

21
Q

How do you handle a question like this …

Show the commands which will be run from this directory

A

Use the echo command

echo $PATH

22
Q

How do you handle a question like this …

Show the first column of the file bob.c (columns are separated by commas).

A
  1. Use the cut command and -f to specify the column to read

cut -f 1

  1. Add the -d as a delimiter

cut -f 1 -d “,” bob.c

OR

cat bob.c | cut -f 1 -d “,”

23
Q

How do you handle a question like this …

Show all instances of vim running on the system (not just you)

A
  1. Use the ps -A to get all the processes

ps -A

  1. Pipe into grep

ps -A | grep vim

24
Q

How do you handle a question like this …

Count how many files contain at least five characters

A
  1. Get all the files that have at least 5 characters

ls -d ?????*

  1. Pipe in and count how many files there are (use wc -l)

ls -d ?????* | wc -l

25
Q

How do you handle a question like this …

Delete all the files that start with s and d but not sd?

A
  1. use the rm function and wild card, must have at least one character in between so use ?

rm s?*d

26
Q

How do you handle a question like this …

Output the name of the first directory which will be used to search for executables? (Hint: “:” is a separator)

A
  1. Use echo $PATH to get the execuables

echo $PATH

  1. Pipe into cut -f 1 and delimit the semicolon

echo $PATH | cut -f 1 -d “:”

27
Q

How do you handle a question like this …

Kill the process 1234

A
  1. Need to use the kill command -9 , to say that this is not ignorable

kill -9 1234

28
Q

How do you handle a question like this …

Show all the files and permissions for all the files in the current directory.

A
  1. Need to list so use ls, “all the files” so need to use -a, for the permissions gotta use -l

ls -al

29
Q

How do you handle a question like this …

Show the first line of the file bob.c that contains the word hello

A
  1. Use grep to search for hello

grep “hello” bob.c

  1. Pipe in and get just the first line using head

grep “hello” bob.c | head -1

OR

cat “bob.c” | head -1 | grep “hello”

30
Q

How do you handle a question like this …

Count the number of .c files in the current directory

A
  1. list all the .c files in the current directory

ls *.c

  1. pipe in and use wc -l to get the number of occurences

ls *.c | wc -l

31
Q

How do you handle a question like this …

Copy all the .c files in the current working directory to the /dest folder. Change all their names to start with old_

A
  1. Use cp to copy all the .c files to the

cp *.c dest/

  1. Need to loop through each one and change its name

for file in dest/*.c; mv dest/$file old_$file.c; done;

32
Q

How do you handle a question like this …

Change all the .pdf files in the current working directory to be prefixed with “new_”.

A

Use a for loop

for file in *.pdf; do mv $file new_$file; done

33
Q

How do you handle a question like this …

Remove the prefix “old_” from all of the .pdf files in the current working directory.

A

Use a for loop

for file in *.pdf; do mv $file ${file#old_}; done

34
Q

How do you handle a question like this …

Remove the suffix “.c” from all of the files in the current working directory.

A

Use a for loop

for file in *.c; do mv $file ${file%.c}; done

35
Q

What is the difference between the grep flags

  • r
  • n
  • c
A
  • c : Counts the number of lines that this word appears
  • r: Recursively searches the directories below the current directory.
  • n: Gives the line number and the contents of the output