· 6 years ago · Aug 19, 2019, 10:32 PM
1--Scenarios--
2Examples, Scenarios and Tricks:
3
4The nothing trick. Creates a file or wipes if it exists
5$ > file.txt
6
7
8The Null Trick
9$ : > file.txt
10Wipes existing file, or creates it. ":" is a shell reserved character for null
11Then open it with default editor app (TextEdit). Dont open .sh scripts
12$ open a.txt
13.
14Old Echo Trick:
15$ echo > file.txt # or
16$ echo " " > file.txt
17[from UbuntuForums]
18
19Append. With no arguments, echo adds a new line to the end of the file
20$ echo >> file.txt
21Add 3 new lines to the end:
22$ for i in {1..3}; do echo >> file.txt; done
23Or
24$ sed '$G;$G;$G'
25
26List unhidden folders only, because folders technically end with / :
27$ ls -d */
28List folders with full path:
29$ ls -d ~/*/
30List root:
31$ ls -d /*/
32List folders inclu hidden:
33$ find . -maxdepth 1 -type d
34List folders and sub-folders:
35$ find . -type d
36
37Show every path in $PATH
38$ for i in {1..7}; do echo $PATH | cut -d: -f$i; done
39
40Show Hex's IP
41$ ifconfig en1 inet
42$ ifconfig | grep 'inet '
43
44Save a list of all the filenames in the current folder
45$ ls > file.txt
46List all files in all folders within the current folder
47$ ls *
48Display the 10 newest files
49$ ls -lt | head
50
51Show (concatenate) the contents of every file in the folder, with newlines:
52$ for i in *; do cat $i && echo; done
53
54[for naming. And generating test files? Clean these up]
55Brace expansions
56$ echo {1..5}
57> 1 2 3 4 5
58$ echo No{1..5}
59> No1 No2 No3 No4 No5
60$ echo {Z..A}
61> Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
62$ echo {a..Z}
63>
64Make 9 folders named "1" to "9"
65$ mkdir {1..9}
66It can take many arguments, space separated
67$ echo 0{5..9} {10..12}
68> 5 6 7 8 9 10 11 12
69Binary Trick
70$ echo {0..1}{0..1}{0..1}
71> 000 001 010 011 100 101 110 111
72Make folders with paths
73$ mkdir -p test/{1..10}/{1..10} [w]
74Date Trick. Name folders by date. 0 is to pad 01-09. 12 mo in a yr.
75$ mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12}
76My Zelda map trick (unused). Makes a big list of 27 names z00_00 - z09_09
77$ echo z0{0..9}_0{0..9} > file.txt
78Nested brace expansions:
79$ echo a{A{1,2},B{3,4}}b
80 aA1b aA2b aB3b aB4b # comma is just a list?
81
82Change 'seq' delimiter from newline to space
83$ seq -s' ' 12
84Or pipe to translate. (end with a newline)
85$ seq 12 | tr '\n' ' '; echo
86> 1 2 3 4 5 6 7 8 9 10 11 12
87
88Sections generator:
89$ for i in {1..3}; do echo "Question $i" && echo; done
90
91Generate empty text files in folders for OU course. That's 4 Blocks, 6 Parts, each with up to 20 files:
92$ touch script.sh && chmod +x script.sh
93```
94mkdir -p Block{1..4}/Pt{1..6}
95sleep 1
96touch Block{1..4}/Pt{1..6}/{0..1}{0..9}.txt
97```
98Or to only do block by block
99` touch Block2/Pt{1..6}/{0..1}{0..9}.txt `
100Delete oo.txt in every folder
101$ rm Block{1..4}/Part{1..6}/00.txt
102Delete every file in every folder
103$ rm Block{1..4}/Part{1..6}/*
104
105
106To duplicate a file 39 times, and number them "01"-"39". [I need "}{0..9" not "1..9" or it skips 10 (09 11)]
107$ for i in {0..3}{0..9}; do cp test.ogg "test$i.ogg"; done
108
109To echo one thing into all files in a folder
110$ for i in *; do echo '<!-- -->' >> $i; done
111
112Wipe all files
113$ for i in *; do : > $i; done
114
115[Use below output as my standard output?]
116To newline an echo pattern (seq only does numbers)
117$ for i in {1..9}; do echo "pg"$i; done
118> ```
119pg1
120pg2...
121```
122To echo specific integers
123$ for i in 1 4 5; do echo "pg"$i; done
124
125To delete everything except line 8, inline edit all files ending "txt" (no backups)
126for f in *txt;
127do ( sed '8!d' "$f" ) > foo && mv foo "$f";
128done
129
130[test this?] Move the subdirectory "my_dir" and also all ".bak" files in the current working directory's parent directory to an existing directory named "new_dir"
131$ mv my_dir ../*.bak new_dir
132
133Join all the files in the current folder together
134$ cat * > bigcat.txt
135Or see the results first
136$ cat * | less
137
138In a dir I had 3 text files with 1 line of text. I joined them and double spaced
139$ cat * > c.txt
140$ sed G c.txt > ch.txt
141
142Number all lines (see AllCmds for minutia)
143$ nl -ba -w3 -s' ' file > file_new # my fave. or
144$ cat -n
145# or
146$ sed = in.txt | sed 'N;s/\n/\t/' > out.txt # or
147$ awk '{print NR "> " $s}' in.txt > out.txt
148
149
150Four ways to handle file names with spaces in them. From Macworld
1511. Drag the directory onto the terminal
1522. Use single quotes (or double):
153$ cd '/Users/directory with spaces'
1543. Prefix the 'space character' with a backslash (keep tabbing):
155$ cd /Users/username/temp/directory\ with\ spaces
1564. Replace spaces with a * (or a ?)
157And a welcome but over-detailed explanation why these work StackOverflow
158
159To strip Youtube transcripts of timestamps, e.g. Hanselman [lol Ytb can do it now]:
160[Updt]
161$ tr -d :0-9 | tr -s '\n' ' ' | tr -s ' ' ' ' < in.txt > out.txt
162H1 - raw
163H2 - colons and numerals removed
164H3 - newlines changed to spaces, any duplicates are 'squeezed' to singles. done
165$ tr -d :0-9 < H1.txt > H2.txt # delete numbers
166$ tr -s '\n' ' ' < H2.txt > H3.txt # change newlines to spaces. Squeeze to single spaces
167$ tr -s ' ' ' ' < in.txt > out.txt # squeeze to single spaces (if it didnt work)
168If that doesnt work then try different line endings '\r\n'
169
170I had a 'table' (from output of Pi's 'lsblk') but copying from Doc to TextEd turned tabs into 4 spaces [23/7]. Term mostly fixed it with:
171$ tr -s ' ' '\t' < in.txt > out.txt
172But pasting back into GDoc didn't work, cos of PC line endings?
173
174To extract the tags (single words) from a list of links mixed in, do:
175$ sed '/http/d' < in.txt > out.txt
176Cos in Firefox's Bookmarks Manager I selected all tags and copied. This pasted the tags AND all links tagged with them into a text doc.
177
178
1794/3/18 Lynxrot. To re-link the html to css after moving folders around, I wanted to edit the path in multiple files.
180[Rewrite this? Clearer folders. "~" delimiters]
181Replace text, only on lines matching a pattern, in all html files
1825104.html were put in 'htmlandcss'
1835104.css were in 'style' beside that folder.
184html had 'href="5104.css" ' I wanted to change to 'href="../styles/5104.css" '
185so
186$ sed -i.bak '/link/s/f="/f="..\/styles\//g' *html
187# -i overwrites file; .bak backs up orig; substitute 'f="' (of href); only on lines containing 'link'
188# I cudv changed the delimiter to _ or ~ to avoid backslashes
189$ rm *.bak
190# delete backups, but .bak was in .gitignore anyway.
191,
192And update index.html hyperlinks from 'href="5104.html" ' to 'href="/COAC/html/5104.css" '
193so
194$ sed -i.bak '/"[0-9]/s/f="/f="COAC\/htmlandcss\//g' index.html
195# only worked as filenames started with a number so if href="5 then find "5. needed this cos the ninja pic URL had 4 numbers in it.
196Oops I originally put 'href="/COAC/html/'
197To remove a slash and correct the path:
198$ sed -i.bak 's/\/C/C/g' index.html
199,
200Next time maybe "git checkout -- file.html" to revert
201
202
203How to edit a copy-dump of FreeCodeCamp's syllabus, remove blank lines, remove words:
204$ sed '/^$/d' f1.txt > f2.txt
205$ sed 's/Complete//; s/Incomplete//' f2.txt > f3.txt
206.
207[See script]
208Or if a syllabus has leading junk, first lesson "Learn HTML" (or "Transcript" on Youtube)
209Get line number (or "nl -ba" or "cat -n") and delete from line before
210$ sed '/Learn/=' f1.txt
211Delete 1st 15 lines
212$ sed '1,15d'
213.
214JS Beta
215Which line has only a 1?
216$ sed -n '/^1$/='
217.
218Which line has only a number?
219$ sed -n '/^[1-9]$/=' [?]
220
221"m"
222[Hang on
223Numbering Challenges.
224First find what line challenge1 is on:
225$ sed -n '/Hello/=' FCCSyllabusHTML.txt
226> 18
227Delete up to there:
228$ sed '1,16d' FCCSyllabusHTML.txt > h1.txt
229Find what line the last challenge is on:
230$ sed -n '$=' h1.txt
231> 59
232That's how many files to make:
233$ touch ch{0..5}{0..9}.txt
234
235
23628/3/18 I want to rearrange a tab-separated table of keyboard shortcuts. (And I can't alt+drag TextExit?)
237[Delete this spaces bit?]
238I copied in 2 halfs in to v01.txt and v02.txt
239First append new lines to 1st half
240$ echo -e '\n\n\n\n' >> v01.txt # or a for loop?
241$ cat v01.txt v02.txt > v03rd.txt
242# join em
243$ cut -f1 v03rd.txt > v04.txt
244$ cut -f3 v03rd.txt > v05.txt
245# extract the 1st+3rd columns (1st had descriptions, 3rd Mac shortcuts, 2nd had Windows unwanted)
246$ paste v05.txt v04.txt > v06.txt
247
248
249To swap a list of pairs so 2nd line is atop 1st.
250First delete the first line, and all blank lines (optional)
251$ sed '1d; /^$/d' mraw.txt > m1.txt
252delete every 2nd line
253$ sed 'n;d' m1.txt > m2.txt
254extract every 2nd line
255$ sed -n 'n;p' m1.txt > m3.txt
256merge em
257$ paste m3.txt m2.txt > m4.txt
258replace tabs (paste) with newlines
259$ tr '\t' '\n' < m4.txt > m5.txt
260
261
262Sort a bibliography alphabetically
263$ sort in.txt | cat -s | sed G > out.txt
264
265Converting a comma separated inline list into a newline list:
266f1.txt - raw "list, like, this"
267$ tr ',' '\n' < f1.txt > f2.txt
268f2.txt - replaced commas with newlines, but with spaces at start
269$ sed 's/^ //' f2.txt > f3.txt
270f3.txt - no spaces at start. done
271
272Converting a newline list into a comma separated list (opposite):
273f1.txt - raw
274$ tr '\n' ',' < f1.txt > f2.txt
275f2.txt - replaced newlines with commas. Now to put spaces at start
276$ sed 's/,/, /' f2.txt > f3.txt
277.
278And the opposite:
279$ sed 's/, /€/g' < in.txt > f2.txt
280$ tr € '\n' < f2.txt > f3.txt
281
282
283Create a vertical list of words, stripping out all non-alphanumeric characters e.g. punctuation and whitespace [from tr man page]:
284$ tr -cs "[:alpha:]" "\n" < file1 > file2
285[Could make practice file by pipe into sort+ uniq?]
286
287
288Log what ROMs I already transferred to Gato (OS RetroPie on SD RECOVERY) by checking what's still on USB stick SLIM:
289setup:
290$ cd /Volumes/SLIM/retropie/roms/n64
291$ ls > ~/GatoGot01.txt
292# creates file. Home dir is an easy to reach path
293workflow:
294$ cd ../gba
295$ ls >> ~/GatoGot01.txt
296$ echo "" >> ~/GatoGot01.txt
297# append new line for readability
298
299
300[For AllCmds Sed exmpls?]
301A rather fiddly way of formatting a playlist dump how I want:
302Chiptune Stream's last.fm playlist has a 5 line pattern - album, artist, song, More, n minutes ago.
303Manually delete first lines to get pattern. Delete every 5th line
304$ sed 'n;n;n;n;d' raw.txt > f1.txt
305Delete every 4th line.
306$ sed 'n;n;n;d' f1.txt > f2.txt
307Now only album > artist > song. Print every 3rd line to extract songs
308$ sed -n 'n;n;p' f2.txt > f3.txt
309Manually add 2 lines at start. Then print every 3rd line to extract artist
310$ sed -n 'n;n;p' f2.txt > f4.txt
311Make the bridging "by" file
312$ for i in {1..20}; do echo 'by' >> f5.txt; done
313Paste
314$ paste f3.txt f5.txt f4.txt > f6.txt
315Tabs to spaces, newlines to commas, commas to comma spaces
316$ cat f6.txt | tr '\t' ' ' | tr '\n' ',' | sed 's/,/, /g' > f7.txt
317
318[For AllCmds Sed exmpls?]
319I copied folder names from a 'parent directory' view of a website. Want to delete trailing slash and space, add a leading space (cos folder tree in a text doc)
320Raw: "
321folder1/
322folder2/
323"
324$ sed 's`/ ``; s/^/ /' in.txt > out.txt
325
326
327Show the total number of files in the current directory and subdirectories. [from?][works but -print not needed. wc shows line count default anyway]
328$ find . -type f -print | wc -l
329This shows all files (inclu hidden?). word/line/chara count
330$ find . | wc
331
332
333
334
335
336
337
338
339
340[for Tests?]
341Can I use this command?
342A guide mentioned a command "dscacheutil". To check if Hex knows it I started typing and it tab-completed.
343Or I opened a fresh shell Cmd+n, hit tab trick to see all cmds and it appeared in the list. Q to quit list or close window.
344
345
346
347
348
349
350
351
352
353Info Tests [some obsolete]:
354$ ls -al - list all hidden files, with more info
355$ <command> --version # check the version of a CLI app or language e.g. vim, python
356$ man <cmd> - show the manual page for the command
357$ type <cmd> - check the type of a command [lxcmd.org]
358$ which <lang> - shows which sys folder the language is? works for progs
359$ echo $PATH - where Hex looks when launching a CLI prog after running a command
360$ printenv | less # shows all environment variables instead of echoing
361$ echo $PS1 - show how the prompt is formatted
362$ <cmd> <file> | less - see results of a command
363$ apropos <cmd> # what other cmds have similar functions?
364$ ifconfig en1 inet - current IP address and connection
365$ ipconfig getifaddr en1
366$ ping bbc.co.uk <n> - Then ctrl+c to stop. Or quantity 10 (lol common pitfall?)(builtin)