User Tools

Site Tools


files-and-directories

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
files-and-directories [2019/09/20 21:41]
Miroslav Bernát
files-and-directories [2020/05/15 12:26] (current)
Miroslav Bernát
Line 16: Line 16:
 | **touch** <​file>​ | creates an empty file, if it exists, it changes last access and modification time to the current time, **-a** changes last access time only, **-m** changes last modification time only, **-d** “<​YYYY-MM-DD hh:​mm>​“ sets the specified time instead of the current one, **-c** does not create any file | | **touch** <​file>​ | creates an empty file, if it exists, it changes last access and modification time to the current time, **-a** changes last access time only, **-m** changes last modification time only, **-d** “<​YYYY-MM-DD hh:​mm>​“ sets the specified time instead of the current one, **-c** does not create any file |
 | **mkdir** <​directory>​ | creates a directory, **-m** <​permissions>​ sets access permissions,​ **-p** creates several child directories at the same time (if they do not exist already)\\ ''​$ mkdir -p /​dir1/​dir2/​dir3''​ | | **mkdir** <​directory>​ | creates a directory, **-m** <​permissions>​ sets access permissions,​ **-p** creates several child directories at the same time (if they do not exist already)\\ ''​$ mkdir -p /​dir1/​dir2/​dir3''​ |
-| **ln** <​source>​ <​target>​ | creates a hard link to file/directory, **-s** symbolic link |+| **ln** <​source> ​(<​target>​| creates a hard link to the file specified by the first argument; if no target is specified, it creates a link in the working ​directory ​to the source with the same name, **-s** symbolic link to a file/​directory ​|
 | **rm** <​file/​directory>​ | removes a file, **-R** directory with its contents, **-i** asks for confirmation,​ **-f** without confirmation,​ **-v** detailed output | | **rm** <​file/​directory>​ | removes a file, **-R** directory with its contents, **-i** asks for confirmation,​ **-f** without confirmation,​ **-v** detailed output |
 | **rmdir** <​directory>​ | removes an empty directory, **-p** more child directories at the same time | | **rmdir** <​directory>​ | removes an empty directory, **-p** more child directories at the same time |
Line 35: Line 35:
 ^ The Contents of a File ^ ^ ^ The Contents of a File ^ ^
 | **file** <​file>​ | determines file data type (ASCII, PDF, HTML etc.), **-z** the contents of the compressed file\\ ''​$ file * | grep ASCII''​ | | **file** <​file>​ | determines file data type (ASCII, PDF, HTML etc.), **-z** the contents of the compressed file\\ ''​$ file * | grep ASCII''​ |
-| **od** (<​file>​) | (**-o**) prints the contents of a binary file in octal format, **-d** in decimal format, **-x** in hexadecimal format, **-j** <​byte>​ prints data from the specified byte (offset), **-w**<​number_of_bytes>​ displays the specified number of bytes on one line; the first column of the output represents an offset – the sequence of the first byte of the particular line from the beginning of the file; if no file is specified, reads from STDIN\\ ''​$ od -w8 /​usr/​bin/​who''​\\ ''​$ od -j 0474360 /​usr/​bin/​find''​ | 
-| **strings** <​file>​ | prints text strings contained especially inside a binary file with a minimum length of four characters\\ ''#​ strings $(which vsftpd) | egrep "​libwrap|hosts"''​\\ (checks if the program is related to tcp wrappers) | 
 | **cat** (<​file>​) | prints the contents of a text file, **-n** numbers all lines, **-b** numbers lines containing a text; if no file is specified, reads from STDIN\\ ''​$ cat a b c  > abc''​ | | **cat** (<​file>​) | prints the contents of a text file, **-n** numbers all lines, **-b** numbers lines containing a text; if no file is specified, reads from STDIN\\ ''​$ cat a b c  > abc''​ |
 | **tac** (<​file>​) | prints the contents of a text file in reverse; if no file is specified, reads from STDIN | | **tac** (<​file>​) | prints the contents of a text file in reverse; if no file is specified, reads from STDIN |
Line 49: Line 47:
 | **grep** <​pattern>​ (<​file/​directory>​) | searches for strings in a text file containing a specified pattern (always prints the entire line), **-v** prints the lines not containing the specified pattern, **-n** prints the line number, **-c** prints the number of lines containing the specified pattern only, **-l** prints the names of files containing the specified pattern only, **-L** prints only the names of files not containing the specified pattern, **-i** ignores case distinctions,​ **-f** <​file>​ reads patterns from a specified file (one per line), **-o** prints only the part of a line matching the pattern, **-E** identical to „egrep“ command, **-R** recursively,​ **-w** finds the whole words that match the pattern, **--color** displays the matched string in color; if no file is specified, reads from STDIN\\ ''​$ grep linux notes''​ / ''​cat notes | grep linux''​\\ ''​$ ls -l | grep '​^d'''​\\ ''​$ grep '​^P.*r$'​ notes''​\\ ''​$ grep -E '​AB|CD'​ notes''​\\ ''​$ grep -R linux ~/​data''​ | | **grep** <​pattern>​ (<​file/​directory>​) | searches for strings in a text file containing a specified pattern (always prints the entire line), **-v** prints the lines not containing the specified pattern, **-n** prints the line number, **-c** prints the number of lines containing the specified pattern only, **-l** prints the names of files containing the specified pattern only, **-L** prints only the names of files not containing the specified pattern, **-i** ignores case distinctions,​ **-f** <​file>​ reads patterns from a specified file (one per line), **-o** prints only the part of a line matching the pattern, **-E** identical to „egrep“ command, **-R** recursively,​ **-w** finds the whole words that match the pattern, **--color** displays the matched string in color; if no file is specified, reads from STDIN\\ ''​$ grep linux notes''​ / ''​cat notes | grep linux''​\\ ''​$ ls -l | grep '​^d'''​\\ ''​$ grep '​^P.*r$'​ notes''​\\ ''​$ grep -E '​AB|CD'​ notes''​\\ ''​$ grep -R linux ~/​data''​ |
 | **egrep** / **grep -E** <​pattern>​ (<​file/​directory>​) | searches for strings in a text file using extended regular expressions;​ if no file is specified, reads from STDIN\\ ''#​ egrep -v '​^[#;​]|^$'​ *conf*''​\\ (prints all lines in configuration files that are not commented or empty)\\ ''​$ egrep -o '​^[^:​]+'​ /​etc/​passwd''​\\ (displays all users in the system) | | **egrep** / **grep -E** <​pattern>​ (<​file/​directory>​) | searches for strings in a text file using extended regular expressions;​ if no file is specified, reads from STDIN\\ ''#​ egrep -v '​^[#;​]|^$'​ *conf*''​\\ (prints all lines in configuration files that are not commented or empty)\\ ''​$ egrep -o '​^[^:​]+'​ /​etc/​passwd''​\\ (displays all users in the system) |
-| **sed** (<​address>​) <​command>​ (<​file>​) | filters and edits a text in a noninteractive way (often used in scripts), **-e** <​script>​ executes a specified script, **-f** <​file>​ executes a script included in the specified file, **-i** saves the changes directly in the file (otherwise changes need to be saved by using redirections),​ **-n** does not print the output on STDOUT; the address represents either a sequential number from the beginning of the input (e.g. 1,10 = range of lines), „$“ character (last line of the input) or a pattern (regular expression) bounded from the both sides by a separator – normally by „/“; command **s** replaces the pattern by the following expression bounded by separators (only the first matching pattern), //n// exactly //n//-th pattern occurence, **g** replaces the pattern globally, in each occurence, **a** appends an expression below the line with the specified pattern, <​n>​**p** prints a particular line of the file (with option "​-n"​),​ **d** removes the whole line containing the specified expression, **!**<​command>​ executes a command for all addresses except for those specified; if no file is specified, reads from STDIN\\ ''​$ echo "11 x, 22 x, 33 x" | sed '​s/​x/​y/​2'''​\\ (replaces only the second occurence of „x“ pattern by „y“)\\ ''​$ sed -i '​s/​string1/​string2/​g'​ file''​\\ (replaces the first string by the second one within the whole file)\\ ''​$ sed 's/ \+/​\t/​g'​ test1 > test2''​\\ (replaces multiple spaces by one tab)\\ ''​$ echo "Log retention:"​ $(sed -n '​3p;​6p'​ /​etc/​logrotate.conf)''​\\ (prints 3rd a 6th line of the file)\\ ''​$ sed '/​dev/​!d'​ file''​\\ (prints all lines containing a „dev“ expression)\\ ''​$ sed -i '​1,​10d'​ file''​\\ (removes first 10 lines of the file)\\ ''​$ sed -i '​5,​$d'​ file''​\\ (removes all from the 5th line to the end of the file)\\ ''​$ sed '​1,/​START/​d'​ file''​\\ (removes all up to and including the word „START“)\\ ''​$ sed '​1,/​STOP/​!d'​ file''​\\ (removes all from the word „STOP“)\\ ''​$ sed -e '/​^#/​d;​ /^$/d' file1 > file2''​\\ (removes all comments and empty lines in the file)\\ ''#​ sed -i '/​auth%%[[:​blank:​]]%%\+required%%[[:​blank:​]]%%\+pam_deny.so/a auth\trequired\tpam_tally2.so deny=5'​ /​etc/​pam.d/​system-auth-ac''​\\ (appends an expression below the line with the specified pattern)\\ ''​$ sed '/​^[^#​]*\(sha[25]\|md5\)/​!d'​ /​etc/​pam.d/​system-auth /​etc/​pam.d/​common-password 2> /dev/null | egrep -o "​sha[1256]{3}|md5"​ || sed '/​^[^#​]*ENCRYPT_METHOD/​!d'​ /​etc/​login.defs''​\\ (displays encryption algorithm for newly created passwords) |+| **sed** (<​address>​) <​command>​ (<​file>​) | filters and edits a text in a noninteractive way (often used in scripts), **-E** allows extended regular expressions to be used, **-f** <​file>​ executes a script included in the specified file, **-i** saves the changes directly in the file (otherwise changes need to be saved by using redirections),​ **-n** does not print the output on STDOUT; the address represents either a sequential number from the beginning of the input (e.g. 1,10 = range of lines), „$“ character (last line of the input) or a pattern (regular expression) bounded from the both sides by a separator – normally by „/“; command **s** replaces the pattern by the following expression bounded by separators (only the first matching pattern), //n// exactly //n//-th pattern occurence, **g** replaces the pattern globally, in each occurence, **a** appends an expression below the line with the specified pattern, <​n>​**p** prints a particular line of the file (with option "​-n"​),​ **d** removes the whole line containing the specified expression, **!**<​command>​ executes a command for all addresses except for those specified; if no file is specified, reads from STDIN\\ ''​$ echo "11 x, 22 x, 33 x" | sed '​s/​x/​y/​2'''​\\ (replaces only the second occurence of „x“ pattern by „y“)\\ ''​$ sed -i '​s/​string1/​string2/​g'​ file''​\\ (replaces the first string by the second one within the whole file)\\ ''​$ sed 's/ \+/​\t/​g'​ test1 > test2''​\\ (replaces multiple spaces by one tab)\\ ''​$ echo "Log retention:"​ $(sed -n '​3p;​6p'​ /​etc/​logrotate.conf)''​\\ (prints 3rd a 6th line of the file)\\ ''​$ sed '/​dev/​!d'​ file''​\\ (prints all lines containing a „dev“ expression)\\ ''​$ sed -i '​1,​10d'​ file''​\\ (removes first 10 lines of the file)\\ ''​$ sed -i '​5,​$d'​ file''​\\ (removes all from the 5th line to the end of the file)\\ ''​$ sed '​1,/​START/​d'​ file''​\\ (removes all up to and including the word „START“)\\ ''​$ sed '​1,/​STOP/​!d'​ file''​\\ (removes all from the word „STOP“)\\ ''​$ sed '/​^#/​d;​ /^$/d' file1 > file2''​\\ ''​$ sed -E '/​^(#​|$)/d' file1 > file2''​\\ (removes all comments and empty lines in the file)\\ ''#​ sed -i '/​auth%%[[:​blank:​]]%%\+required%%[[:​blank:​]]%%\+pam_env.so/a auth\trequired\tpam_tally2.so deny=5'​ /​etc/​pam.d/​system-auth-ac''​\\ (appends an expression below the line with the specified pattern)\\ ''​$ sed '/​^[^#​]*\(sha[25]\|md5\)/​!d'​ /​etc/​pam.d/​system-auth /​etc/​pam.d/​common-password 2> /dev/null | egrep -o "​sha[1256]{3}|md5"​ || sed '/​^[^#​]*ENCRYPT_METHOD/​!d'​ /​etc/​login.defs''​\\ (displays encryption algorithm for newly created passwords) |
 | **awk** '<​program>'​ (<​file>​) | filters a text in a noninteractive way (often used in scripts); the program is a set of rules containing a pattern, action, or both, the action is separated by „{}“; **-f** <​file>​ reads a program from the specified file, **-F** <​string>​ sets input items` separator, **-v** <​variable>​=<​value>​ sets a specified variable within the program; the following built-in variables are available **NR** number of records (lines) separated by a new line, **NF** number of fields in the record separated by a separator (white space by default), particular fields are represented by $1, $2 etc., $0 represents the whole record, **RS** defines records separator, **FS** defines fields separator, **ORS** defines output records separator, **OFS** defines output fields separator; the following operators are used to compare strings **==** is equal to, **!=** is not equal to, **<** less than, **>** more than, **<=** less than or equal to, **>=** greater than or equal to; regular expressions are delimited by „/​“„/​“,​ for comparison use **~** pattern matches RE, **!~** pattern does not match RE; **||** logical „or“, **&&​** logical „and“; if no file is specified, reads from STDIN\\ ''​$ awk '/​nameserver/​ {print}'​ /​etc/​resolv.conf''​\\ (prints all lines containing „nameserver“)\\ ''​$ awk '​{print $1, $2}' /​etc/​fstab''​\\ (prints each first and second word of each line of the file)\\ ''​$ awk -F ":"​ '​{print $1, $7}' /​etc/​passwd''​\\ (prints all users and their login shells)\\ ''​$ awk '​{print $1 > "​file1";​ print $2 > "​file2"​}'​ test''​\\ (copies the first and second word of the file to the specified files)\\ ''​$ awk '​length > 10' list.txt''​\\ (prints lines longer than 10 characters)\\ ''​$ awk 'NF < 5' list.txt''​\\ (prints lines shorter than 5 words)\\ ''​$ awk 'NR == 5' error.log''​\\ (prints 5th line of the file)\\ ''​$ awk 'NR == 5, NR == 10' album''​\\ (prints 5th up to 10th line of the file)\\ ''​$ awk '​{print NR,"​->",​NF}'​ list.txt''​\\ (prints number of fields for each record of the file)\\ ''​$ ls -l | grep "​^-"​ | awk '{sum += $5} END {print sum/​1024/​1024}'''​\\ (prints the size of all files in the working directory in MB)\\ ''#​ ne=$(awk -F ":"​ '{if (length($2) > 2 && ($5 == ""​ || $5 >= 99999)) print $1}' /​etc/​shadow);​ %%[[ -z "​$ne"​ ]]%% && echo "​NONE"​ || echo "​$ne"​ | tr " " "​\n"''​\\ (prints users with never expiring passwords)\\ ''​$ sed '/​^[^:​]/​!d'​ /​etc/​inittab | awk -F "#"​ '​{print $1}' | awk -F ":"​ '{if %%(($4 !~ /^\// && $4 !~ /^$/) || ($4 ~ /​[|]+.?​[^/​]/​ && $4 !~ /^$/))%% print $4}'''​\\ (finds commands in the file that are not specified in the absolute path) | | **awk** '<​program>'​ (<​file>​) | filters a text in a noninteractive way (often used in scripts); the program is a set of rules containing a pattern, action, or both, the action is separated by „{}“; **-f** <​file>​ reads a program from the specified file, **-F** <​string>​ sets input items` separator, **-v** <​variable>​=<​value>​ sets a specified variable within the program; the following built-in variables are available **NR** number of records (lines) separated by a new line, **NF** number of fields in the record separated by a separator (white space by default), particular fields are represented by $1, $2 etc., $0 represents the whole record, **RS** defines records separator, **FS** defines fields separator, **ORS** defines output records separator, **OFS** defines output fields separator; the following operators are used to compare strings **==** is equal to, **!=** is not equal to, **<** less than, **>** more than, **<=** less than or equal to, **>=** greater than or equal to; regular expressions are delimited by „/​“„/​“,​ for comparison use **~** pattern matches RE, **!~** pattern does not match RE; **||** logical „or“, **&&​** logical „and“; if no file is specified, reads from STDIN\\ ''​$ awk '/​nameserver/​ {print}'​ /​etc/​resolv.conf''​\\ (prints all lines containing „nameserver“)\\ ''​$ awk '​{print $1, $2}' /​etc/​fstab''​\\ (prints each first and second word of each line of the file)\\ ''​$ awk -F ":"​ '​{print $1, $7}' /​etc/​passwd''​\\ (prints all users and their login shells)\\ ''​$ awk '​{print $1 > "​file1";​ print $2 > "​file2"​}'​ test''​\\ (copies the first and second word of the file to the specified files)\\ ''​$ awk '​length > 10' list.txt''​\\ (prints lines longer than 10 characters)\\ ''​$ awk 'NF < 5' list.txt''​\\ (prints lines shorter than 5 words)\\ ''​$ awk 'NR == 5' error.log''​\\ (prints 5th line of the file)\\ ''​$ awk 'NR == 5, NR == 10' album''​\\ (prints 5th up to 10th line of the file)\\ ''​$ awk '​{print NR,"​->",​NF}'​ list.txt''​\\ (prints number of fields for each record of the file)\\ ''​$ ls -l | grep "​^-"​ | awk '{sum += $5} END {print sum/​1024/​1024}'''​\\ (prints the size of all files in the working directory in MB)\\ ''#​ ne=$(awk -F ":"​ '{if (length($2) > 2 && ($5 == ""​ || $5 >= 99999)) print $1}' /​etc/​shadow);​ %%[[ -z "​$ne"​ ]]%% && echo "​NONE"​ || echo "​$ne"​ | tr " " "​\n"''​\\ (prints users with never expiring passwords)\\ ''​$ sed '/​^[^:​]/​!d'​ /​etc/​inittab | awk -F "#"​ '​{print $1}' | awk -F ":"​ '{if %%(($4 !~ /^\// && $4 !~ /^$/) || ($4 ~ /​[|]+.?​[^/​]/​ && $4 !~ /^$/))%% print $4}'''​\\ (finds commands in the file that are not specified in the absolute path) |
 | **tee** (<​file>​) | reads from STDIN and writes to STDOUT and possibly to a specified file, **-a** appends the output to the end of a file\\ ''​$ ls | tee /tmp/test | wc -l''​\\ (the output of „ls“ command is saved to „/​tmp/​test“,​ the number of lines is displayed on STDOUT)\\ ''​$ who | sort | tee -a log1 log2''​\\ (the sorted output of „who“ command is written both to STDOUT and to files „log1“ and „log2“) | | **tee** (<​file>​) | reads from STDIN and writes to STDOUT and possibly to a specified file, **-a** appends the output to the end of a file\\ ''​$ ls | tee /tmp/test | wc -l''​\\ (the output of „ls“ command is saved to „/​tmp/​test“,​ the number of lines is displayed on STDOUT)\\ ''​$ who | sort | tee -a log1 log2''​\\ (the sorted output of „who“ command is written both to STDOUT and to files „log1“ and „log2“) |
-| **cat >** <​file>​ | inserts the contents of STDIN into a file\\ ''​$ cat > /dev/pts/2 **<​-'​**''​\\ ''<​text>​ **<​-'​**''​\\ ''​**ctrl**+**d**''​ |+| **cat >** <​file>​ | inserts the contents of STDIN into a file\\ ''​$ cat > /dev/pts/2 **<​-'​**''​\\ ''<​text>​ **<​-'​**''​\\ ''​**Ctrl**+**d**''​ |
 | **>** <​file>​ / **cat /dev/null >** <​file>​ | removes the contents of a file | | **>** <​file>​ / **cat /dev/null >** <​file>​ | removes the contents of a file |
-| **vi** / **vim** <​file>​\\ **ESC**\\ **:​**<​n>​\\ **:q**\\ **:q!**\\ **:w** / **:w** <​file>​\\ **:wq** / **:x** / **ZZ**\\ **:w!** / **:wq!**\\ **:set number** / **nonumber**\\ **:set ignorecase** / **noignorecase**\\ **:set all**\\ \\ **moving the cursor:**\\ left:\\ **h** / <**left arrow**>\\ **b**\\ **0**\\ right:\\ **l** / <**right arrow**>\\ **w**\\ **$**\\ up:\\ **k** / <**up arrow**>\\ **H**\\ **1G** \\ down:\\ **j** / <**down arrow**>\\ **L**\\ **G** \\ \\ **adding a text:**\\ **i**\\ **a**\\ **I**\\ **A**\\ **o**\\ **O**\\ \\ **removing** / **extracting a text:** \\ **x**\\ **db**\\ **dw**\\ **d0**\\ **d$**\\ **dd** / <​n>​**dd**\\ **d1G**\\ **dG**\\ **:1,$d**\\ \\ **modifying a text:** \\ **yw** / <​n>​**yw**\\ **yy** / <​n>​**yy**\\ **P** / **p**\\ \\ **r**\\ **R**\\ **:​**(<​address>​)**s****/​**<​searched_string>​**/​**<​replacing_string>​(**/​**<​options>​)\\ \\ \\ \\ \\ **u**\\ **U**\\ **.**\\ \\  **searching in a text:**\\ **/​**<​string>​\\ **?​**<​string>​\\ **n** / **N** | starts a „vi“/​„vim“ text editor and creates or opens a file\\ changes the edit mode into the command mode\\ moves the cursor at the beginning of a particular line\\ leaves the editor (provided the file was not changed)\\ leaves the editor without saving possible changes in the file\\ saves the changes in the file / saves the file as a new one\\ saves the changes and leaves the editor\\ saves the changes / saves the changes and leaves the editor with a read-only file\\ numbers the lines / removes line numbering\\ ignores/​respects case distinctions\\ prints option settings\\ \\ \\ \\ one character to the left\\ at the beginning of the previous word\\ at the beginning of the line\\ \\ one character to the right\\ at the beginning of the following word\\ at the end of the line \\ \\ on the previous line\\ on the first line of the screen\\ on the first line of the file\\ \\ on the following line\\ on the last line of the screen\\ on the last line of the file\\ \\ \\ at the current cursor position\\ after the current cursor position\\ at the beginning of the line\\ at the end of the line\\ at the beginning of a new line bellow the current line\\ at the beginning of a new line above the current line\\ \\  \\ one character in the current cursor position\\ characters before the current cursor position to the beginning of the word\\ characters from the current cursor position to the end of the word\\ characters before the current cursor position to the beginning of the line\\ characters from the current cursor position to the end of the line\\ the whole line in the current cursor position / next //n// lines\\ the whole line in the current cursor position to the beginning of the file\\ the whole line in the current cursor position to the end of the file\\ contents of the whole file\\ \\ \\ copies characters from the current cursor position to the end of the word / following //n// words\\ copies the current line / following //n// lines\\ pastes the deleted or copied text before/​after the cursor or above/below the current line (in case of removal/​copy of whole lines)\\ replaces a character in the current cursor position\\ replaces characters in the current cursor positions\\ replaces the searched expression with a new one; address **1,$** specifies the range of the whole document, if the address is not specified, the change affects the current line only, option **g** replaces all appearances of the searched string, option **c** asks for confirmation to proceed\\ '':​1,​$s/​hundred/​100/​g''​\\ (replaces all appearances of "​hundred"​ with "​100"​ within the whole file)\\ restores the last performed change\\ restores all changes made on the current line\\ repeats the last command\\ \\ \\ searches the file forwards for the specified string\\ searches the file backwards for the specified string\\ continues in further searching / searching in the reverse direction | +| **vi** / **vim** <​file>​\\ **v**\\ **Shift**+**v**\\ **Ctrl**+**v**\\ **Esc**\\ **:​**<​n>​\\ **:q**\\ **:q!**\\ **:w** / **:w** <​file>​\\ **:wq** / **:x** / **ZZ**\\ **:w!** / **:wq!**\\ **:set number** / **nonumber**\\ **:set ignorecase** / **noignorecase**\\ **:set all**\\ \\ **moving the cursor:**\\ left:\\ **h** / **Left Arrow**\\ **b**\\ **0**\\ right:\\ **l** / **Right Arrow**\\ **w**\\ **$**\\ up:\\ **k** / **Up Arrow**\\ **H**\\ **1G** \\ down:\\ **j** / **Down Arrow**\\ **L**\\ **G** \\ \\ **adding a text:**\\ **i**\\ **a**\\ **I**\\ **A**\\ **o**\\ **O**\\ \\ **removing** / **extracting a text:** \\ **x**\\ **db**\\ **dw**\\ **d0**\\ **d$**\\ **dd** / <​n>​**dd**\\ **d1G**\\ **dG**\\ **:1,$d**\\ \\ **modifying a text:** \\ **yw** / <​n>​**yw**\\ **yy** / <​n>​**yy**\\ **P** / **p**\\ \\ **r**\\ **R**\\ **:​**(<​address>​)**s****/​**<​searched_string>​**/​**<​replacing_string>​(**/​**<​options>​)\\ \\ \\ \\ \\ **u**\\ **U**\\ **.**\\ \\  **searching in a text:**\\ **/​**<​string>​\\ **?​**<​string>​\\ **n** / **N** | starts a „vi“/​„vim“ text editor and creates or opens a file\\ starts/​quits visual character mode\\ starts/​quits visual line mode\\ starts/​quits visual block mode\\ changes the edit mode into the command mode\\ moves the cursor at the beginning of a particular line\\ leaves the editor (provided the file was not changed)\\ leaves the editor without saving possible changes in the file\\ saves the changes in the file / saves the file as a new one\\ saves the changes and leaves the editor\\ saves the changes / saves the changes and leaves the editor with a read-only file\\ numbers the lines / removes line numbering\\ ignores/​respects case distinctions\\ prints option settings\\ \\ \\ \\ one character to the left\\ at the beginning of the previous word\\ at the beginning of the line\\ \\ one character to the right\\ at the beginning of the following word\\ at the end of the line \\ \\ on the previous line\\ on the first line of the screen\\ on the first line of the file\\ \\ on the following line\\ on the last line of the screen\\ on the last line of the file\\ \\ \\ at the current cursor position\\ after the current cursor position\\ at the beginning of the line\\ at the end of the line\\ at the beginning of a new line bellow the current line\\ at the beginning of a new line above the current line\\ \\  \\ one character in the current cursor position\\ characters before the current cursor position to the beginning of the word\\ characters from the current cursor position to the end of the word\\ characters before the current cursor position to the beginning of the line\\ characters from the current cursor position to the end of the line\\ the whole line in the current cursor position / next //n// lines\\ the whole line in the current cursor position to the beginning of the file\\ the whole line in the current cursor position to the end of the file\\ contents of the whole file\\ \\ \\ copies characters from the current cursor position to the end of the word / following //n// words\\ copies the current line / following //n// lines\\ pastes the deleted or copied text before/​after the cursor or above/below the current line (in case of removal/​copy of whole lines)\\ replaces a character in the current cursor position\\ replaces characters in the current cursor positions\\ replaces the searched expression with a new one; address **1,$** specifies the range of the whole document, if the address is not specified, the change affects the current line only, option **g** replaces all appearances of the searched string, option **c** asks for confirmation to proceed\\ '':​1,​$s/​hundred/​100/​g''​\\ (replaces all appearances of "​hundred"​ with "​100"​ within the whole file)\\ restores the last performed change\\ restores all changes made on the current line\\ repeats the last command\\ \\ \\ searches the file forwards for the specified string\\ searches the file backwards for the specified string\\ continues in further searching / searching in the reverse direction | 
-| **tr** <​string1>​ (<​string2>​) | replaces individual characters from the first string with new ones from the other string, does not overwrite the original file, **-d** removes characters, **-s** repeating characters replaces by one occurrence\\ ''​$ cat file1 | tr '​a-z'​ '​A-Z'​ > file2''​\\ ''​$ cat text1 | tr ' ' '​\t'​ > text2''​\\ (each space in the file is replaced with a tab )\\ ''​$ awk -F ":"​ '​{print $1}' /etc/passwd | tr '​\n'​ ' '''​\\ (new line is replaced with space)\\ ''​$ tr -d ';'​ < text1.txt > text2.txt''​ |+| **tr** <​string1>​ (<​string2>​) | replaces individual characters from the first string with new ones from the other string, does not overwrite the original file, **-d** removes characters, **-s** repeating characters replaces by one occurrence\\ ''​$ echo "FIBRE CHANNEL STATISTICS REPORT"​ | tr '​A-Z'​ '​a-z'''​\\ (converts uppercase to lowercase)\\ ''​$ cat file1 | tr '​a-z'​ '​A-Z'​ > file2''​\\ ''​$ cat text1 | tr ' ' '​\t'​ > text2''​\\ (each space in the file is replaced with a tab )\\ ''​$ awk -F ":"​ '​{print $1}' /etc/passwd | tr '​\n'​ ' '''​\\ (new line is replaced with space)\\ ''​$ tr -d ';'​ < text1.txt > text2.txt''​ |
 | **cut** (<​file>​) | **-c** <m-n> cuts a specified range of characters on each line of the file (meant from the beginning of the line), **-f** <m-n> cuts a specified range of columns (fields) separated by a tab, **-d** <​character>​ defines a field separator instead of a tab; list items are separated by commas; if no file is specified, reads from STDIN\\ ''​$ cut -f 2,4 -d ' ' data''​ | | **cut** (<​file>​) | **-c** <m-n> cuts a specified range of characters on each line of the file (meant from the beginning of the line), **-f** <m-n> cuts a specified range of columns (fields) separated by a tab, **-d** <​character>​ defines a field separator instead of a tab; list items are separated by commas; if no file is specified, reads from STDIN\\ ''​$ cut -f 2,4 -d ' ' data''​ |
 | **paste** (<​file1>​ <​file2>​) | joins contents of files one next to the other (1 column = 1 file), **-d** <​string>​ defines a field separator instead of a tab, **-s** transposition of lines and columns (1 line = 1 file); if no file is specified, reads from STDIN\\ ''​$ paste -s file1 file2 > file3''​ | | **paste** (<​file1>​ <​file2>​) | joins contents of files one next to the other (1 column = 1 file), **-d** <​string>​ defines a field separator instead of a tab, **-s** transposition of lines and columns (1 line = 1 file); if no file is specified, reads from STDIN\\ ''​$ paste -s file1 file2 > file3''​ |
Line 68: Line 66:
 | **convmv** <​file/​directory>​ | changes the type of coding of a file/​directory name, **-f** <​source_code>​ defines the original code, **-t** <​target_code>​ defines a new code, **-i** interactive mode, **-r** recursively,​ **--notest** ensures real recoding of a file name\\ ''​$ convmv --notest -f cp1250 -t UTF-8 *''​ | | **convmv** <​file/​directory>​ | changes the type of coding of a file/​directory name, **-f** <​source_code>​ defines the original code, **-t** <​target_code>​ defines a new code, **-i** interactive mode, **-r** recursively,​ **--notest** ensures real recoding of a file name\\ ''​$ convmv --notest -f cp1250 -t UTF-8 *''​ |
 | **recode** <​source_code>​**..**<​target_code>​ <​file>​ | changes the type of coding of a text file, **-l** prints types of coding, **-f** suppresses error output, **-v** detailed output\\ ''​$ echo 'List of all file names' | recode -f UTF-8..flat''​\\ ''​$ recode ..HTML < page.txt > page.html''​\\ ''​$ find . -name "​*.txt"​ -exec recode cp1250..UTF-8 '​{}'​ \;''​ | | **recode** <​source_code>​**..**<​target_code>​ <​file>​ | changes the type of coding of a text file, **-l** prints types of coding, **-f** suppresses error output, **-v** detailed output\\ ''​$ echo 'List of all file names' | recode -f UTF-8..flat''​\\ ''​$ recode ..HTML < page.txt > page.html''​\\ ''​$ find . -name "​*.txt"​ -exec recode cp1250..UTF-8 '​{}'​ \;''​ |
 +| **od** (<​file>​) | (**-o**) prints the contents of a binary file in octal format, **-d** in decimal format, **-x** in hexadecimal format, **-j** <​byte>​ prints data from the specified byte (offset), **-w**<​number_of_bytes>​ displays the specified number of bytes on one line; the first column of the output represents an offset – the sequence of the first byte of the particular line from the beginning of the file; if no file is specified, reads from STDIN\\ ''​$ od -w8 /​usr/​bin/​who''​\\ ''​$ od -j 0474360 /​usr/​bin/​find''​ |
 +| **strings** <​file>​ | prints text strings contained especially inside a binary file with a minimum length of four characters\\ ''#​ strings $(which vsftpd) | egrep "​libwrap|hosts"''​\\ (checks if the program is related to tcp wrappers) |
 +| **msgfmt** <​file>​.po | converts a text file to a binary format, **-o** <​file>​.mo specifies the output file\\ ''​$ msgfmt -o woocommerce-cs_CZ.mo woocommerce-cs_CZ.po''​ |
 | **convert** <​source>​ <​target>​ | changes file formats and possibly their sizes\\ ''​$ convert image.tif image.jpg''​\\ ''​$ IFS=',';​ cmds="​hostname -s,​date,​ifconfig -a"; for cmd in $cmds; do { echo "​[$USER@${HOSTNAME} ${PWD/#​$HOME/​~}]"​ "​${cmd}";​};​ eval "​${cmd}";​ done > info.txt && convert -background black -fill white info.txt info.png''​\\ (creates a printscreen of the terminal with the specified commands) | | **convert** <​source>​ <​target>​ | changes file formats and possibly their sizes\\ ''​$ convert image.tif image.jpg''​\\ ''​$ IFS=',';​ cmds="​hostname -s,​date,​ifconfig -a"; for cmd in $cmds; do { echo "​[$USER@${HOSTNAME} ${PWD/#​$HOME/​~}]"​ "​${cmd}";​};​ eval "​${cmd}";​ done > info.txt && convert -background black -fill white info.txt info.png''​\\ (creates a printscreen of the terminal with the specified commands) |
 | **aplay** <​file>​ | plays a specified audio file | | **aplay** <​file>​ | plays a specified audio file |
Line 75: Line 76:
 ^ Archiving & Compression ^ ^ ^ Archiving & Compression ^ ^
 | **tar** <​file/​directory>​ | archives or restores data (the source is retained), **-c** creates an archive, **-f** <​file>​ specifies an archive name, **-v** detailed output, **--remove-files** removes the source, **--acls** preserves ACL permissions,​ **--selinux** preserves SELinux settings, **-M** multi-volume archive (data requires more than one medium), **-V** <​name>​ defines a volume name, **-X** <​file>​ excludes files listed in the specified file, **-d** compares data in an archive with data on the disk, **-t** prints the contents of an archive (even compressed),​ **-r** appends files to the end of an archive, **-x** extracts files from an archive, **-C** changes to a directory (specifies a target directory for the archive to be restored in), **-p** preserves the original ownership and permissions during the restore, **-Z** (de)compresses data with compress, **-z** (de)compresses data with gzip, **-j** (de)compresses data with bzip2, **-J** (de)compresses data with xz, **-w** interactive mode\\ ''#​ tar -cvf ssh.tar /​etc/​ssh''​\\ ''#​ tar --acls -cpvf scripts.tar backup.sh restore.sh''​\\ ''#​ tar -cMf /dev/st0 / -V system_bckp''​\\ ''#​ tar -tvf ssh.tar''​\\ ''#​ tar -xvf ssh.tar''​\\ ''#​ tar --acls -xpvf scripts.tar -C /​usr/​bin''​\\ for archiving and (de)compression by compress:\\ ''#​ tar -cvZf ssh.tar.Z /​etc/​ssh''​\\ ''#​ tar -xvZf ssh.tar.Z''​\\ for archiving and (de)compression by gzip:\\ ''#​ tar -cvzf ssh.tar.gz /​etc/​ssh''​\\ ''#​ tar -xvzf ssh.tar.gz''​\\ for archiving and (de)compression by bzip2:\\ ''#​ tar -cvjf ssh.tar.bz2 /​etc/​ssh''​\\ ''#​ tar -xvjf ssh.tar.bz2''​\\ for archiving and (de)compression by xz:\\ ''#​ tar -cvJf ssh.tar.xz /​etc/​ssh''​\\ ''#​ tar --remove-files -cvJf maillog_old.tar.xz maillog-20161225 maillog-20170102''​\\ ''#​ tar -xvJf ssh.tar.xz''​ | | **tar** <​file/​directory>​ | archives or restores data (the source is retained), **-c** creates an archive, **-f** <​file>​ specifies an archive name, **-v** detailed output, **--remove-files** removes the source, **--acls** preserves ACL permissions,​ **--selinux** preserves SELinux settings, **-M** multi-volume archive (data requires more than one medium), **-V** <​name>​ defines a volume name, **-X** <​file>​ excludes files listed in the specified file, **-d** compares data in an archive with data on the disk, **-t** prints the contents of an archive (even compressed),​ **-r** appends files to the end of an archive, **-x** extracts files from an archive, **-C** changes to a directory (specifies a target directory for the archive to be restored in), **-p** preserves the original ownership and permissions during the restore, **-Z** (de)compresses data with compress, **-z** (de)compresses data with gzip, **-j** (de)compresses data with bzip2, **-J** (de)compresses data with xz, **-w** interactive mode\\ ''#​ tar -cvf ssh.tar /​etc/​ssh''​\\ ''#​ tar --acls -cpvf scripts.tar backup.sh restore.sh''​\\ ''#​ tar -cMf /dev/st0 / -V system_bckp''​\\ ''#​ tar -tvf ssh.tar''​\\ ''#​ tar -xvf ssh.tar''​\\ ''#​ tar --acls -xpvf scripts.tar -C /​usr/​bin''​\\ for archiving and (de)compression by compress:\\ ''#​ tar -cvZf ssh.tar.Z /​etc/​ssh''​\\ ''#​ tar -xvZf ssh.tar.Z''​\\ for archiving and (de)compression by gzip:\\ ''#​ tar -cvzf ssh.tar.gz /​etc/​ssh''​\\ ''#​ tar -xvzf ssh.tar.gz''​\\ for archiving and (de)compression by bzip2:\\ ''#​ tar -cvjf ssh.tar.bz2 /​etc/​ssh''​\\ ''#​ tar -xvjf ssh.tar.bz2''​\\ for archiving and (de)compression by xz:\\ ''#​ tar -cvJf ssh.tar.xz /​etc/​ssh''​\\ ''#​ tar --remove-files -cvJf maillog_old.tar.xz maillog-20161225 maillog-20170102''​\\ ''#​ tar -xvJf ssh.tar.xz''​ |
-| (<​source>​ %%|%%) **cpio** > <​target>​ | archives or restores data, **-o** creates an archive, **-d** creates a directory tree, **-v** detailed output, **-i** extracts an archive, **-m** retains previous file modification time, **-t** prints the contents of an archive\\ ''​$ ls | cpio -ov > archive.cpio''​\\ ''#​ ls /home | cpio -ov > /​dev/​rmt0''​\\ ''#​ find / -mtime -1 -type f -print | cpio -ov > /​dev/​rmt0''​\\ ''​$ cpio -o > /dev/fd0 **<​-'​**''​\\ ''<​file1>​ **<​-'​**''​\\ ''<​file2>​ **<​-'​**''​\\ ''​**ctrl**+**d**''​\\ ''#​ cpio -ivmd /home/\* < /​dev/​st0''​ |+| (<​source>​ %%|%%) **cpio** > <​target>​ | archives or restores data, **-o** creates an archive, **-d** creates a directory tree, **-v** detailed output, **-i** extracts an archive, **-m** retains previous file modification time, **-t** prints the contents of an archive\\ ''​$ ls | cpio -ov > archive.cpio''​\\ ''#​ ls /home | cpio -ov > /​dev/​rmt0''​\\ ''#​ find / -mtime -1 -type f -print | cpio -ov > /​dev/​rmt0''​\\ ''​$ cpio -o > /dev/fd0 **<​-'​**''​\\ ''<​file1>​ **<​-'​**''​\\ ''<​file2>​ **<​-'​**''​\\ ''​**Ctrl**+**d**''​\\ ''#​ cpio -ivmd /home/\* < /​dev/​st0''​ |
 | **compress** <​file/​directory>​ | compresses a file with the standard compress unix tool (the source is removed), **-c** prints the output on STDOUT and retains the source, **-r** recursively,​ **-v** detailed output | | **compress** <​file/​directory>​ | compresses a file with the standard compress unix tool (the source is removed), **-c** prints the output on STDOUT and retains the source, **-r** recursively,​ **-v** detailed output |
 | **uncompress** / **compress -d** <​file.Z>​ | decompresses a file (the source is removed), **-c** prints the output on STDOUT and retains the source (identical to „zcat“ command), **-v** detailed output\\ ''​$ zcat ssh.tar.Z | tar -x(f -)''​ | | **uncompress** / **compress -d** <​file.Z>​ | decompresses a file (the source is removed), **-c** prints the output on STDOUT and retains the source (identical to „zcat“ command), **-v** detailed output\\ ''​$ zcat ssh.tar.Z | tar -x(f -)''​ |