Bash Box

So far 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’re going to talk about Navigating in Bash. These are the commands used on a daily basis that should become the staples of your command line kung-fu. If these seem a bit simple to you, that’s okay. 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 overlooked the most in Bash is the idea of user context. If you are working with SELinux, this is important and it’s pretty easy to determine your context.

    $ whoami
    mmcclaren

Or we need to know that we have the ability to run a command not in the current user context or 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 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 located in another folder. How do I make sure I know where that is?

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

This looks as if I’m in the /etc/example directory. But what if I take a look at this with the -P flag which avoids symlinks? In that case, I get something different and discover 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 lists the contents of the directory. In most cases, this returns the names of the items and they’re color-coded to indicate if they’re 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 each line is the permissions configuration. Items in this output that begin with the letter “d” are directories. Reading from the left, the next item in the listing is the type: 1 is a file  and 2 is a directory. The remaining information corresponds to the user, group, size, modification time, and finally the file or directory name.

So that tells us what is here, either files or directories, but the output can be difficult to parse. For this we can add the -h argument to our ls -l command. This makes it “human readable”. Notice the difference in the size of the file filelist from the previous example.

[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 we need to take a peek at. For instance, if we are working with ssh configurations in our home directory. First, we need to make sure we are in our user’s 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 period (.).

[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 learn the basics, I recommend you head over to LPI Linux Essentials course. This is a great place to get started, and there are hands-on labs 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 you normally do with your current machine. You’ll get practice and you might surprise yourself when you find you can get everything done using a Free and Opensource operating system!

Until next time keep on learning!

New call-to-action

Leave a Reply

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

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

Get actionable training and tech advice

We'll email you our latest articles up to once per week.