Skip to content

Latest commit

 

History

History
282 lines (194 loc) · 5.65 KB

linux_cheat.adoc

File metadata and controls

282 lines (194 loc) · 5.65 KB

X Linux cheat code

Do for all files

#!/bin/bash
find . -name \*.txt -type f -print0 | xargs -0 -n1 -I {} echo "{}"
find:
  • -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.

xargs
  • -0 receive ASCII NULL character.

  • -n1 process received arguments one at a time

  • -I {} will replace occurrences of {} in command by the actual argument.

Search within files

#!/bin/bash
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.

usefull commands

grep

search into files of current working dir:

#!/bin/bash
grep -r PATTERN
  • -r :

xargs

  • -p : print & prompt the user to run it

  • -I % : This replaces occurrences of the argument with the argument passed to xargs (% in that case).

#!/bin/bash
cat /etc/passwd | xargs -I % echo "### % ###"

diff

#!/bin/bash
diff <(ls new_dir) <(old_dir)   # compare content of 2 dirs

make

Table 1. automatic variable rules
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.

$(@D)

The directory part of the file path of the target. Also apply to <, ?, ^, …​

$(@F)

The file part of the file path of the target. Also apply to <, ?, ^, …​

sed

sed works with streams of characters on a per-line basis use sed where there are patterns in the text.

usage
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)

examples:
# "-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

awk for delimited fields on a per-line basis. Use awk when the text looks more like rows and columns.

Table 2. basics 1
Command

awk '/pattern/ {action}' file

Execute action for matched pattern pattern on file file

$0

Reference current record line

$1 $2 $3 .. $NF

Reference first, second third column and so on .. to the last

;

Char to separate two actions

print

Print current record line

FS

Field separator of input file (default whitespace)

-F

Command line option to specify input field delimiter

NF

Number of fields in current record

NR

Line number of the current record

^

Match beginning of field

~

Match opterator

!~

Do not match operator

BEGIN

Denotes block executed once at start

END

Denotes block executed once at end

str1 str2

Concat str1 and str2

example
$> 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

rename bunch of files

requires : rename, parallel, find

rename all files with .match extension to .matches.

example
find ./ -iname "*.match" -print0 | parallel -j8 rename -v 's/.match/.matches/' {}

parallel

do parallel tasks

Convert all *.wav to *.mp3
parallel lame {} -o {.}.mp3 ::: *.wav

see parallel

watch

execute a command or program periodically

#!/bin/bash
watch -d ifconfig
  • -d highlights the changes in the command output.

expect

command talks to other interactive programs

script.sh
#!/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

CSV

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.

ubuntu alias
function pretty_csv {
    column -t -s, -n "$@" | less -F -S -X -K
}

 — see source for more details : stefaanlippens

disk usage

#!/bin/bash
du -h . | sort -h -r | head -n 10
  • du -h :for human readable format,

  • add du -s to display only a total for each argument,

  • sort -h : tells sort it is human readable format,

  • sort -r : tells reverse sort.