Bash : Command guide & tips



This page contains some commands i found to be useful.



1) Page typo

This first chapter aims to explain typo rules used in this page (as in many others within this blog).

  • A keyboard key : [esc]
    • e.g : press [enter]
  • A sequence of keys, not pressed at the same time : [key1]->[key2]
    • press [enter]->[backspace]
  • A combination of keys, all pressed together : [key1]+[key2]
    • press [ctrl]+[alt]+[backspace] to restart the X server
  • A value you should replace : <value>
    • cd <dir>
  • A command (to be entered in a terminal) :
    mount server:/dir /mnt



2) CLI tips

2.1 Re-call the last parameter or full command

When typing in some commands on different lines, to get the last “arg”, “param” or anything else you just entered, just press the [esc] key followed by the [underscore] key, this will re-call the last “word” you entered (whether it is an option, a parameter or just a command) .

ls -al /home/test_dir/inside_test_dir
rm <enter now the [esc]->[underscore] sequence> # this is equivalent to "rm /home/test_dir/inside_test_dir"

This will remove the /home/test_dir/inside_test_dir directory, without the needs of entering its path again.


2.2 Search backward AND forward within your command history

The key combination [Ctrl]+[R] will start the so-called reverse-i-search and give you a prompt such as


From this prompt as you type in any keys bash will search and display the matching command from your history.

  • To travel within the history of commands matching the keys you can :
    Press [Ctrl]+[R]again to keep searching backward
    Press [Ctrl]+[S] to go forward from where you are (yes it is just wonderful!)
  • Then you may press enter to launch the selected command, or
  • press [Ctrl]+[G] to exit to a clear prompt, or
  • using right or left arrow keys to edit the selected command, or

Note : if you encounter any problem when trying to go forward using the combination mentioned above ([Ctrl]+[s]), that must be because you terminal is interpreting this very combination in an unexpected way. To fix this you may want to set the stty stop key binding to another value, such as ^x (or even to undef), using the following command :

stty stop ^x

for those using the kde terminal (konsole) you can untick the corresponding option in the konsole Settings menu :
SettingsEdit current profileAdvanced (tabs) → Enable flow control using Ctrl+S, Ctrl+Q (untick this) : Damn the gui! i feel like i am giving tips for windows! such a crap! (there must be a configuration file for this but i do not know its location and definitely do not have spare time for this)
To setup this value (of stty) for every terminal from now, add the previous command (/bin/stty stop ^x) to your .bash_profile.


2.3 Deleting all text from the cursor

  • From the cursor to the END of the line

Pressing [Ctrl+K] deletes all text from the cursor to the end of the line.

  • From the cursor to the BEGINNING of the line

Pressing [Ctrl+X] and then [Backspace] deletes all the text from the cursor to the beginning of the line.


2.3 Negate a globbing expression (AKA pathname expansion)

To achieve this wonderful piece of bash you just need to activate the extglob option for your shell, using this command

shopt -s extglob

And here we go! to negate a globbing expression just use the !(glob_expr) syntax, as

ls -l !(*Pierre*) # this would list anything BUT files or directories containing Pierre in its name... good...

To go back to previous setting enter

shopt -u extglob

I will be adding more about the shopt command soon, but now it id time to go to bed, good night.



3) Commands guide

Here is a list (not exhaustive at all!) of the commands and their options i used (sometimes or every time).

  • dd
    • Create an ISO image of a CD/DVD :
      dd if=/dev/cdrom of=/path/to/destination_file.iso
    • Create a full hdd backup:
      dd if=/dev/sda of=/path/to/sda_backup.img

      note : The “.img” file is nothing but a zipped file you may extract it to modify the content and recreate the .img file(e.g to build a custom initrd image)

    • Backup MBR
      dd if=/dev/sda of=/path/to/destination_file count=1 bs=512

      note : You may consider backing up disk informations when using dd to backup full or part of a drive (something like: fdisk -lu /dev/sda > sda_info.txt), one may also consider compressing the .img file with compression tool like tar or gzip.

  • declare
    • When used in a function declare creates a local variable.
  • du
    • Summarize disk usage of each FILE, recursively for directories :
      du -sh <path>
    • Sorting by size all directories of a given <path> , note that you will have a better result without the -h or -k options as sortmay have some problems dealing with differents units such as Mo, Go, Ko … :
      du -sk <path>/* | sort -rn
Options Effect Comments
-s summarize, otherwise displays the size of each <dir> in <path>
-h display size in human-readable format
-c display a grand total when giving multiple <path> as arg
-x skip directories on different file systems useful to check only your /rootfilesystem (excluding others physical hdd)


  • mount
    • mount a <filesystem> to a <mount_point>:
      mount /dev/cdrom /mnt


      mount server:/test /media/server_test


      mount -t iso9660 /home/pier/RHel55esx64.iso /mnt -o loop
Options Effect Comments
t filesystem type for a NFS mount, you usually don’t need to specified the filesystem, as for most filesystems (either a superbloc or the syntax will be enough for mount to guess the fs type)
v verbose
a all : mount every fs found in /etc/fstab
o specify an option / option_list This field allow you to specify useful options, as seen in /etc/fstab file, see following row for possibles options and there effects.
-o defaults Use default options: rw, suid, dev, exec, auto, nouser, and async.
-o user user Allow an ordinary user to mount the filesystem. The name of the mounting user is written to mtab so that he can unmount the filesystem again. This option implies the options noexec, nosuid, and nodev (unless overridden by subsequent options, as in the option line user,exec,dev,suid).
-o users users Allow every user to mount and unmount the filesystem.
-o noatime noatime Do not update inode access times on this filesystem (e.g, for faster access on the news spool to speed up news servers).
-osync/async sync / async sync: All I/O to the filesystem should be done synchronously. In case of media with limited number of write cycles (e.g. some flash drives) “sync” may cause life-cycle shortening.async: All I/O to the filesystem should be done asynchronously. kept in a buffer ’till its time!
-o ro / rw ro / rw Mount the filesystem read-only / read-write
-oexec/noexec exec / noexec Permit execution of binaries. / Do not allow direct execution of any binaries on the mounted filesystem.
-o auto/noauto Can be mounted with the -a option. / Can only be mounted explicitly (i.e., the -a option will not cause the filesystem to be mounted)


  • grep
    • grep a <pattern> in a <file>
      grep "string" /path/to/file
    • grep a <pattern> containing special character (such as a “$” or a “*“) you don’t want the shell to evaluate, (use the simple quote properties) in a <file>
      grep '$TEST' /path/to/file

      note : In this example the shell will first evaluate the =$TEST= variable (the shell replace the variable by its value, if defined) and then grep for the value.

    • grep a <pattern> in multiple files, recursively
      grep -R "string" /path/to/file/*
    • grep a <pattern>without getting the annoying “binary file match…” and error messages
      grep -Is "string" /path/to/dir/

      note: Please do not use the cat, less or more with a pipe when “grepping” as this is useless and resource consuming! (e.g DO NOT DO THAT: cat <file> |grep <string>)

    • Others useful options:
      1. -w: Match whole word only
        grep -w "/dev/nul" # this would match /dev/nul but not /dev/null


      • Different grep implementation
  1. grep, stands for global regular expression print, search a pattern in all files within current directory
  2. egrep (or grep -E), stands for “extended grep” where additional regexp metacharacters have been added (such as “+”, “?“, “|” and “()“).
  3. fgrep (or grep -F), stands for “fixed or fast grep” where no regexp metacharacters is being evaluated.
  • useradd
    • add a user, with a UID=555, create his $HOME dir if does not exists
      useradd -u 555 -g <groupname_or_GID> -d /path/to/home_dir -c "gecos field" -s /path/to/login_shell
    • same as above but we want to give the root UID(0) to the newly created user
      useradd -o -u 0 -g <groupname_or_GID> -d /path/to/home_dir -c "gecos field" -s /path/to/login_shell username

      note : If one wants to launch a unique shell script as a given <user> or command just specify the desired shell script or command path in the login_shell path (after the “-s” option)

  • ipmi
    ipmi is a Shell interface to an IPMI system. (such as the one implemented on a DELL server: backplane / BMC /IDRAC)

    • view the chassis power status of host (at IP_@) using username <username> <passwd>
      ipmitool -P <passwd> -U <username> -H <IP_@> chassis power status
  • pgrep
    pgrep, pkill– look up or signal processes based on name and other attributes.

    • print PID of a process name matching <name>
      pgrep konqueror
    • print PID of a process name matching <name> own by the given GID (“0” will be interpredted as the pgrep lancher GID) (-G to match only the real group, -u and -Ufor same purpose apllied to UID)
      pgrep -g 1850 konqueror
    • print PID of a process name matching <name>, the matching is search against the whole command line of the process
      pgrep -f
    • print PID of a process name NOT matching <name> (negate the matching)

      pgrep -v konqueror


    • print PID of a process nameEXACTLY matching <name>

      pgrep -x konqueror
  • cut
    cut allow you to remove sections from each line of files or STDIN.
    cut is often used to sort / format piped stream. Default separator is set by $IFS

    • print characters from number 1 to 10 of <file>
      cut -c1-10 <file>
    • print characters from number 1 to 10 AND from 14 to the end of <file>
      cut -c1-10,14- <file>
    • print only the second field defined by delimiter “=”
      cut -d"=" -f2 <file>
  • split
    split is a splitting tool (yes it is!), used to cut any file into pieces.
    From man split: Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, …; default size is 1000 lines, and default PREFIX is ‘x’. With no INPUT, or when INPUT is ““, read standard input. Mandatory arguments to long options are mandatory for short options too.
    split <source_file> in pieces of <pieces_size> (in octets), output to <dest_part_name.prefix>

    split -b <em>&lt;pieces_size&gt;</em> <em>&lt;source_file&gt;</em> <em>&lt;dest_part_name.prefix&gt;</em><strong></strong>

    note : use cat to recover the <source_file> as it was (a “solo” file).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.