Thursday, March 14, 2019

I Can Linux and So Can You (Bash commands) pt.1

Often times I here people talking about wanted to learn Linux but have no clue where to start. I also noticed that those that do rely almost solely on a GUI. So let's say screw that GUI and try doing things on Bash. Open up a terminal and let's get started.

So let's start with some background. Bash, the Bourne Again Shell is based on the Bourne Shell (SH) and is commonly used by Linux and even now Mac. Other shells may be used like Dash (on Debian), Almquist Shell (Ash on Unix, also used on BSD and old Android versions), Tcsh (on BSD), Korn Shell (ksh, variants like mksh are on Android). Most of them are very similar and use similar conventions. I'm going to assume you're on Bash because that seems to be the most common Linux one.

The prompt should end in either a $, which is a standard user or # which is the root user. The prompts themselves may all look different and are customizable. I'll show commands after the prompt and their output. So let's get our bearings here. Let's check out what shell we are in, for that run the command:

$ echo $SHELL
/bin/bash

The $SHELL is a variable that says what shell you're in. Okay, so let's see what directory we are in. Chances are it may say on your prompt, but let's do a command.

$ pwd
/home/matt

You may see a tilde (~) in your prompt, that's the directory. It's shorthand for your home directory. So let's get an idea of the files and directories where we are. It's a very simple command:

$ ls
bash_history  Documents  Music     Public     Videos
Desktop       Downloads  Pictures  Templates

This gives us a basic list of the directory contents. Often times this is actually an alias to include colors based on the file type, like blue for directories. Mine is set as an alias for "ls --color=tty" and may be different for you. Now let's check out some of the flags you may use for ls to see some more information.

$ ls -a
.               Desktop        .msf4       Videos
..              Documents      Music       .viminfo
.armitage       Downloads      .oh-my-zsh  .vimrc
.armitage.prop  .gnupg         Pictures    .wine
.bash_history   .ICEauthority  .profile    .zcompdump-XXX-L-5.6.2
bash_history    .java          Public      .zcompdump-XXX-L-5.7.1
.bashrc         .local         .rnd        .zsh_history
.cache          .maltego       .set        .zshrc
.config         .mozilla       Templates

The -a flag shows hidden files, which are denoted by the beginning period "." You may also notice a single "." and a double ".." These are special, the "." refers to this directory and the ".." refers to the parent directory. Now we may want to look at even more information about our files, like permissions and ownership. For this, we use the -l option, which can also be used with the -a option if you want along the lines of ls -a -l or ls -al.

$ ls -l
total 32
-rw-r--r-- 1 matt matt    0 Jan  3 14:52 bash_history
drwxr-xr-x 2 matt matt 4096 Dec  6 14:17 Desktop
drwxr-xr-x 4 matt matt 4096 Feb 12 13:49 Documents
drwxr-xr-x 2 matt matt 4096 Feb 16 15:15 Downloads
drwxr-xr-x 2 matt matt 4096 Dec  6 14:17 Music
drwxr-xr-x 3 matt matt 4096 Feb 16 15:15 Pictures
drwxr-xr-x 2 matt matt 4096 Dec  6 14:17 Public
drwxr-xr-x 2 matt matt 4096 Dec  6 14:17 Templates
drwxr-xr-x 2 matt matt 4096 Dec  6 14:17 Videos


As you can see, there's a decent amount of stuff, so let's go column by column. The first column first letter is the file type, in this case d is directory and - is a normal file. The next three letters indicate read, write, and execute permissions for the user. An r means they can read, w can write, x can execute but a - in its place means the permission is off. The next three letters are for the group permissions and the last three are for everyone. The next column after that is the number of links, followed by owner, group, size, then a date of last modification and time, then the name of the file. There are still yet more permissions you can view that come into play with selinux. That is the -Z option. An example on a raspberry pi running CentOS I have looks something like this.

# ls -Z
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 anaconda-ks.cfg
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 epel-release-latest-7.noarch.rpm
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 http_fping.mod
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 http_fping.pp
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 http_fping.tt
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 README

The extra column is for selinux stuff. It can add another layer of security on your system and is a little outside of what I want to start with. So let's move on, shall we?

So ls can list directories anywhere, you just simply need to specify a path. But how do we know what path to use? Well, let's go over some Linux directory basics. The main folder is the root, which is /. This is not to be confused with /root, as that is the root user and / is the root to the file system. All of your files get attached under the root directory. Your user file will likely be in /home/<username>. So let's start by taking a glance at the root directory, so a simple ls / should do the trick.

$ ls /
bin   home            lib32       media  root  sys  vmlinuz
boot  initrd.img      lib64       mnt    run   tmp  vmlinuz.old
dev   initrd.img.old  libx32      opt    sbin  usr
etc   lib             lost+found  proc   srv   var

So what are all of these? The ones you should learn to familiarize yourself with are bin, which contains your binaries, or executable programs and sbin which has system binaries. The boot folder has your information for the system booting, like grub (grand unified boot loader). The dev folder contains device files like hard drives, attachable media, discs, ports, sockets, and so on. The configuration files for most stuff on a system level will be in etc. The home folder contains user folders, except for root, that's in root. When you plug in media, it will often be in media. The mnt folder is used for mounting, similar to media I guess. The opt folder contains optional programs which may also be in the usr folder, along with other binaries. The proc folder contains process information and system information. The tmp folder contains temporary files and if you think you lost something or downloaded something and can't find it, you may want to look in here. Other than that, I don't often use or look in the other folders or files myself. They have uses, I just have never needed to actively go into them.

We can probe around further to look into say... etc by doing ls /etc. So now we can look around the system, so let's talk about moving. Specifically, we need to change directory. The command for this is cd, and is the exact same command on Windows cmd. So let's change to root by running this command.

$ cd /

Chances are your prompt has changed replacing a ~ with a /. I would show you, but it varies system to system and can be configured. However now when we do an ls, it will list the / folder and not /home/<username>. So now we can move around, but how do we really jump around? There are some simple quick ways around.

cd ~ will move you to your home directory.
cd - will take you to the directory you were just in, so you can quickly toggle back and fourth.
cd .. will take you up one directory.

With those, you can begin exploring. I'd show some examples, but some that would eat a lot of space real quick. With cd and ls, you're on your way to not needing a file browser! So what other common things do we do with a file browser?

To copy a file, we have the cp command.
To move or rename a file we have the mv command.
To delete a file we have rm.
To delete a directory we can use rm or rmdir if it's empty.

Okay, so let's run through these commands with some context. If we want to copy a file called test1 and call it test2 we would do this.

$ cp test1 test2

Easy enough, and of course file paths can be used to copy to another directory, like so.

$cp test1 ../test2

This would copy it to the parent directory. So let's look at moving and renaming, since it's the same command.

$ mv test1 ../Documents/
$ mv test1 testing123
$ mv test1 /etc/samba/smb.conf

The first command moves test1 to the Documents folder, perhaps say from Downloads. The second one simply renames the file. You can also move and rename at the same time by just including the file name like the third example. So how about deleting a file? Simple.

$ rm test1

Now let's say we have a directory we need to delete.

$ rmdir uselessDirectory

If the directory is not empty, we need to delete everything first. Luckily we can do it all in one shot.

$ rm -R uselessDirectory

The R stands for recursive. It's just a fancy way of saying it deletes everything from the bottom up. Here's where things get a little weird, some systems alias rm to always prompt you about deleting every file. If you don't want a prompt, you use the -f option like

$ rm -R -f uselessDirectory

If it still prompts, it's an alias. We can look at getting around that later. For now, we know we can navigate, move, copy, rename, and delete stuff. So now we can pretty much shove aside a file browser, right? Well... we can still use it for a couple more things, but this seems long enough. Next I'll go over viewing, editing, and altering files.

No comments:

Post a Comment

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)