Bash In Bites





This will be written piece by piece, slowly.

I do strongly recommend that you at least do help commandname or man command name.

This is an introduction, not a fullblown course

It was inspired by a conversation with my beloved wife about how many people only have time for a small bash lesson per day. I her exact phrase was "bite sized bash".




This is too simple! i want GEEKITUDE!



Did you know that:

you can get Kilobyte, and Megabyte markers (human readable) by adding -h to ls
ls -lah


You can separate commands on the commandline with ;
cd DIRECTORY; ls -la


You can make the above look better by using backslash \
cd DIRECTORY\      hit enter here
; echo "cd done"\  hit enter here
; ls -la           hit enter again and it will now execute all three commands


You can make each command dependent on if the previous one worked using &&(AND)
cd DIRECTORY && ls -la

if it didn't cd to the directory, it won't do the "ls -la"


You can make a command dependent on that the former command didn't work,
using ||(OR) (that's two pipesymbols)
ls filename || touch filename

if the file "filename" does NOT exist, ls fails and touch filename is run


Did you know that when doing a temporary "cd" somewhere and then returning it's easier to use pushd and popd?

for instance:
cd /var/log/mail/archived/
ls
cd /users/home/mine/bin/   where you started out
ls

is easier like this:
pushd /var/log/mail/archived/
ls
popd


Did you know that you can cd to your home folder in at least 3 ways?

cd /home/username    (or what ever the full path is)
cd $HOME
cd ~/

The $HOME is a variable that usually exists, but it also means that you can put a long foldername INTO a variable as in:

LONGFOLDER="/var/log/long/longer/longest/example/"
cd $LONGFOLDER

(this won't survive a logout)


Did you know that bash can do maths?

echo $[10+10+10]


Did you know that bash can give you alternative spellings?
echo {h,H}{i,I,1}

(it will yeild "hi hI h1 Hi HI H1")


Did you know that you can refer to a users homedirectory by using ~username
cd ~username


Did you know that you can use "for" on the command line?

mylogin$ ls
Mail/ Procmail/ bmail/ mmail/ restore/
News/ bin/ setiathome/ todo
mylogin$

say i want to back the bmail mmail and Mail folders up, but i don't want Procmail.

for FOLDER in bmail mmail Mail;do cp -R $FOLDER ~/backups/;done

saywha?

say "for" each and every thingy in the list (bmail mmail Mail), put it into the variable FOLDER, then do the bit between "do" and "done" and replace $FOLDER with the contents of it.

in effect above it says:

cp -R bmail ~/backups/
cp -R mmail ~/backups/
cp -R Mail ~/backups/

you just save some typing


Did you know that you can execute a command and use that output as an argument to another command all on the same command line?

have you ever wanted to use the date and exact time in a filename? it's great for backups and similar (well i find it so)

date +%Y%m%d_%H%M%S

under linux gives
YYYYMMDD_HHMMSS
at the time of writing this gives me:
20031125_085500 which translates to ( 25th of November 2003 at 08:55:00 AM )

you want the YYYYDDMM format some people prefer?
date +%Y%d%m_%H%M%S

so how do i get it into the filename?

well you expand it
echo "this is my content..blablablabla" >> `date +%Y%m%d_%H%M%S`.txt

NOTE the backticks around date +%Y%m%d_%H%M%S
please notice that when using second or in some cases even minute "precision" it might be advisable to..not use minute or secon precision actually.

try typing in the above 3 to 4 times and you'll see why

you might be better of with something like

NOW=`date +%Y%m%d_%H%M%S`
echo "my content" >> $NOW.txt
echo "my content2" >> $NOW.txt
echo "my content3" >> $NOW.txt
unset -v $NOW

In effect, take the date this very second and put it into the variable "NOW". We won't be using "NOW" all over the place so there is no need for us to export it. Echo some content into $NOW.txt file (it'll have a YYYYMMDD_HHMMSS.txt look). Unset (empty) the variable "NOW".


Did ya know that as well as backticks (see above) you can use $(commandhere) to execute a command before the main line executes

ls -la $(which perl)

will expand to (on my system) ls -la /usr/bin/perl


Did you know that the differance between " and ' is that ' is more exact?

and that " allows usage of variables inside it


Did you know that:

ctrl is here used to imply ^key

up arrow goes through the last typed commands

ctrl-r allows a "reverse-i-search" where you type and it matches your typed command against the history

ctrl-a moves the cursor to the beginning of the commandline

ctrl-e moves the cursor to the end of the commandline

ctrl-l clears the screen

ctrl-u kills/cuts the entire commandline

ctrl-k kills/cuts everything from what the cursor is at to the end of the line

ctrl-y paste the last text that was killed, at location of cursor

ctrl-_ undo the last thing typed on this command line (this often doesn't work as i want it)

ctrl-z allows you to put most programs into the background, you recall them with fg [jobspec]

history allows you to look through the commands you've used lately in a numbered list

!NUMBER from above history list executes that command

pwd is a command that tells you the full path to where you are in the filesystem

$PWD usually contains the full path to where you are in the filesystem

$OLDPWD usually contains the last folder you were in before this (if you navigate with 'cd')

 These last 2 mean that if you want to use the full path to your location for instance for ls, then ls $PWD will do it, and if you want to go back to the last directory cd $OLDPWD will do the trick


Did ya know that you can access files with spaces and so on in at least three ways?

ls -la "file name"

ls -la 'file name'

ls -la file\ name


This handy little snippet outputs the number of files (not folders/dirs) in the current directory

echo "files in folder $PWD $[`ls -la |grep -vn ^d |tail -1 |cut -f 1 -d:`-1]"

That's truly SICK!

thank you thank you *bows*

  1. echo "files in folder

  2. $PWD, this one contains the current working directory

  3. $[, start some bash mathematics

  4. `, backtick, start a subshell and run...

  5. ls -la, (we all know this one) dump the result into

  6. grep -vn ^d, list all entries, that does NOT(-v) have d as the first letter on the line, do this dump with line numbers, dump the result onto

  7. tail -1, grab the last line, dump the result into

  8. cut -f 1 -d:, cut the first field using ":" as separator (ie the left most thing of the leftmost ":"

  9. `, this marks the end of the subshell

  10. -1, remove one from the output

  11. ]", end the bash mathematics and echo statement quotes




w0nderer