fbpx

Bash Tips and Tricks

This article introduces some lesser known Bash Functions and Commands which are also included in the subject area of the LPI certifications. Please use the feedback function at the end of this article to share any other tips not discussed here.

cd -
change to the previous directory
set +o history
turn off Bash History (.bash_history)
set -o history
turn on Bash History again
apropos <keyword>
search the man pages
Example:
$ apropos telnet
cacaserver (1)       - telnet server for libcaca
telnet (1)           - user interface to the TELNET protocol
telnet.netkit (1)    - user interface to the TELNET protocol
whatis <keyword>
To display the man page description
Example:
$ whatis crontab
crontab (5)          - tables for driving cron
crontab (1)          - maintain crontab files for individual users (Vixie Cron)
Both man pages can be retrieved using: man crontab 5; man crontab 1
pr
Print the text file via the command line
nl
To number the rows
Example:
$ cat test.txt | nl
     1	das ist zeile 1
     2	und das zeile 2
fmt
Format text with a specified width
Example:
$ fmt --width=5 test.txt 
this
is
row
1
and
this
is
row
2
cut -d ' ' -f1
Crop output line by line
-d … delimiter
-f1 … select field 1
Example:
$ cut -d ' ' -f1 test.txt 
this
and
sort
sorts a text file
uniq
removes doubled row. It makes sense to remove any duplicates following a “sort”.
tr
performs substitutions by (“translate”).
Example:
cat test.txt | tr '[:lower:]' '[:upper:]'
DAS IST ZEILE 1
UND DAS ZEILE 2
tr
performs substitutions by (“translate”).
Example:
cat test.txt | tr '[:lower:]' '[:upper:]'
THIS IS ROW 1
AND THIS IS ROW 2
[Str]+[R]
Browse the command history
history
Display command history
find / -iname <Datei/Verzeichnisname>
Browse the file/directories with no case sensitivity. A case-sensitive search can be done using “-name”.
find / -type f -print0 | xargs -0 grep -i <keyword>
Search for specific text in all files /Keyword
find -L . -type l
Find broken symbolic links(that do not have a valid destination)
bash -x <scriptname>
Debugging from Bash-Scripts
rename
Rename the files with common names
Example:
ls
 file-1  file-2  file-3
rename 's/-/_/' file-*
ls
 file_1  file_2  file_3
Dit bericht is gepost in VPS. Bookmark de link.

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *