Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Scenarios--
- Examples, Scenarios and Tricks:
- The nothing trick. Creates a file or wipes if it exists
- $ > file.txt
- The Null Trick
- $ : > file.txt
- Wipes existing file, or creates it. ":" is a shell reserved character for null
- Then open it with default editor app (TextEdit). Dont open .sh scripts
- $ open a.txt
- .
- Old Echo Trick:
- $ echo > file.txt # or
- $ echo " " > file.txt
- [from UbuntuForums]
- Append. With no arguments, echo adds a new line to the end of the file
- $ echo >> file.txt
- Add 3 new lines to the end:
- $ for i in {1..3}; do echo >> file.txt; done
- Or
- $ sed '$G;$G;$G'
- List unhidden folders only, because folders technically end with / :
- $ ls -d */
- List folders with full path:
- $ ls -d ~/*/
- List root:
- $ ls -d /*/
- List folders inclu hidden:
- $ find . -maxdepth 1 -type d
- List folders and sub-folders:
- $ find . -type d
- Show every path in $PATH
- $ for i in {1..7}; do echo $PATH | cut -d: -f$i; done
- Show Hex's IP
- $ ifconfig en1 inet
- $ ifconfig | grep 'inet '
- Save a list of all the filenames in the current folder
- $ ls > file.txt
- List all files in all folders within the current folder
- $ ls *
- Display the 10 newest files
- $ ls -lt | head
- Show (concatenate) the contents of every file in the folder, with newlines:
- $ for i in *; do cat $i && echo; done
- [for naming. And generating test files? Clean these up]
- Brace expansions
- $ echo {1..5}
- > 1 2 3 4 5
- $ echo No{1..5}
- > No1 No2 No3 No4 No5
- $ echo {Z..A}
- > 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
- $ echo {a..Z}
- >
- Make 9 folders named "1" to "9"
- $ mkdir {1..9}
- It can take many arguments, space separated
- $ echo 0{5..9} {10..12}
- > 5 6 7 8 9 10 11 12
- Binary Trick
- $ echo {0..1}{0..1}{0..1}
- > 000 001 010 011 100 101 110 111
- Make folders with paths
- $ mkdir -p test/{1..10}/{1..10} [w]
- Date Trick. Name folders by date. 0 is to pad 01-09. 12 mo in a yr.
- $ mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12}
- My Zelda map trick (unused). Makes a big list of 27 names z00_00 - z09_09
- $ echo z0{0..9}_0{0..9} > file.txt
- Nested brace expansions:
- $ echo a{A{1,2},B{3,4}}b
- aA1b aA2b aB3b aB4b # comma is just a list?
- Change 'seq' delimiter from newline to space
- $ seq -s' ' 12
- Or pipe to translate. (end with a newline)
- $ seq 12 | tr '\n' ' '; echo
- > 1 2 3 4 5 6 7 8 9 10 11 12
- Sections generator:
- $ for i in {1..3}; do echo "Question $i" && echo; done
- Generate empty text files in folders for OU course. That's 4 Blocks, 6 Parts, each with up to 20 files:
- $ touch script.sh && chmod +x script.sh
- ```
- mkdir -p Block{1..4}/Pt{1..6}
- sleep 1
- touch Block{1..4}/Pt{1..6}/{0..1}{0..9}.txt
- ```
- Or to only do block by block
- ` touch Block2/Pt{1..6}/{0..1}{0..9}.txt `
- Delete oo.txt in every folder
- $ rm Block{1..4}/Part{1..6}/00.txt
- Delete every file in every folder
- $ rm Block{1..4}/Part{1..6}/*
- To duplicate a file 39 times, and number them "01"-"39". [I need "}{0..9" not "1..9" or it skips 10 (09 11)]
- $ for i in {0..3}{0..9}; do cp test.ogg "test$i.ogg"; done
- To echo one thing into all files in a folder
- $ for i in *; do echo '<!-- -->' >> $i; done
- Wipe all files
- $ for i in *; do : > $i; done
- [Use below output as my standard output?]
- To newline an echo pattern (seq only does numbers)
- $ for i in {1..9}; do echo "pg"$i; done
- > ```
- pg1
- pg2...
- ```
- To echo specific integers
- $ for i in 1 4 5; do echo "pg"$i; done
- To delete everything except line 8, inline edit all files ending "txt" (no backups)
- for f in *txt;
- do ( sed '8!d' "$f" ) > foo && mv foo "$f";
- done
- [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"
- $ mv my_dir ../*.bak new_dir
- Join all the files in the current folder together
- $ cat * > bigcat.txt
- Or see the results first
- $ cat * | less
- In a dir I had 3 text files with 1 line of text. I joined them and double spaced
- $ cat * > c.txt
- $ sed G c.txt > ch.txt
- Number all lines (see AllCmds for minutia)
- $ nl -ba -w3 -s' ' file > file_new # my fave. or
- $ cat -n
- # or
- $ sed = in.txt | sed 'N;s/\n/\t/' > out.txt # or
- $ awk '{print NR "> " $s}' in.txt > out.txt
- Four ways to handle file names with spaces in them. From Macworld
- 1. Drag the directory onto the terminal
- 2. Use single quotes (or double):
- $ cd '/Users/directory with spaces'
- 3. Prefix the 'space character' with a backslash (keep tabbing):
- $ cd /Users/username/temp/directory\ with\ spaces
- 4. Replace spaces with a * (or a ?)
- And a welcome but over-detailed explanation why these work StackOverflow
- To strip Youtube transcripts of timestamps, e.g. Hanselman [lol Ytb can do it now]:
- [Updt]
- $ tr -d :0-9 | tr -s '\n' ' ' | tr -s ' ' ' ' < in.txt > out.txt
- H1 - raw
- H2 - colons and numerals removed
- H3 - newlines changed to spaces, any duplicates are 'squeezed' to singles. done
- $ tr -d :0-9 < H1.txt > H2.txt # delete numbers
- $ tr -s '\n' ' ' < H2.txt > H3.txt # change newlines to spaces. Squeeze to single spaces
- $ tr -s ' ' ' ' < in.txt > out.txt # squeeze to single spaces (if it didnt work)
- If that doesnt work then try different line endings '\r\n'
- 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:
- $ tr -s ' ' '\t' < in.txt > out.txt
- But pasting back into GDoc didn't work, cos of PC line endings?
- To extract the tags (single words) from a list of links mixed in, do:
- $ sed '/http/d' < in.txt > out.txt
- 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.
- 4/3/18 Lynxrot. To re-link the html to css after moving folders around, I wanted to edit the path in multiple files.
- [Rewrite this? Clearer folders. "~" delimiters]
- Replace text, only on lines matching a pattern, in all html files
- 5104.html were put in 'htmlandcss'
- 5104.css were in 'style' beside that folder.
- html had 'href="5104.css" ' I wanted to change to 'href="../styles/5104.css" '
- so
- $ sed -i.bak '/link/s/f="/f="..\/styles\//g' *html
- # -i overwrites file; .bak backs up orig; substitute 'f="' (of href); only on lines containing 'link'
- # I cudv changed the delimiter to _ or ~ to avoid backslashes
- $ rm *.bak
- # delete backups, but .bak was in .gitignore anyway.
- ,
- And update index.html hyperlinks from 'href="5104.html" ' to 'href="/COAC/html/5104.css" '
- so
- $ sed -i.bak '/"[0-9]/s/f="/f="COAC\/htmlandcss\//g' index.html
- # 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.
- Oops I originally put 'href="/COAC/html/'
- To remove a slash and correct the path:
- $ sed -i.bak 's/\/C/C/g' index.html
- ,
- Next time maybe "git checkout -- file.html" to revert
- How to edit a copy-dump of FreeCodeCamp's syllabus, remove blank lines, remove words:
- $ sed '/^$/d' f1.txt > f2.txt
- $ sed 's/Complete//; s/Incomplete//' f2.txt > f3.txt
- .
- [See script]
- Or if a syllabus has leading junk, first lesson "Learn HTML" (or "Transcript" on Youtube)
- Get line number (or "nl -ba" or "cat -n") and delete from line before
- $ sed '/Learn/=' f1.txt
- Delete 1st 15 lines
- $ sed '1,15d'
- .
- JS Beta
- Which line has only a 1?
- $ sed -n '/^1$/='
- .
- Which line has only a number?
- $ sed -n '/^[1-9]$/=' [?]
- "m"
- [Hang on
- Numbering Challenges.
- First find what line challenge1 is on:
- $ sed -n '/Hello/=' FCCSyllabusHTML.txt
- > 18
- Delete up to there:
- $ sed '1,16d' FCCSyllabusHTML.txt > h1.txt
- Find what line the last challenge is on:
- $ sed -n '$=' h1.txt
- > 59
- That's how many files to make:
- $ touch ch{0..5}{0..9}.txt
- 28/3/18 I want to rearrange a tab-separated table of keyboard shortcuts. (And I can't alt+drag TextExit?)
- [Delete this spaces bit?]
- I copied in 2 halfs in to v01.txt and v02.txt
- First append new lines to 1st half
- $ echo -e '\n\n\n\n' >> v01.txt # or a for loop?
- $ cat v01.txt v02.txt > v03rd.txt
- # join em
- $ cut -f1 v03rd.txt > v04.txt
- $ cut -f3 v03rd.txt > v05.txt
- # extract the 1st+3rd columns (1st had descriptions, 3rd Mac shortcuts, 2nd had Windows unwanted)
- $ paste v05.txt v04.txt > v06.txt
- To swap a list of pairs so 2nd line is atop 1st.
- First delete the first line, and all blank lines (optional)
- $ sed '1d; /^$/d' mraw.txt > m1.txt
- delete every 2nd line
- $ sed 'n;d' m1.txt > m2.txt
- extract every 2nd line
- $ sed -n 'n;p' m1.txt > m3.txt
- merge em
- $ paste m3.txt m2.txt > m4.txt
- replace tabs (paste) with newlines
- $ tr '\t' '\n' < m4.txt > m5.txt
- Sort a bibliography alphabetically
- $ sort in.txt | cat -s | sed G > out.txt
- Converting a comma separated inline list into a newline list:
- f1.txt - raw "list, like, this"
- $ tr ',' '\n' < f1.txt > f2.txt
- f2.txt - replaced commas with newlines, but with spaces at start
- $ sed 's/^ //' f2.txt > f3.txt
- f3.txt - no spaces at start. done
- Converting a newline list into a comma separated list (opposite):
- f1.txt - raw
- $ tr '\n' ',' < f1.txt > f2.txt
- f2.txt - replaced newlines with commas. Now to put spaces at start
- $ sed 's/,/, /' f2.txt > f3.txt
- .
- And the opposite:
- $ sed 's/, /€/g' < in.txt > f2.txt
- $ tr € '\n' < f2.txt > f3.txt
- Create a vertical list of words, stripping out all non-alphanumeric characters e.g. punctuation and whitespace [from tr man page]:
- $ tr -cs "[:alpha:]" "\n" < file1 > file2
- [Could make practice file by pipe into sort+ uniq?]
- Log what ROMs I already transferred to Gato (OS RetroPie on SD RECOVERY) by checking what's still on USB stick SLIM:
- setup:
- $ cd /Volumes/SLIM/retropie/roms/n64
- $ ls > ~/GatoGot01.txt
- # creates file. Home dir is an easy to reach path
- workflow:
- $ cd ../gba
- $ ls >> ~/GatoGot01.txt
- $ echo "" >> ~/GatoGot01.txt
- # append new line for readability
- [For AllCmds Sed exmpls?]
- A rather fiddly way of formatting a playlist dump how I want:
- Chiptune Stream's last.fm playlist has a 5 line pattern - album, artist, song, More, n minutes ago.
- Manually delete first lines to get pattern. Delete every 5th line
- $ sed 'n;n;n;n;d' raw.txt > f1.txt
- Delete every 4th line.
- $ sed 'n;n;n;d' f1.txt > f2.txt
- Now only album > artist > song. Print every 3rd line to extract songs
- $ sed -n 'n;n;p' f2.txt > f3.txt
- Manually add 2 lines at start. Then print every 3rd line to extract artist
- $ sed -n 'n;n;p' f2.txt > f4.txt
- Make the bridging "by" file
- $ for i in {1..20}; do echo 'by' >> f5.txt; done
- Paste
- $ paste f3.txt f5.txt f4.txt > f6.txt
- Tabs to spaces, newlines to commas, commas to comma spaces
- $ cat f6.txt | tr '\t' ' ' | tr '\n' ',' | sed 's/,/, /g' > f7.txt
- [For AllCmds Sed exmpls?]
- 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)
- Raw: "
- folder1/
- folder2/
- "
- $ sed 's`/ ``; s/^/ /' in.txt > out.txt
- Show the total number of files in the current directory and subdirectories. [from?][works but -print not needed. wc shows line count default anyway]
- $ find . -type f -print | wc -l
- This shows all files (inclu hidden?). word/line/chara count
- $ find . | wc
- [for Tests?]
- Can I use this command?
- A guide mentioned a command "dscacheutil". To check if Hex knows it I started typing and it tab-completed.
- 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.
- Info Tests [some obsolete]:
- $ ls -al - list all hidden files, with more info
- $ <command> --version # check the version of a CLI app or language e.g. vim, python
- $ man <cmd> - show the manual page for the command
- $ type <cmd> - check the type of a command [lxcmd.org]
- $ which <lang> - shows which sys folder the language is? works for progs
- $ echo $PATH - where Hex looks when launching a CLI prog after running a command
- $ printenv | less # shows all environment variables instead of echoing
- $ echo $PS1 - show how the prompt is formatted
- $ <cmd> <file> | less - see results of a command
- $ apropos <cmd> # what other cmds have similar functions?
- $ ifconfig en1 inet - current IP address and connection
- $ ipconfig getifaddr en1
- $ 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