Navigating in Bash | Bash basics

Posted on April 24, 2019 by MichaelMcClarenMichaelMcClaren

Bash Box

So far what we have taken a look at how the AWK command, the SED command and event designators can make your Bash life easier but they really are a touch more advanced than what I would consider Bash Basics. This time we are going to talk about Exploring in Bash. These are the commands that are used on a daily basis that really should become the staples of your command line kung-fu. If these seem a bit simple to you that is ok. If these commands are new to you I hope that they help you. After all,  It’s Okay to Be New!

Who and where am I?

The first thing that we need to do is get a point of reference. Probably one of the things that gets most overlooked in Bash is the idea of user context. If you are working with SELinux this is important and it is pretty easy to determine your context:

    $ whoami
    mmcclaren

or we need to know that we have the ability to run a command that is not in the current user context or something that requires root:

    $ sudo su
    $ whoami
    root

Once we know who we are we need to know where we are.

    [cloud_user@mmcclaren1c ~]$ pwd
    /home/cloud_user

But are we really where we think that we are? The Bash command Print Working Directory has some arguments that we can use to really see where we are. Consider this, I have a directory in my current folder that is a symlink to another directory that is located in another folder. How do I make sure that I know where that is?

    [cloud_user@mmcclaren1c example]$ pwd
    /etc/example

This looks as if I am in the /etc/example directory but if I take a look at this with the -P flag, which avoids symlinks I get something different and this is where I really am.

    [cloud_user@mmcclaren1c example]$ pwd -P
    /usr/local/example

What kind and size am I?

Now that we know where we are we need to determine what is here for this we can use the ‘ls’ command. This command can be used to list the contents of the directory. In most cases, this returns the names of the items and they are color-coded to indicate if they are files or directories. Over SSH, this might not be the case and we need to give the command some help.

[cloud_user@mmcclaren1c ~]$ ls -l
total 16
drwxr-xr-x. 2 cloud_user cloud_user 6 Apr 18 19:36 Desktop
drwxrwxr-x. 2 cloud_user cloud_user 6 Apr 19 14:41 Documents
-rw-rw-r--. 1 cloud_user cloud_user 10810 Apr 19 14:44 filelist
drwxrwxr-x. 2 cloud_user cloud_user 6 Apr 19 14:42 Pictures
-rw-rw-r--. 1 cloud_user cloud_user 68 Apr 19 14:45 thatone
-rw-rw-r--. 1 cloud_user cloud_user 0 Apr 19 14:42 thisone

The -l argument results in the long listing. The first item on the line is the permissions, the items in this output that begin with the letter d are directories. The next item in the listing reading from the left is the type, 1 is a file  and 2 is a directory. Then we see the user / group / size / modification time / file:directory name.

So that tells us what I am, either a file or directory but not how large are the files. For this we can add the -h argument to our ls -l this makes it “human readable” notice the difference in the size of the file named ‘filelist’ from the previous example to the below.

[cloud_user@mmcclaren1c ~]$ ls -lh
total 16K
drwxr-xr-x. 2 cloud_user cloud_user 6 Apr 18 19:36 Desktop
drwxrwxr-x. 2 cloud_user cloud_user 6 Apr 19 14:41 Documents
-rw-rw-r--. 1 cloud_user cloud_user 11K Apr 19 14:44 filelist
drwxrwxr-x. 2 cloud_user cloud_user 6 Apr 19 14:42 Pictures
-rw-rw-r--. 1 cloud_user cloud_user 68 Apr 19 14:45 thatone
-rw-rw-r--. 1 cloud_user cloud_user 0 Apr 19 14:42 thisone

Sometimes there are system files that we need to take a peek at, for instance, if we are working with ssh configurations in out home directory. First we need to make sure that we are in our users context, and then change directory into our home ‘~’ once there we can add the -a argument to see all of the directories even the hidden ones that start with a .

[cloud_user@mmcclaren1c ~]$ whoami
cloud_user
[cloud_user@mmcclaren1c ~]$ cd ~

[cloud_user@mmcclaren1c ~]$ ls -lah
total 64K
drwx------. 12 cloud_user cloud_user 4.0K Apr 19 14:44 .
drwxr-xr-x. 5 root root 49 Apr 13 04:21 ..
-rw-------. 1 cloud_user cloud_user 1.1K Apr 19 15:27 .bash_history
-rw-r--r--. 1 cloud_user cloud_user 18 Oct 8 2018 .bash_logout
-rw-r--r--. 1 cloud_user cloud_user 193 Oct 8 2018 .bash_profile
-rw-r--r--. 1 cloud_user cloud_user 231 Oct 8 2018 .bashrc
drwx------. 8 cloud_user cloud_user 4.0K Apr 18 19:40 .cache
drwxr-xr-x. 10 cloud_user cloud_user 4.0K Mar 8 18:37 .config
drwx------. 3 cloud_user cloud_user 24 Oct 15 2018 .dbus
drwxr-xr-x. 2 cloud_user cloud_user 6 Apr 18 19:36 Desktop
drwxrwxr-x. 2 cloud_user cloud_user 6 Apr 19 14:41 Documents
-rw-------. 1 cloud_user cloud_user 16 Oct 15 2018 .esd_auth
-rw-rw-r--. 1 cloud_user cloud_user 11K Apr 19 14:44 filelist
-rw-------. 1 cloud_user cloud_user 310 Apr 18 19:35 .ICEauthority
drwx------. 3 cloud_user cloud_user 18 Oct 15 2018 .local
drwxr-xr-x. 4 cloud_user cloud_user 37 Aug 15 2018 .mozilla
drwxrwxr-x. 2 cloud_user cloud_user 6 Apr 19 14:42 Pictures
drwx------. 2 cloud_user cloud_user 28 Oct 8 2018 .ssh
-rw-rw-r--. 1 cloud_user cloud_user 68 Apr 19 14:45 thatone
-rw-rw-r--. 1 cloud_user cloud_user 0 Apr 19 14:42 thisone
-rw-------. 1 cloud_user cloud_user 652 Oct 8 2018 .viminfo
drwxr-xr-x. 2 cloud_user cloud_user 4.0K Apr 18 19:35 .vnc
-rw-------. 1 cloud_user cloud_user 2.6K Apr 18 19:35 .Xauthorit

Where should I go to learn more?

If you need to get your Bash skills up to speed and you need to learn the basics, I recommend that you head over to LPI Linux Essentials course. This is a great place to get started and there are hands-on labs there that you can use to get some real world experience working with the command shell. Challenge yourself to install Linux, any distribution of it, and use it to do all of the things that you normally do with your current machine. You will get practice and you might surprise yourself that you can get everything done using a Free and Opensource operating system!

Until next time keep on learning!

New call-to-action

0 Comments

Leave a Reply

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