Advertisement
Legomancer

Terminal Scenarios

Aug 19th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.11 KB | None | 0 0
  1. --Scenarios--
  2. Examples, Scenarios and Tricks:
  3.  
  4. The nothing trick. Creates a file or wipes if it exists
  5. $ > file.txt
  6.  
  7.  
  8. The Null Trick
  9. $ : > file.txt
  10. Wipes existing file, or creates it. ":" is a shell reserved character for null
  11. Then open it with default editor app (TextEdit). Dont open .sh scripts
  12. $ open a.txt
  13. .
  14. Old Echo Trick:
  15. $ echo > file.txt # or
  16. $ echo " " > file.txt
  17. [from UbuntuForums]
  18.  
  19. Append. With no arguments, echo adds a new line to the end of the file
  20. $ echo >> file.txt
  21. Add 3 new lines to the end:
  22. $ for i in {1..3}; do echo >> file.txt; done
  23. Or
  24. $ sed '$G;$G;$G'
  25.  
  26. List unhidden folders only, because folders technically end with / :
  27. $ ls -d */
  28. List folders with full path:
  29. $ ls -d ~/*/
  30. List root:
  31. $ ls -d /*/
  32. List folders inclu hidden:
  33. $ find . -maxdepth 1 -type d
  34. List folders and sub-folders:
  35. $ find . -type d
  36.  
  37. Show every path in $PATH
  38. $ for i in {1..7}; do echo $PATH | cut -d: -f$i; done
  39.  
  40. Show Hex's IP
  41. $ ifconfig en1 inet
  42. $ ifconfig | grep 'inet '
  43.  
  44. Save a list of all the filenames in the current folder
  45. $ ls > file.txt
  46. List all files in all folders within the current folder
  47. $ ls *
  48. Display the 10 newest files
  49. $ ls -lt | head
  50.  
  51. Show (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]
  55. Brace 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. >
  64. Make 9 folders named "1" to "9"
  65. $ mkdir {1..9}
  66. It can take many arguments, space separated
  67. $ echo 0{5..9} {10..12}
  68. > 5 6 7 8 9 10 11 12
  69. Binary Trick
  70. $ echo {0..1}{0..1}{0..1}
  71. > 000 001 010 011 100 101 110 111
  72. Make folders with paths
  73. $ mkdir -p test/{1..10}/{1..10} [w]
  74. Date 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}
  76. My 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
  78. Nested brace expansions:
  79. $ echo a{A{1,2},B{3,4}}b
  80. aA1b aA2b aB3b aB4b # comma is just a list?
  81.  
  82. Change 'seq' delimiter from newline to space
  83. $ seq -s' ' 12
  84. Or 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.  
  88. Sections generator:
  89. $ for i in {1..3}; do echo "Question $i" && echo; done
  90.  
  91. Generate 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. ```
  94. mkdir -p Block{1..4}/Pt{1..6}
  95. sleep 1
  96. touch Block{1..4}/Pt{1..6}/{0..1}{0..9}.txt
  97. ```
  98. Or to only do block by block
  99. ` touch Block2/Pt{1..6}/{0..1}{0..9}.txt `
  100. Delete oo.txt in every folder
  101. $ rm Block{1..4}/Part{1..6}/00.txt
  102. Delete every file in every folder
  103. $ rm Block{1..4}/Part{1..6}/*
  104.  
  105.  
  106. To 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.  
  109. To echo one thing into all files in a folder
  110. $ for i in *; do echo '<!-- -->' >> $i; done
  111.  
  112. Wipe all files
  113. $ for i in *; do : > $i; done
  114.  
  115. [Use below output as my standard output?]
  116. To newline an echo pattern (seq only does numbers)
  117. $ for i in {1..9}; do echo "pg"$i; done
  118. > ```
  119. pg1
  120. pg2...
  121. ```
  122. To echo specific integers
  123. $ for i in 1 4 5; do echo "pg"$i; done
  124.  
  125. To delete everything except line 8, inline edit all files ending "txt" (no backups)
  126. for f in *txt;
  127. do ( sed '8!d' "$f" ) > foo && mv foo "$f";
  128. done
  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.  
  133. Join all the files in the current folder together
  134. $ cat * > bigcat.txt
  135. Or see the results first
  136. $ cat * | less
  137.  
  138. In 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.  
  142. Number 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.  
  150. Four ways to handle file names with spaces in them. From Macworld
  151. 1. Drag the directory onto the terminal
  152. 2. Use single quotes (or double):
  153. $ cd '/Users/directory with spaces'
  154. 3. Prefix the 'space character' with a backslash (keep tabbing):
  155. $ cd /Users/username/temp/directory\ with\ spaces
  156. 4. Replace spaces with a * (or a ?)
  157. And a welcome but over-detailed explanation why these work StackOverflow
  158.  
  159. To 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
  162. H1 - raw
  163. H2 - colons and numerals removed
  164. H3 - 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)
  168. If that doesnt work then try different line endings '\r\n'
  169.  
  170. I 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
  172. But pasting back into GDoc didn't work, cos of PC line endings?
  173.  
  174. To extract the tags (single words) from a list of links mixed in, do:
  175. $ sed '/http/d' < in.txt > out.txt
  176. Cos 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.  
  179. 4/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]
  181. Replace text, only on lines matching a pattern, in all html files
  182. 5104.html were put in 'htmlandcss'
  183. 5104.css were in 'style' beside that folder.
  184. html had 'href="5104.css" ' I wanted to change to 'href="../styles/5104.css" '
  185. so
  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. ,
  192. And update index.html hyperlinks from 'href="5104.html" ' to 'href="/COAC/html/5104.css" '
  193. so
  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.
  196. Oops I originally put 'href="/COAC/html/'
  197. To remove a slash and correct the path:
  198. $ sed -i.bak 's/\/C/C/g' index.html
  199. ,
  200. Next time maybe "git checkout -- file.html" to revert
  201.  
  202.  
  203. How 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]
  208. Or if a syllabus has leading junk, first lesson "Learn HTML" (or "Transcript" on Youtube)
  209. Get line number (or "nl -ba" or "cat -n") and delete from line before
  210. $ sed '/Learn/=' f1.txt
  211. Delete 1st 15 lines
  212. $ sed '1,15d'
  213. .
  214. JS Beta
  215. Which line has only a 1?
  216. $ sed -n '/^1$/='
  217. .
  218. Which line has only a number?
  219. $ sed -n '/^[1-9]$/=' [?]
  220.  
  221. "m"
  222. [Hang on
  223. Numbering Challenges.
  224. First find what line challenge1 is on:
  225. $ sed -n '/Hello/=' FCCSyllabusHTML.txt
  226. > 18
  227. Delete up to there:
  228. $ sed '1,16d' FCCSyllabusHTML.txt > h1.txt
  229. Find what line the last challenge is on:
  230. $ sed -n '$=' h1.txt
  231. > 59
  232. That's how many files to make:
  233. $ touch ch{0..5}{0..9}.txt
  234.  
  235.  
  236. 28/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?]
  238. I copied in 2 halfs in to v01.txt and v02.txt
  239. First 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.  
  249. To swap a list of pairs so 2nd line is atop 1st.
  250. First delete the first line, and all blank lines (optional)
  251. $ sed '1d; /^$/d' mraw.txt > m1.txt
  252. delete every 2nd line
  253. $ sed 'n;d' m1.txt > m2.txt
  254. extract every 2nd line
  255. $ sed -n 'n;p' m1.txt > m3.txt
  256. merge em
  257. $ paste m3.txt m2.txt > m4.txt
  258. replace tabs (paste) with newlines
  259. $ tr '\t' '\n' < m4.txt > m5.txt
  260.  
  261.  
  262. Sort a bibliography alphabetically
  263. $ sort in.txt | cat -s | sed G > out.txt
  264.  
  265. Converting a comma separated inline list into a newline list:
  266. f1.txt - raw "list, like, this"
  267. $ tr ',' '\n' < f1.txt > f2.txt
  268. f2.txt - replaced commas with newlines, but with spaces at start
  269. $ sed 's/^ //' f2.txt > f3.txt
  270. f3.txt - no spaces at start. done
  271.  
  272. Converting a newline list into a comma separated list (opposite):
  273. f1.txt - raw
  274. $ tr '\n' ',' < f1.txt > f2.txt
  275. f2.txt - replaced newlines with commas. Now to put spaces at start
  276. $ sed 's/,/, /' f2.txt > f3.txt
  277. .
  278. And the opposite:
  279. $ sed 's/, /€/g' < in.txt > f2.txt
  280. $ tr € '\n' < f2.txt > f3.txt
  281.  
  282.  
  283. Create 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.  
  288. Log what ROMs I already transferred to Gato (OS RetroPie on SD RECOVERY) by checking what's still on USB stick SLIM:
  289. setup:
  290. $ cd /Volumes/SLIM/retropie/roms/n64
  291. $ ls > ~/GatoGot01.txt
  292. # creates file. Home dir is an easy to reach path
  293. workflow:
  294. $ cd ../gba
  295. $ ls >> ~/GatoGot01.txt
  296. $ echo "" >> ~/GatoGot01.txt
  297. # append new line for readability
  298.  
  299.  
  300. [For AllCmds Sed exmpls?]
  301. A rather fiddly way of formatting a playlist dump how I want:
  302. Chiptune Stream's last.fm playlist has a 5 line pattern - album, artist, song, More, n minutes ago.
  303. Manually delete first lines to get pattern. Delete every 5th line
  304. $ sed 'n;n;n;n;d' raw.txt > f1.txt
  305. Delete every 4th line.
  306. $ sed 'n;n;n;d' f1.txt > f2.txt
  307. Now only album > artist > song. Print every 3rd line to extract songs
  308. $ sed -n 'n;n;p' f2.txt > f3.txt
  309. Manually add 2 lines at start. Then print every 3rd line to extract artist
  310. $ sed -n 'n;n;p' f2.txt > f4.txt
  311. Make the bridging "by" file
  312. $ for i in {1..20}; do echo 'by' >> f5.txt; done
  313. Paste
  314. $ paste f3.txt f5.txt f4.txt > f6.txt
  315. Tabs 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?]
  319. I 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)
  320. Raw: "
  321. folder1/
  322. folder2/
  323. "
  324. $ sed 's`/ ``; s/^/ /' in.txt > out.txt
  325.  
  326.  
  327. Show 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
  329. This shows all files (inclu hidden?). word/line/chara count
  330. $ find . | wc
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. [for Tests?]
  341. Can I use this command?
  342. A guide mentioned a command "dscacheutil". To check if Hex knows it I started typing and it tab-completed.
  343. Or 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.  
  353. Info 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement