Linux Notes¶
The normal settings for /tmp are 1777, which ls shows as drwxrwxrwt. That is: wide open, except that only the owner of a file can remove it (that’s what this extra t bit means for a directory).
Nice commands¶
find duplicate files, generate rm command to remove old same size file to make it remove duplicates, remove echo from xargs.
1 | |
shell programing tips¶
set -x mode (set -o xtrace), enable xtrace for shell script
- print everything as if it were executed, after substitution and expansion is applied
- indicate the depth-level of the subshell (by default by prefixing a + (plus) sign to the displayed command)
- indicate the recognized words after word splitting by marking them like ‘x y’
- in shell version 4.1, this debug output can be printed to a configurable file descriptor, rather than sdtout by setting the BASH_XTRACEFD variable.
set -v mode (set -o verbose) - print commands to be executed to stderr as if they were read from input (script file or keyboard) - print everything before any ( substitution and expansion, …) is applied
Making xtrace print more¶
xtrace output would be more useful if it contained source file and line number. Add this assignment PS4 at the beginning of your script to enable the inclusion of that information:
1 | |
- The
set -eoption instructs bash to immediately exit if any command has a non-zero exit status. - The
set -uoption instructs bash to immediately exit if a reference to any variable you haven’t previously defined - with the exceptions of $* and $@. - The
set -opipefail option instructs bash to prevents errors in a pipeline from being masked. - Setting IFS to $’\n\t’ means that word splitting will happen only on newlines and tab characters.
1 2 3 4 5 6 7 | |
Hint: These modes can be entered when calling Bash:
- from commandline: bash -vx ./myscript
- from shebang (OS dependant): #!/bin/bash -vx
Use Strace to check process status¶
1 | |
Inject debugging code¶
Insert echos everywhere you can, and print to stderr:
1 | |
If you read input from anywhere, such as a file or command substitution, print the debug output with literal quotes, to see leading and trailing spaces!
1 2 | |
Bash’s printf command has the %q format, which is handy for verifying whether strings are what they appear to be.
1 2 3 | |
awk example¶
split and print
1 | |
expect example¶
handle first time ssh login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
coredump file check simple example¶
to check where coredump file will be generate
1 | |
1 2 3 4 | |
grep usage¶
To search the string in a single file,
grep file1.txt -e ‘techieshouts’
To search the full word and not the substring,
grep -w file1.txt -e ‘techieshouts’
To search the full word by ignoring case sensitiveness,
grep -wi file1.txt -e ‘techieshouts’
To search all the files recursively in the directory for the same word,
grep -wir /directory -e ‘techieshouts’
To search and get the line number of the word in all the files
grep -wirn /directory -e ‘techieshouts’
To search and get only the file names that contain the word
grep -wirl /directory -e ‘techieshouts’
To forces grep to print the file name
grep -H abc xxx.txt
hostname¶
To view the current hostname
hostnamectl
To change the system hostname
hostnamectl set-hostname lin
Check the Runlevel In Linux (SysV init)¶
To check the system runlevel
runlevel N 3
In the above output, the letter ‘N’ indicates that the runlevel has not been changed since the system was booted. And, 3 is the current runlevel i.e the system is in CLI mode.
Enable service auto run
chkconfig httpd on
alias releate¶
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt.
1 2 3 4 | |
Yum¶
1 2 3 4 | |