Bash part 1 Introduction

in bash •  7 years ago  (edited)


Credit

Bash

Bash is often used as the way to interact with the unix shell, there are plenty of alternatives, but since bash is the standard on and I guess os X, that makes it used a whole lot.
Windows even have a bash implementation now.

Glossary

~ - (tilde) current users home dir.
root - both the "admin" and the lowest directory on the system.
. - (single dot) means current directory
.. - (double dot) means one directory up (towards the root)

Navigating the file system.

Since bash is used as a way to interact with the unix system, it is important to be able to navigate the file system, and this is possible in bash, in a couple of ways
one of the more common ways to navigate the filesystem is to use the cd command, this command change directory, some things that are valuable to know is ~ means the current users
home directory, . means current directory and .. means parent directory (meaning up the tree towards the root).

cd might be a nice way to navigate the file system, but it might end up getting a little tiresome, luckily there are some alternatives
for one you can use pushd and popd, these commands allow you to use a stack of directories where pushd puts a directory on the stack, and popd removes one.
This is very nice if you are creating a script, however there is also another thing you can do which makes it a lot easier to visit your favorite files more often.

Modifying your bashrc

Okay you can setup a lot of nice stuff in the bashrc, right now I will only explain how you setup shortcuts to directories, but a lot of things can be done with that.

export music=~/music

That would make your music directory available from anywhere on your system (if you called it music in your home dir).
all you need to do is to type in the following command in your terminal.

cd $music

that would bring you to ~/music.
Though you should be aware that music and Music are different names in unix, case matters.

Alias

Another smart way to navigate the file-system in bash would be to define some useful aliases.
Let us say that I go to ~/Documents often, and would like to have a convenient shortcut to go there.
This would be a fairly simple thing.

alias gd="cd ~/Documents"

now whenever you type gd and then enter you will be moved to ~/Documents, this is much more convenient.
However this does not only work for moving in the filesystem you can do any bash command in an alias, and
give it a shorter name.

Doing Stuff

When you have moved to where you want to do something, then you begin to use more than just bash itself, often the commands you invoke
are actually "small" c programs that lives in the $PATH, but work as if they were built into bash.
One very useful command to know is ls which list the files in the current directory.
This is likely something you will be doing a lot, if you are not quiet sure where you are in the filesystem pwd comes in handy,
It stand for Print Working Directory.

A simple thing that you might want to do would be to quickly add a line to a specific file, this is one of the simpler things that can be done.

echo "What you want to add" >> fileToAddTo.ext

technically you do not need the quotes around most things, the >> is significant that means append, while > means overwrite.
ext is the extension, linux is pretty clever about extensions, so if you have a jpg file that does not have an extension most file browsers
can infer from meta-data what file format it is, but it might be a good habit to give them the correct extension since that helps with organization.
text files might not need an extension, but I have begun to give mine the .txt extension, since that makes it easy to glob them.

Globbing

With bash being a text interface you might think this is horribly inefficient, that is however not the case in many cases it is faster to do
stuff through this old text interface than it would be in a modern gui, this is due to some fantastic features of bash (not unique in the unix world at all).
One of this features would be globbing, globbing means that you can use wildcards as filenames or parts of filenames, so you can in fact use regular expressions
to list files, this is why I use .txt for text files even when it is not necessary.

ls *.mp3

That code would list all mp3 in the current folder, this could be very convenient if you want to build a playlist. Perhaps you want to have each song on
a line by itself in the file songs.txt, that can be done with the following snippet.

  touch songs.txt
  for i in $( ls *.mp3)
      do 
          echo $i >> songs.txt
      done

now you have all the songs in the current folder each on a new line in songs.txt, now let me explain the code snippet.
first we touch the file, this is smart since then we are sure the file it created, echo might create it if it is not already there, touch does however
not truncate the file if it is already existing.
the *for i in $( ls .mp3 ) makes a loop going over the lines between do and done once for each item in the directory, which will be referred to as $i since
we said for i in.

Okay that might be fine to have all the mp3 files in the working directory but what if you want all the mp3 files you have anywhere under you home directory,
turns out there is an option for that.

  cd ~
  ls -R | grep .mp3 

That snippet would list all you mp3 anywhere in your home directory, grep is a program that takes an input and searches for a specific string in that input
if it does not find the string it throws the input away, if it finds the string it reports that string back.

Okay now with this piece of information we could write a bigger shell script that would fetch all the mp3's anywhere in our home directory, however
I like the philosophy of KISS, therefore I would like to introduce a new tool for doing this.

  find ~/ -iname "*.mp3"

Okay that will not put it in a file, rather that command will print the output to STDOUT (typically your screen), if you want to write to a file
you just add the >> filename.ext to the end.
But generally tools are simpler than the equivalent bash for the same thing, otherwise it would be dumb to make a tool for it, if you make it more difficult
than it was before the tool, that way you wont get a lot of users.

  cd ~
  for i in $( ls -R )
  do
      echo $i | grep .mp3 >> songs.mp3
  done

I like to use find for searching, find has a lot of options for fine-tuning the search, but I usually use that setup, it does a basic search
which is what I need most of the time.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Nice post, I feel that someone's productivity on Linux can be improved with Bash. You could automate tasks, for example. Recently, I was working on a project in a STEM class, and greatly benefited from such knowledge.

Thanks, I am considering to make this a series, I think I can add.
I might even be able to find some nice things that more seasoned linux users could benefit from.
I am also considering to write some stuff on applications based on the command line interface, since that is largely what I use, just except for my web-browser, which I probably also could do an article on, since it is not a mainstream browser.

Nice! Can you please post this on reddit.com/r/linux_mentor

You should remove the unix tag and use the linux tag instead. You will get allot more traffic.

Thanks for the tip, sounds like a fair idea, I searched for linux stuff and found you and decided to follow :)

Thanks I can give you my discord username if you ever wanna chat.

Yeah I don't use discord on linux, find it somewhat annoying with the popups.
If you want to chat I do however use XMPP

Haha I stopped using xmpp but I still login sometimes. You can add me [email protected]

Btw if you plan on turning this into a series you can reference the articles on my wiki:

https://yoirtuts.com/index.php?title=Learning_Linux

That is a good resource.