Snippets Flashcards

1
Q

How to compress using tar

A

tar -zcvf archive-name.tar.gz directory-name

Where,

  • <kbd>-z<kbd> : Compress archive using gzip program</kbd></kbd>
  • <kbd>-c</kbd>: Create archive
  • <kbd>-v</kbd>: Verbose i.e display progress while creating archive
  • <kbd>-f</kbd>: Archive File name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to uncompress using tar

A

$ tar -zxvf prog-1-jan-2005.tar.gz
Where,

  • <kbd>-x</kbd>: Extract files
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Checking the size of a folder

A

$ du -sh

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

Tmux start a new session

A
tmux new -s [name of session]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Check tmux sessions

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

Detach from tmux session

A
ctrl+b d
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In tmux, to split a pane horizontally

A
ctrl+b "
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In tmux, To split pane vertically

A
ctrl+b %
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In tmux to move from pane to pane

A
ctrl+b [arrow key]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In tmux, expand the pane down a few lines

A
ctrl+b :
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Tmux, Kill named session

A

tmux kill-session -t [name of session]

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

tmux, Kill current pane

A

ctrl+b x

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

Kill tmux server, along with all sessions:

A

tmux kill-server

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

A very simple backup script

A
#!/bin/bash OF=/var/my-backup-$(date +%Y%m%d).tgz tar -czvf $OF /home/me/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Functions and Local variables in bash.

A
HELLO=Hello function hello { local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Check the size of a specific folder?

A

du -sch Dir1 File1

17
Q

If statement in bash

A
#!/bin/bash T1="foo" T2="bar" if ["$T1" = "$T2"]; then echo expression evaluated as true else echo expression evaluated as false fi
18
Q

For in bash

A
#!/bin/bash for i in `seq 1 10`; do echo $i done
19
Q

bash for loop ten times

A
#!/bin/bash for i in `seq 1 10`; do echo $i done
20
Q

bash while statement

A
#!/bin/bash COUNTER=0 while [$COUNTER -lt 10]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done
21
Q

Bash Until Statement

A
#!/bin/bash COUNTER=20 until [$COUNTER -lt 10]; do echo COUNTER $COUNTER let COUNTER-=1 done
22
Q

Bash function with parameters

A
#!/bin/bash function quit { exit } function e { echo $1 echo $2 } e Hello Bye
23
Q

Using select to make simple menus

A
#!/bin/bash OPTIONS="Hello Quit" select opt in $OPTIONS; do if ["$opt" = "Quit"]; then echo done exit elif ["$opt" = "Hello"]; then echo Hello World else clear echo bad option fi done
24
Q

BAsh script to do backup for a specific folder. The folder to be backed up given in argument to the script.

A
#!/bin/bash if [-z "$1"]; then echo usage: $0 directory exit fi SRCD=$1 TGTD="/home/alhaol/" OF=\_BackUp\_$(date +\_%Y\_%\_m\_%d).tgz tar -czvf $TGTD$SRCD$OF $SRCD echo ====================================== echo =====================Done =========== echo check $TGTD$SRCD$OF
25
Q

Use cw to find how many lines in a file?

A

wc -l file

26
Q

Use wc to count the number of words?

A

wc -w fileName

27
Q

use ls and wc to find number of files in a folder.

A

ls FolderName | wc -l

28
Q

Use head to show first 2 line in a file:

A

head -n 2 FileName

29
Q

How to add header line to a file using echo, >, and cat

A

echo “A, B, C”> header.txt

cat header.csv file.csv > Newfile.csv

30
Q

How to use sed to change the content of a file?

A

sed “s/<string>/<string>/g" <source_file> &gt; <target_file></target_file></source_file></string></string>

31
Q

use grep and wc to find how many times something happened in a file.

A

grep “Something” File | wc -l

32
Q

use sed and > to find a pattern in a file and change it to something else

A

sed “s/somthing/somethingelse/g” adult.csv > adult.csv

33
Q

Using head and tail to sub set large file

A

head -n 120 adult.csv | tail -n 20 > adult_sample.csv

34
Q

Finding number of duplicate lines is a file using uniq, sort and wc.

A

sort file.csv | uniq -d | wc -l

35
Q

how to use for loop to replace spaces with _

A

replace_source=’ ‘

replace_target=’_’

for filename in ./*.csv; do new_filename=${filename//$replace_source/$replace_target}

mv “$filename” “$new_filename”

done

36
Q
A