-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bash & Terminal.html
343 lines (276 loc) · 9.46 KB
/
Bash & Terminal.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<html>
<head>
<link rel="Stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/cosmo/bootstrap.min.css" />
<title>Bash %title% Terminal</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/styles/default.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js"></script>
<script>
window.onload = function() {
var allPre, i, j;
allPre = document.getElementsByTagName("pre");
for (i = 0, j = allPre.length; i < j; i++) {
hljs.highlightBlock(allPre[i]);
}
};
</script>
</head>
<body>
<a href="https://github.com/coisnepe/vimwiki_html" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>
<a href="index.html">Index</a>
<hr>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div id="Bash & Terminal commands"><h2 id="Bash & Terminal commands">Bash & Terminal commands</h2></div>
<ul>
<li>
<a href="Bash & Terminal.html#xargs">xargs</a>
<li>
<a href="Bash & Terminal.html#cp">cp</a>
<li>
<a href="Bash & Terminal.html#wc">wc - Word Count</a>
<li>
<a href="Bash & Terminal.html#grep">grep</a>
<li>
<a href="Bash & Terminal.html#find">find</a>
<li>
<a href="Bash & Terminal.html#du/df">du/df - Directory Size</a>
<li>
<a href="Bash & Terminal.html#shred">Shred</a>
<li>
<a href="Bash & Terminal.html#sed">sed</a>
<li>
<a href="Bash & Terminal.html#Snippets">Snippets</a>
</ul>
<p>
Chain commands
</p>
<pre>
$ ls -al | grep "file_name"
</pre>
<p>
<br/>
</p>
<div id="Bash & Terminal commands-xargs"><h3 id="xargs">xargs</h3></div>
<p>
Use the output of one command as arguments for the next
</p>
<pre>
# Find ruby files in app/ that contain the string user.
# In the files matched, look for the string "transaction"
$ grep "users" -lr app/**/*.rb | xargs grep "transaction"
</pre>
<p>
<br/>
</p>
<div id="Bash & Terminal commands-cp"><h3 id="cp">cp</h3></div>
<pre>
$ cp source_file target_file (or target_directory/)
-r # Recursive
-i # Interactive: ask what to do with existing files in the destination
-v # Verbose
-f # Force
</pre>
<p>
<br />
</p>
<div id="Bash & Terminal commands-wc"><h3 id="wc">wc</h3></div>
<p>
Count words, lines, characters, etc... in a file
</p>
<pre>
$ wc -[w | l | c] your_file.csv
</pre>
<p>
<br/>
</p>
<div id="Bash & Terminal commands-grep"><h3 id="grep">grep</h3></div>
<p>
Search one file
</p>
<pre>
$ grep "redmine" /etc/hosts
// Search filetypes
$ grep -rn "flagged" --include=*.{rb,} .
</pre>
<p>
Flags
</p>
<pre>
-r # search recursively
-n # display the line number
-l # only output filenames of matched files
-B 10 # display 10 lines before the line containing the match
-A 10 # display 10 lines after the line containing the match
-C 10 # display 10 lines before and after the line containing the match
-i # case insensitive
-h # Omit filenames in the results
-v # Invert, i.e. display lines that do not match
</pre>
<p>
<br />
</p>
<div id="Bash & Terminal commands-find"><h3 id="find">find</h3></div>
<pre>
find
-name "*.js" # Find js files
-wholename "controller" # Look through the entire filename, not just extension
-regex # Search with regex pattern
-type f # Files
-maxdepth X # Go down at most X levels (1 == current directory only)
-not
-path # Exclude path
-delete # Delete the files found
# Find all .js files in the app directory minus [[app/assets/.]]
$ find app/. -name "*.js" -type f -not -path "*/assets/*"
# Find files with a specific pattern and exclude hidden files
# 'find' matches the entire path, so append '.*/' to the search
$ find . -maxdepth 1 -wholename ".*/application*.js" -not -name ".*"
# Find files and move htem
$ find . -maxdepth 1 -type f -name "*.sql" | xargs -I '{}' mv '{}' dumps/
</pre>
<p>
<br/>
</p>
<div id="Bash & Terminal commands-du/df"><h3 id="du/df">du/df</h3></div>
<p>
du : Display sizes of directories
</p>
<pre>
du
-s # Display an entry for each file/directory
-h # Display the sizes in a "human readable" way
directory_name # Specific directory
* # Every directory in pwd
# 1: Get directories and files by size
# 2: Reverse sort (-r reverse / -h readable)
# 3: Only display the top 10 results
$ du -sh * | sort -rh | head -10
</pre>
<p>
df : Display space usage at the disk level
</p>
<pre>
df
-h # Display the sizes in a "human readable" format
$ df -h
</pre>
<p>
<br/>
</p>
<div id="Bash & Terminal commands-sed"><h3 id="sed">sed</h3></div>
<p>
Delete in situ lines containing a given string
</p>
<pre>
$ sed -i "/words_in_line/d" ./file_path.log
</pre>
<p>
<a href="http://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files">Replace strings</a>
</p>
<pre>
$ sed -i -- 's/https/http/g' file_path.txt
</pre>
<p>
<br />
</p>
<div id="Bash & Terminal commands-shred"><h3 id="shred">shred</h3></div>
<p>
Safely delete files : rm on steroids! It overwrites the file content
</p>
<pre>
shred
-f # Force change permissions to allow writing
-n # Number of iterations
-v # Verbose (show progress
-u # Actually remove file after progress
-z # On the last round, overwrite the file with zeros
$ shred -uvzn 3 /Documents/file_to_nuke.txt
</pre>
<p>
<br />
</p>
<div id="Bash & Terminal commands-Snippets"><h3 id="Snippets">Snippets</h3></div>
<pre>
###########################################
# == Count the number of lines for files in a given directory and display the sorted result ==
#
# 1: Find ruby files in the current directory
# 2: Count the number of lines
# 3: Reverse sort them
# 4: Omit the "total" line
# 5: Only display the top 10 results
$ find . -name "*.rb" -type f | xargs wc -l | sort -rn | grep -v ' total$' | head -10
###########################################
###########################################
# 1: Find all files in a directory
# 2: Nuke!
$ find directory_path/ -depth -type f -exec shred -uvzn 2 {} \;
###########################################
###########################################
# == Copy a command output to the clipboard
# Linux
$ sudo apt-get install xclip
$ cat my_file.txt | xclip -selection clipboard
# OSX: pbcopy and pbpaste
$ cat ~/.ssh/id_rsa.pub | pbcopy
###########################################
###########################################
# == grep filetypes
$ grep -rn "string" --include=*.{rb,} ./
$ find . -type f -name "*.rb" | xargs grep -n "string"
###########################################
</pre>
<p>
<a href="http://stackoverflow.com/questions/3509332/how-to-convert-a-tab-separated-file-into-comma-separated-file#30219010">Substitute characters in file</a>
</p>
<p>
<a href="http://stackoverflow.com/questions/64860/best-way-to-convert-text-files-between-character-sets#64889">Change file encoding</a>
</p>
<p>
<a href="http://mindspill.net/computing/linux-notes/determine-and-change-file-character-encoding/">File encoding in Unix</a>
</p>
<pre>
# Encode file to UTF-8
$ vim +"set nobomb | set fenc=utf8 | x" file_to_encode.txt
# Substitute tabs with semi-colons. On OSX, \t doesn't work. ctrl-v then hit tab
$ LC_CTYPE=C sed 's/ /;/g' infile.txt outfile.csv
</pre>
<pre>
# Kill all processes from a given application
$ ps aux | grep firefox | awk '{print $2}' | xargs kill -9
</pre>
<pre>
# Batch rename files
$ brew install rename
$ find . -type f -name "*GUIDELINES*" -exec rename 's/GUIDELINES.md/README.md/' \{\} \;
</pre>
<pre>
# Port already in use?
$ lsof -i :3000 | awk '{print $2}' | grep -v PID | xargs kill -9
</pre>
<pre>
# List all available commands
(echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u
}}
{{{
# Find duplicate files
$ find app/controllers/ -type f -name "*.rb" -printf '%f\n' | sort | uniq -c | sort -rn
# find app/controllers/ -type f -name "*.rb" -printf '%f\n'
# Find ruby files and only print their name
# uniq -c
# Output unique names and prepend the number of occurences
# sort -rn
# Sort them in reverse order
</pre>
<pre>
# Sort folders by size
$ du -cskh * | sort -rh
</pre>
</div>
</div>
</div>
</body>
</html>