Sie sind auf Seite 1von 9

Unix Commands

echo
“hello world”

clear
clears the terminal

pwd
print current directory

cd
change directory, either using a relative or absolute path

~
home directory. Example: /home/diego is the same as ~
.
path to your current working directory
..
move working directory to the folder above your current one

you can use tap completition

ls
lists files and folders within your working directory

options:
-1
make a long list

mkdir
make a new directory
name of the directory is the argument
you can use both relative and absolute paths

touch
create a new file
name of the file, and type of file, is the argument

wc
word count
lines words characters

cat
concatenate files
files that are to be concatenated are the arguments
might be used to print text files to the terminal
less
use program designed to read multi-page files
q quit
b return to last page
spacebar next page
arrowkeys scroll up and down

head
prints the first ten lines
tail
prints the last ten lines
programs to glimpse a text

options:
-n x
where x is any natural number
specify the number of lines you want to see

Output Redirection (>)


store text inside a file, rather than printing it on the terminal
Example: echo “text” > text.txt
append (>>)
add text to the end of an existing file
using > instead of >> will overwrite the existing text in that file with the next text, rather than adding
it, which can’t be undone

nano
vim
emacs
are text editors

mv
move a file or folder inside a directory
argument 1: file’s path to be moved
argument 2: destination folder path
can also be used to rename a file or folder, with the first argument being the original file, and the
second being the file’s new name

cp
copies a file or folder
argument 1: file’s path to be copied
argument 2: destionation folder path

options:
-r
keeps the underlying structure of the directory to be copied intact

rm
removes a file
options:
-r to remove an entire directory

Alternatively, try moving a file with mv to the Trash, so that it can be deleted later.
∼/.local/share/Trash

man
search a command’s documentation
argument 1: command you are interested in

use / to start a search, then use n to search the next occurrence of what you are searching, and swift +
n to look at the previous occurrence

q to quit

apropos
search a command

Wildcards and Quantifiers (metacharacters)


*
after or/and before some characters. It stands for zero or more characters of any type. Allows
searching for many files with similar names

grep
a tool to search through text files.
Argument 1: “” regular expression that you are searching for
Argument 2: file or files in which you are looking for instances of the regular expression

egrep
extends grep’s search functionality

“.”
metacharacter that stands for any character in egrep’s regular expression. Example: egrep “a.f”
document will search the character a, followed by any other character, and then followed by f in
document

“+”
metacharacter that stands for one or an indefinite number of repetitions of the previous character in
the regular expression. “*” stands for zero or more repetitions of the previous character. “?” stands
for zero or one more repetition of the previous character

“{}”
specify the exact number of occurrences of the previous characters inside a regular expression. You
can also search for an interval of occurences of the previous characters by adding a “,” between the
numbers in the curly brackets. You can combine this with the “.” metacharacter.

“()” capturing group


by adding a string within the parenthesis and adding curly brackets after them, you can search for an
specific number of iterations of the string

Remember that you can combine metacharacters for more complex searches

Character Sets
\w
search for word characters
\d
search for number characters
\s
search for space characters

-v
option that will search for the invert match, making the search result in only the matches that wouldn’t
have been obtained without adding this option

\W
inverse set of word characters that will search for strings without them
\D
inverse set of number characters that will search for strings without them
\S
inverse set of space characters that will search for strings without them

note that the inverse sets and the -v inverse match don’t work the same way

“[]”
create a set of characters to search for. Example: [aeiou] will search for strings with elements of the
vocal’s set. By adding “^” inside the brackets, you will search for matches of the complement of the set,
such as anything that has non-vocal characters. Use a “-” to specify an inclusive interval of letters or
numbers. The set is sensitive to lower and upper case. The -i option will make grep ignore case when
searching

“\”
allows you to escape the metacharacter functionality if you want to search a character that stands for a
metacharacter

“^”
search at the beginning of a line. Example: ^M will search for lines that start with M, rather than
search for all strings with an M inside them

“$”
search at the end of a line. Example: $n will search for line that end with n, rather than search for all
strings with an n inside them

“|”
pipe that stands for ‘or’, and can be used to search for multiple regexs (regular expressions). Example:
“North|South|East|West” will search for strings with at least one of the regexs separated by the pipe
-n
grep’s option that will add, in the printed output, the line where the search matches are

find
search for files within a directory
argument 1: directory where you want to search. Search will occur in all directories within the
directory of this argument
argument 2: the way you are going to perform the search depends on the selected option

-name
search a file by its name. After this option, use a regular expression as an argument to perform a
search

history
shows the history of commands used in the past. Best to combine it with head or tail

alias
assign an alias that stands for a whole command. Useful for long commands of frequent use
alias = ‘command’

∼ /.bash_history
lists commands that we have used in the past

∼ /.bash_profile
text file that is run every time we start a shell, so it’s a good place to assign alias commands

run source ~/.bash_profile every time you edit the .bash_profile file to make the changes effective

diff
check which lines of text are different between two files

sdiff
make a side-by-side comparison of differences between two files

Checksum or Hashes
hashing programs generate an unique code that allows us to check if we are getting a genuine file or
not

MD5
SHA-1
two commonly used file hashes

|
the pipe takes the previous command’s output as the input of the next one

make
create files based on a makefile document’s structure
if the makefile has an all argument, make will create all the files within all
you can create specific makefile files by adding the name as an argument of make
if the file has already been created and there were no changes in the dependencies, make won’t create
the file, because it’s ‘up-to-date’
makefiles are files that contain a set of rules:
[target]: [dependencies]
[commands]

[all]: ….

uniq

sort

expr
evaluate bash expressions

operators: + , - , / , %, \* (you have to escape the * in order to do multiplication). Notice that bash
does integer division by default and rounds the answer.

bc
bench calculator program allows you to perform more complex calculations

options:
- l use decimals in your calculations
-i use interactive mode (type ‘quit’ to escape)

(tip) combine: echo “expression” | bc – l

Bash programs are run from in order from the first line to the last one

echo $variable
print the value of a variable. Use the $ sign to retrieve a variable’s value

let
modify the value of a variable. Example: let variable=$variable+1

Command substitution
use $(command) to assign a command’s resulting value to a variable

You can access command line argnición Dra. Selene Cancino Optativa guments within your own
scripts using the dollar sign followed by the number of the argument

read variable
prompts the user to type agnición Dra. Selene Cancino Optativa n input, which can be stored in a
variable

Exit status
a number particular to a program that runs or doesn’t run (true=0,false=1)
$?
exit status of the last program

conditional execution
and && command on the right executes only if what’s in the left has an exit status of 0
or | | command on the right executes only if what’s in the left has an exit status of 1

also use && and | | as you would normally use the logical and, or

conditional expressions
always between [[conditional expression]]

(tip): && echo t || echo f


after a conditional expression, in order to output the result of it
if
then
fi
elif
else

arrays
they are ordered lists of values in bash
use parentheses () to assign lists to a variable
variable=(1 2 3 4 5)

to retrieve elements from the array use ${array[number]} where the number stands for the element’s
position within the array, starting from 0. use * instead of a number to retrieve all the elements of the
array and ${array[number]:startingnumber:numberofelements} to start retrieving from a certain
element of an array and some additional elements

use array+={element1 element2 element3} to add elements to an array

${#array[*]} to obtain the number of elements in the array

array[element]=newelement to change a specific element of the array to a new one

braces
brace expansion uses the curly brackets and two periods { .. } to create a sequence of letters or numbers
you can put strings or sequences on either side of the curly brackets and they’ll be “pasted” onto the
corresponding end of the sequence, or you can combine sequences with a comma between brackets {{},{}}

If you want to use variables in order to define a sequence you need to use the eval command in order to
create the sequence

iteration with loops

for i in {}
do
commands
done

count=3
while [[ $count -gt 0 ]]
do
echo "count is equal to $count"
let count=$count-1
done

while will run while the conditional expression is true, so there is a potential for having an infinite loop if
one is not careful

Just like IF statements for and while loops can be nested within each other. Don’t just limit yourself to nested
FOR loops, use nested WHILE loops, or FOR and WHILE loops in nested combinations.

Das könnte Ihnen auch gefallen