Bash - Anton-L-GitHub/Learning GitHub Wiki

Help

Command Expl extra
man <command> Opens "manual-page for command"
<command> [-h / --help] Print help for command
file <fileName.Example> Get file info

Man page guide:

  • [Frivillig] - Valfritt / måste inte.
  • <Måste> - Måste ha med.
  • ... - Kan använda flera / kan repeteras.
  • Val1 | Val2 - Använd val1 OR val2, inte båda.
  • ReplaceMe - Finstilt, skriv något där.

History

Command Expl extra
history get your history
  • Get list of used commands
  • !<historynumber> - to choose a command.
  • !! - Last used command
  • History is saved to your user.

Navigate, files & folders.

Command Expl extra
cd Change directory
pwd print working directory
mkdir print working directory -p - skapa ett helt mapträd
exit log out from terminal

Text manipulation

Command Expl extra
cat Concatinate
touch Create empty file / Update file

Create a file

touch <fileName.example> ...

General info

commandName -options inputs
  • -d = short option
  • --long = long option, kan inte chainas/buntas ihop som korta kan.
  • date -a -b -c -d -e = ->
  • date -abcde (Ordningen spelar ingen roll)
  • command1; command2 - Run multiple commands.
  • * - Wildcard

Pipes

Out- / Inputs

  • | - Bring result (object) to next side.
  • > - Redirect output to... (Truncate/Write-over)
  • >> - Redirect output to... (Append)
  • < - Standard input to...
  • tee - Skicka output till två ställen.
  • xargs - Används i pipeline när ett kommand inte kan pipeas direkt och behöver argument(xargs). Typ som $_/throwaway variable.

Standard input = 0
Standard output = 1
Standard error = 2

0< - Redirect standard input from...
1> - Redirect standard output to...
2> - Redirect standard error to...

Wildcards

Globbing

*

  • Match everyting
  • ls A* - List everything that starts on 'A'

?

  • ls ???e1.txt - 3 unknown chars and ends with e1.txt (ex. file1.txt)

[]

  • Match manythings
  • ls file[1234567890] - Match file0 to file9
  • ls file[0-9] - Same as above..
  • ls file[0-9][0-9] - file00 -> file 99
  • ls file[0-9][0-9] - Examples:
  • cat > input.txt - Write to file.
  • cat < input-txt - Write out from file.

{}


Navigation

Command Expl flags
ls lists items -l - Longer format -a -All files
<command> [-h / --help] Print help for command
file <fileName.Example> Get file info
echo print
pwd print working dir
cd change directory . -Current directory
.. -Parent directory
~ -Home directory
- -Previous directory
touch <filename.Example> creates file, change timestamp
cat <filename.Example> read contents of a file
less <filename.Example> read and navigate through file
history shows previous commands !88 -Chooses the command 88
cp <filename.Example> <path> copy one or more files * -Wildcard
? -Any single char
[ ] -Any char inside the brackets
-r -Copy everything in a dir
-i -Prompt before execution
mv <oldname> <newname> move or rename file -b -Creates a backup
mv <filename.Example> <path> -i -Prompt before execution
mkdir <directoryname> creates directory if not exist -p -Create subdirectories
rm <filename.Example> removes file -f -Forces rm
-i -Prompt before execution
-r -Removes everything in a dir
find </home> -name <filename> searches for a file or dir -type d -Searches for a dir
man <command> prints info about command
whatis <command> brief description of command
alias <name>=<command> creates a temporary alias ~/.bashrc -Save alias here
unalias <name> removes an alias
exit exits the shell
logout logs you out
` ` piping - stdout of command1 becomes stdin for command2
` tee ` writes output to screen and file
env displays environment variables $PATH -Systems search path
cut <filename.Example> cuts a part of text from a file -c 5 -Gets 5th char on each line
-f 2 -Cuts text based on field (default is tab)
-d ";" -Delimiter is ";" (combine with -f)
paste -s <filename> merges all lines together in a file -d ' ' -Delimiter is a space (default is tab)
head <filename> displays first 10 lines (default) -n 15 -Displays first 15 lines
tail <filename> displays last 10 lines (default) -n 15 - Displays last 15 lines
-f -Follows the file as it grows
expand <filename> prints output with tabs converted to spaces
unexpand -a <filename> converts spaces back to tabs
join <file1> <file2> joins files together -1 2 -Joins at field 2 in file 1
split <filename> splits file into multiple (default split at line 1000)
sort <filename> sorts lines (default alphabetical order) r -Reverse order
-n -Numerical order
tr <fromchars> <tochars> translates a set of chars to a different set of chars
uniq <filename> shows unique values, no duplicates (use with sort) -c -Counts number of duplicates
-u -Unique values
-d -Duplicate values
wc <filename> shows total count of word in file -l -Number of lines
-w -Number of words
-c -Number of bytes
-nl -Gives every line a number
grep <search> <filename> searches for chars/patterns in file -i -Case sensitive
sudo <command> runs command as superuser (user must be in /etc/sudoers)
su <username> substitute users (default is root)
adduser <username> creates user (contains helpful features, creates home dir etc.)
useradd <username> creates user
userdel <username> removes user
passwd <username> change user password (requires root access)

stdout, stdin, stderr

Command Expl flags
echo Hello World > filename.Example redirects output to a file
cat < filename.Example input from a file
ls /fake/directory 2> filename.Example redirects error message
ls /fake/directory > filename.Example 2>&1 redirects output and error &> -Does same thing
ls /fake/directory 2> /dev/null no error message will be diplayed
⚠️ **GitHub.com Fallback** ⚠️