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/04/12 19:18]
Miroslav Bernát
files-and-directories [2019/06/14 10:22] (current)
Miroslav Bernát
Line 49: Line 49:
 | **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 given 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 given 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''​\\ ''​$ grep '​^P.*r$'​ notes''​\\ ''​$ grep -E '​AB|CD'​ notes''​\\ ''​$ grep -R linux ~/​data''​\\ ''​$ ls -l | grep '​^d'''​ | | **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 given 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 given 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''​\\ ''​$ grep '​^P.*r$'​ notes''​\\ ''​$ grep -E '​AB|CD'​ notes''​\\ ''​$ grep -R linux ~/​data''​\\ ''​$ ls -l | grep '​^d'''​ |
 | **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 -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 -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 given 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 ​       required ​     pam_deny.so/​a auth        ​required ​     pam_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** <​script>​ executes a specified script, **-f** <​file>​ executes a script included in the given 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) |
 | **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 given 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 given 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 given 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 given 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“) |
Line 68: Line 68:
 | **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 '​{}'​ \;''​ |
-| **convert** <​source>​ <​target>​ | changes ​picture ​file formats and possibly ​the file sizes\\ ''​$ convert image.tif image.jpg''​ |+| **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 |
 | **arecord** <​file>​.wav | creates a "​wav"​ audio file | | **arecord** <​file>​.wav | creates a "​wav"​ audio file |
Last modified: 2019/04/12 19:18 by Miroslav Bernát

visits: