Posted: . At: 12:04 PM. This was 2 years ago. Post ID: 16152
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A very interesting bash shell trick. Edit the last command in the history.


This is a very interesting bash shell trick. You may use the !! operator to allow editing a misspelled command in your bash history and rerun it correctly. This allows for fixing an error you made with a previous command as shown below.

[ jason@$deusexmachina.local ]
[ Jobs 0. PWD: ~. -bash 3.2.57. ] [ 32-:$ ]
-> ehco hello
-bash: ehco: command not found

As we can see here this is easy to fix.

[ jason@$deusexmachina.local ]
[ Jobs 0. PWD: ~. -bash 3.2.57. ] [ 35-:$ ]
-> !!:gs/ehco/echo/
echo hello
hello

This reruns the command and it is spelled correctly. This is a nifty feature of the bash shell. This allows easy correction of mistakes with no fuss at all.

Print a directory listing of only folders in the current directory using ls.

[ jason@$deusexmachina.local ]
[ Jobs 0. PWD: ~. -bash 3.2.57. ] [ 37-:$ ]
-> ls -dhula */
drwx------@ 75 jason  staff   2.3K 10 Apr 15:27 Desktop/
drwx------@ 75 jason  staff   2.3K 10 Apr 16:53 Documents/
drwx------@ 48 jason  staff   1.5K 10 Apr 08:54 Downloads/
drwx------@ 84 jason  staff   2.6K  3 Apr 10:32 Library/
drwx------+  6 jason  staff   192B 30 Mar 17:06 Movies/
drwx------+ 16 jason  staff   512B 10 Apr 15:27 Music/
drwx------+ 12 jason  staff   384B 10 Apr 16:46 Pictures/
drwxr-xr-x+  5 jason  staff   160B  2 Jan  2019 Public/
drwxr-xr-x   9 jason  staff   288B  8 Apr 15:12 Videos/

The ls -dhula */ command is the best way to do this.

Another way is to only list directories with the bash shell. Just use the find command and ls.

[ jason@$deusexmachina.local ]
[ Jobs 0. PWD: ~. -bash 3.2.57. ] [ 53-:$ ]
-> find . -name *. -maxdepth 1 -type d -exec ls -dhula */ {} +
drwxr-xr-x+ 54 jason  staff   1.7K 11 Apr 11:18 .
drwx------@ 75 jason  staff   2.3K 10 Apr 15:27 Desktop/
drwx------@ 75 jason  staff   2.3K 10 Apr 16:53 Documents/
drwx------@ 48 jason  staff   1.5K 10 Apr 08:54 Downloads/
drwx------@ 84 jason  staff   2.6K  3 Apr 10:32 Library/
drwx------+  6 jason  staff   192B 30 Mar 17:06 Movies/
drwx------+ 16 jason  staff   512B 10 Apr 15:27 Music/
drwx------+ 12 jason  staff   384B 10 Apr 16:46 Pictures/
drwxr-xr-x+  5 jason  staff   160B  2 Jan  2019 Public/
drwxr-xr-x   9 jason  staff   288B 11 Apr 11:47 Videos/

This will list all folders on one level under the current working directory. A very useful Linux trick.

Display an ANSI colour palette with the bash shell. Very useful to see all available bash colours.

e="\033["
for f in 0 7 `seq 6`; do
  no="" bo=""
  for b in n 7 0 `seq 6`; do
    co="3$f"; p="  "
    [ $b = n ] || { co="$co;4$b";p=""; }
    no="${no}${e}${co}m   ${p}${co} ${e}0m"
    bo="${bo}${e}1;${co}m ${p}1;${co} ${e}0m"
  done
  echo -e "$no\n$bo"
done

This prints all colour codes for making a nice colour prompt. This is a useful way to get a certain colour you need.

Print information about your user with the id command.

[ jason@$deusexmachina.local ]
[ Jobs 0. PWD: ~. -bash 3.2.57. ] [ 64-:$ ]
-> id -P $LOGNAME
jason:********:501:20::0:0:John Cartwright:/Users/jason:/bin/bash

This prints the user information as a password file entry. Very interesting to see in action.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.