find . -name \*.txt -type f -print0 | xargs -0 -n1 -I {} echo "{}"
-
-name \*.txt
Its filename must end with .txt -
-type f
Only search for files (not folders) -
-print0
separate found file names with ASCII NULL character.
-
-0
receive ASCII NULL character. -
-n1
process received arguments one at a time -
-I {}
will replace occurrences of{}
in command by the actual argument.
find . -name \*.txt -type f -print0 | xargs -0 grep -Hn '$USER'
-
find .
find all files, folders, symlinks, etc in the current directory recursively -
-name \*.txt
Its filename must end with .txt -
-type f
Only search for files (not folders) -
-print0
and-0
use ASCII NULL character. -
-H
Always print filename headers with output lines. -
-n
Each output line is preceded by its relative line number in the file, starting at line 1.
-
-p
: print & prompt the user to run it -
-I %
: This replaces occurrences of the argument with the argument passed to xargs (%
in that case).
cat /etc/passwd | xargs -I % echo "### % ###"
wildcard | |
---|---|
|
The target of the rule. |
|
The name of the first prerequisite. |
|
The names of all the prerequisites that are newer than the target. |
|
The names of all the prerequisites. |
|
The directory part of the file path of the target. Also apply to |
|
The file part of the file path of the target. Also apply to |
For more details, see: https://www.gnu.org/software/make/manual/make.html#Automatic-Variables
sed
works with streams of characters on a per-line basis
use sed where there are patterns in the text.
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
usefull options:
-
-e
the command (directly as plain text, use-f
to read it from file) -
-i
inplace (change directly the file instead of outputing on stdout)
# "-231.45" -> "(231.45)"
sed 's/-\([0-9.]\+\)/(\1)/g' inputfile
# apply PATTERN1-> PATTERN2 except the first line
sed '2,$s/PATTERN1/PATTERN2/' filename
# keep from line 16224 to 16482 of __filename__
sed -n '16224,16482p;16483q' filename > newfile
# remove
sed -e 's/.../.../;t;d'
# t without any label conditionally skips all following commands, d deletes line.
# keep 1 on 10th files
ls /tmp | sed -n '0~10p'
awk for delimited fields on a per-line basis. Use awk when the text looks more like rows and columns.
Command | |
---|---|
|
Execute action for matched pattern |
|
Reference current record line |
|
Reference first, second third column and so on .. to the last |
|
Char to separate two actions |
|
Print current record line |
|
Field separator of input file (default whitespace) |
|
Command line option to specify input field delimiter |
|
Number of fields in current record |
|
Line number of the current record |
|
Match beginning of field |
|
Match opterator |
|
Do not match operator |
|
Denotes block executed once at start |
|
Denotes block executed once at end |
|
Concat str1 and str2 |
$> awk '{print $3 "\t" $4}' marks.txt
# print out 3rd and 4th fields of mark.txt file
$> awk -F: '{print toupper($NF)}' /etc/passwd
# print last field of ':' separated file upper-case
$> awk -F: 'BEGIN { FS=":" }{print toupper($NF)}' /etc/passwd
# do the same
requires : rename, parallel, find
rename all files with .match
extension to .matches
.
find ./ -iname "*.match" -print0 | parallel -j8 rename -v 's/.match/.matches/' {}
execute a command or program periodically
watch -d ifconfig
-
-d
highlights the changes in the command output.
command talks to other interactive programs
#!/usr/bin/expect -f
spawn ssh [email protected]
expect "[email protected]'s password:"
send "temppwd\r"
set prompt_re {\$ $}
expect -re $prompt_re
send "mkdir -p emma && cd emma\r"
expect -re $prompt_re
interact
Nicely display a CSV file, with columns aligned.
cat data.csv | perl -pe 's/((?<=,)|(?<=^)),/ ,/g;' | column -t -s, | less -S
Make an alias into .bashrc
.
function pretty_csv {
column -t -s, -n "$@" | less -F -S -X -K
}
— see source for more details : stefaanlippens