Essential tools Flashcards
Piping and redirection
| > >> 2>/dev/null < du -h / 2>/dev/null | sort -nr | head -n 5
./script.sh 2>errors.log 2>&1
all output and errors will be shown on a screen despite the first redirection of errors to errors.log file
file< commands > file2
ls nosuchfiles 2>/dev/null
all errors will be sent to nowhere
ls -l /etc | wc -show how many lines, words and characters are in the output of ls -l /etc commannd
Show environment vars
env
Stdin, stdout, stderr
stdin: 0
stdout: 1
stderr: 2
Show users logged in to the system
Show name of current user
who
whoami
grep
-R -recurcive
-l -show only file names
-w -match whole word
-A number -print number of lines after the match
-B number- print number of lines before the match
n- add line number
grep ‘^$’ -search for blank lines
Regexp should be surrounded by single quotes.
Man pages
- exec programs and shell commands
- system calls- function provided by kernel
- library calls
- special files typically found in /dev
- files formats and conventions
- games
- miscellaneous
- for sysadmins
- for non-standard kernel routines
man -k command= apropos command
mandb- this command is used to update man pages db
Vim
dd -delete line yy -copy line p -paste copied v- visual block y -copy block which was visualized u -undo crtl-r -redo gg/+shift+g -go to the beginning G/shift+g - go to the end cw- while in command mode immediately switches to insert mode removes current word and allows to change this to something else cc-removes the line and put into insert mode shift+r -replaces current chars with new ones while typing over
/text -forward search
?text- backwards search
n/?- moving forward/backward to the next occurrence of the search
^ -move the cursor to the beginning of the line
$- move the cursor to the end of the line
:%s/old/new/g - search and substitute from old value to new globally
Cockpit
http://localhost:9090
cat options
- A -show non-printable symbols
- b -number of lines
- s suppress repeated empty lines
cut
sort
tr
cut -f 1,2,3 -d : /etc/passwd | sort -n -n- numerical sort tr- translate cut -f 1 -d : /etc/passwd | tr [a-z] [A-Z] - accounts will be shown in uppercase the same: tr [:lower:] [:upper:]
sed
sed -n 4p file -print fourth line of file
sed -i s/old/new file
-i - overwrite the file
sed -i -e ‘2d’ file1- delete second line of file1
-e -edit
Run a command being substituted, similar to sudo command
su -c “fdisk -l”
Redirect both errors and output to the file
vim myfile: hdhjdjd ls -la \:wq chmod u+x myfile ./myfile &> alloutput when running only ./myfile > alloutput without &, an error message will be shown on display and output from ls -la will be added to alloutput file.
Redirect standard error to stdout
fdshfs 2>&1
benefit from this- using output from this command and redirecting it to somewhere else:
dshfs 2>&1 | grep -i ‘failed to search’
or
ls -la huj»_space; myfile 2&>1
cat myfile => invalid option j…
Login as root into login shell
su -
su -l
su –login
Find files
find / -type f -size +100M
find / -exec grep -l user {} \; 2>/dev/null
find / -name "*.tar" -ctime 1 use weak quotes when using file globbing \_\_\_\_\_ -name -name -ctime n -last change time (in days) -mtime n-modified within n days -mtime +3 -modified more than 3 days ago -atime -access time (in days) -empty -uid -type f or -type -d -user user_name- files belonging to specified user
find . -name myfile{1..100} -exec rm -rf {} \;
find /text -empty | xargs rm -f