Skip to content

Instantly share code, notes, and snippets.

@cocoaNib
Created April 2, 2019 10:26
Show Gist options
  • Save cocoaNib/cc1b2bbf39dc2783bbc05d92e0ba39d7 to your computer and use it in GitHub Desktop.
Save cocoaNib/cc1b2bbf39dc2783bbc05d92e0ba39d7 to your computer and use it in GitHub Desktop.
Terminal Basics
Terminal Basics
Befehl beenden (Breaker)
ctrl + c
NAVIGATION / FILE SYSTEM
$ pwd outputs the name of the current working directory
$ mkdir creates a new directory in the working directory
$ cd switches you into the directory you specify
$ cd ..  one up in folder structure
$ cd ../../ two up in folder structure
$ cd ../folder one up and the into folder
$ cd ~ home directory (/Users/username)
$ ls lists all files and directories in the working directory
$ ls -a lists all contents, including hidden files and directories
$ ls -l lists all contents of a directory in long format
$ ls -t order files and directories by the time they were last modified
$ which mate shows location of a script (/usr/local/bin/mate)
$ which name if you add a new script, make sure that the name is unique (test exists!)
FILES
$ touch hello.txt creates a new file inside the working directory
$ nano hello.txt opens a file in a simple editor
$ less hello.txt show file content (quit with q)
$ cat hello.txt outputs the contents of a file to the terminal
$ cp one.txt numbers/ copys a file to directory "numbers"
$ cp -a /source/. /dest/ copies the content of a folder to another existing folder
The -a option is an improved recursive option,
that preserve all file attributes, and also preserve symlinks
The . at end of the source path is a specific cp syntax that
allow to copy all files and folders, included hidden ones
$ mv one.txt numbers/ move a file to directory "numbers"
$ mv one.txt two.txt numbers/ move several files to a directory "numbers"
$ mv foo.txt bla.txt rename foo to bla
rm ⚠️ deletes files and directories permanently. There isn't an undelete command. ⚠️
$ rm name.txt removes a file
$ rm -r comedy deletes a directory and all of its child directories.
* Options modify the behavior of commands:
*   ls -a lists all contents of a directory, including hidden files and directories
*   ls -l lists all contents in long format
*   ls -t orders files and directories by the time they were last modified
*   Multiple options can be used together, like ls -alt
* Wildcards are useful for selecting groups of files and directories
BATCH RENAMING
$ for f in *.png; do echo mv "$f" "${f/1_*_F/1_F}"; done
prints: mv Ani_01_0000_Frame_27.png Ani_01_Frame_27.png
If it works remove echo
$ for f in *.png; do mv "$f" "${f/1_*_F/1_F}"; done
echo
$ echo "Hello" The echo command accepts the string "Hello" as standard input, and echos
Hello               the string "Hello" back to the terminal as standard output
standard input stdin   is information inputted into the terminal through the keyboard or input device
standard output stdout  is the information outputted after a process is run.
standard error stderr  is an error message outputted by a failed process.
$ echo "Hello" > hello.txt  The standard output "Hello" is redirected by > to the file hello.txt.
>       redirects the standard output to a file and overwrites all original content
>>      takes the standard output of the command on the left and appends (adds) it to the file on the right.
cat > >>
$ cat one.txt | wc | cat > statistic.txt
Here the output of cat "one.txt" is the standard input of wc.
In turn, the wc command outputs the number of lines, words, and characters in "statistic.txt", respectively.
sort uniq > >>
$ sort lakes.txt
sort takes the stdin and orders it alphabetically for the stdout. Here, the lakes in sort lakes.txt are listed in alphabetical order.
$ cat lakes.txt | sort > sorted-lakes.txt
Here, the command takes the stdout from cat lakes.txt and "pipes" it to sort. The stdout of sort is redirected to sorted-lakes.txt.
$ uniq deserts.txt          Filters out adjacent (benachbart), duplicate lines in a file
$ sort deserts.txt | uniq   A more effective way is to call sort to alphabetize a file, and "pipe" the standard output to uniq.
                            All duplicate lines are alphabetized (and thereby made adjacent) and filtered out.
sort deserts.txt | uniq > uniq-deserts.txt  Same as above and written to a file
grep (= global regular expression print)
searches files for lines that match a pattern case sensitive and returns the results.
$ grep Mount mountains.txt  grep searches for "Mount" in mountains.txt.
$ grep -i Mount mountains.txt   grep searches case insentitive
$ grep -R   searches all files in a directory and outputs filenames and lines containing matched results
$ grep -Rl  searches all files in a directory and outputs only filenames with matched results.
            -R stands for "recursive" and l stands for "files with matches".
$ sed (= stream editor) is similar to "find and replace"
$ sed 's/rain/sun' weather.txt replace first appearance of the word rain with sun in weather.txt
$ sed 's/rain/sun/g' weather.txt replace all rain with sun in weather.txt
$ clear     clears the terminal window 😀
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment