Gizmo's Freeware Forum

Gizmo's Freeware Forum (https://www.techsupportalert.com/freeware-forum/)
-   Linux (https://www.techsupportalert.com/freeware-forum/linux/)
-   -   Linux command line intro (https://www.techsupportalert.com/freeware-forum/linux/897-linux-command-line-intro.html)

debtboy 24. May 2009 02:38 AM

Linux command line intro
 
OK, here goes...
No matter what distro your using, you will have the ability to open a shell terminal or console
(may have different names such as Term, Xtrem, ETerm, Console, Konsole, etc...)
I believe every distro will open to a BASH Shell (Born Again SHell), next generation of the old Bourne Shell.
Open the shell terminal/console and you should see your login name "@" the computer or domain name.
This is what mine looks like:
Sorry, while posting here, I can only use 4 images, so to see the image, use this link:
http://home.comcast.net/~linuxboy/console1.jpg

This is where the real power of Linux comes from, but for this intro we will discuss only 2 or 3 basic commands.
The first command we will try is ls, this command stands for listing, similar to the old DOS dir command.
It gives us a listing of file/directory names but nothing else.
What we want is a long listing with owner, group, size, etc...
so we add the -l switch. see image:
http://home.comcast.net/~linuxboy/console2.jpg

How are we supposed to remember what "switch" to use like -l,
well there are thousands of commands and most have many switch options,
so a built-in manual was included in every Unix and Linux.
It's called man pages (sorry ladies Ha! Ha! Ha!) you type man and then the command your inquiring about.
For example, in our situation, we would type man ls see image:
http://home.comcast.net/~linuxboy/console_man.jpg

I've already scrolled down to the area I'm interested in, to scroll down you can use the enter key (line by line) or spacebar (page down) see image:
http://home.comcast.net/~linuxboy/console4.jpg

NOTE: now to make matters worse, after you scroll or page to the bottom, you can't get out... WTH!!
enter the letter q which stands for quit. (you will get used to it)

When we use the ls -l command we see a full listing which includes the owner, the group and access permissions
(we'll worry about this later, in another post).

Now lets do a listing ls and then cd (change directory) into one of the directories available.
Once you've gone into the directory, do a long listing of the files within.
see image:
http://home.comcast.net/~linuxboy/console5.jpg

Two more commands you can try is clear
this command will clear the terminal window and
exit this command will exit the terminal.

ls
ls -l
man
cd
clear
exit

debtboy 24. May 2009 01:03 PM

moving around with cd (change directory)
 
Hi everyone :)

Now that your able to cd down into your home directory,
you might want to move up directory by directory or jump directly
to a particular location.
cd .. this will bring you UP one directory

cd ~ or cd ~/ will bring you to your home directory
(if your username is debtboy, it will take you here /home/debtboy)

cd / will bring you directly to the root directory
(the root directory is the uppermost or top directory of the filesystem,
everything resides inside/below the root directory,
do not get this confused with the user root
which is all powerful like OZ (ha! Ha! Ha! :p))

Lets say your in the root directory and you want jump directly to the /usr/share directory, how would you do it?
cd /usr/share
Now you realize you really wanted to go to the /sbin directory so...
cd /sbin

Lets say your not sure where you are, try this command.
pwd I believe it stands for (print working directory)

Now with cd and ls or ls -l you have the ability move all around the filesystem and list files.

NOTE:
The filesystem IS CASE SENSITIVE so keep this in mind.

Next we'll go over that strange looking but all important
permissions structure we see when doing ls -l
drwxr-xr-x

wdhpr 24. May 2009 02:48 PM

Hello Debtboy
I was on mepis this morning walking through the line commands as per your example. Not so intimidating after you see the logic in what its doing. Its not that much differant than dos commands which also uses switches.
The permissions are important. They can be a real head scratcher. :O

Again thanks for sharing

Cheers
Wdhpr

debtboy 24. May 2009 04:18 PM

Permissions
 
Thanks for the positive feedback,
I've never tried explaining this stuff before.

Permissions...

here is a typical example of a ls -l listing:
drwxr-xr-x 2 debtboy users 4096 2009-05-23 20:17 Documents

d This character means directory, you might see - (file), l (link) or d (directory)

Now you have 3 sets of 3 permissions each, always in the exact same order.
The 3 permissions are rwx (Read, Write, Execute) and they can be in any combination, the - hyphen means you don't have this permission.

rwx The first set of 3 are owner permissions, if you created the file you are the owner.

r-x The second set of 3 are group permissions, everyone in the group will have these permissions unless your the owner which gives you additional permissions.

r-x The third 3 are other permissions, I like to think of these permissions as everyone else.

In the listing above you have the owner debtboy
then you also have the group users
so you can determine who has what permissions
(no who is on first :p, what's on second :p Ha! Ha! Ha!)

There are 3 basic manipulation commands:
chown Which changes the owner
chgrp Which changes the group
chmod Which can change permissions for all 3 (owner, group, other)

We are only worried about permissions in this post so:
There are different ways use chmod so I will go over how I use it.
Permissions equate to numbers (octal actually)
r equates to a 4
w equates to a 2
x equates to a 1

I give 3 numbers after chmod the first for the owner, the second for the group and the third for other.

Just add up the equated numbers of the permissions you wish to grant.

For example (using the same listing above):

chmod 777 Documents would produce:
drwxrwxrwx

chmod 724 Documents would produce:
drwx-w-r-- (not your typical permissions but a good example)

The 7 is really rwx or (4 read + 2 write + 1 execute)
A 6 is really rw- or (4 read + 2 write)
A 5 is really r-x or (4 read + 1 execute)
A 4 is really r-- or (4 read)
A 3 is really -wx or (2 wite + 1 execute)
A 2 is really -w- (2 write)
A 1 is really --x (1 execute)
A 0 is really --- or no permissions

All permissions can be manipulated by the user root (all powerful OZ!!) :eek:

This was just a basic overview of permissions (no extended details to confuse),
I'll show 4 images in my next post,
sometimes a picture is worth a thousand words.

debtboy 24. May 2009 05:04 PM

Permissions w/ Images
 
Open up a console/terminal and you should be in your home directory.
If not that would be easy enough to fix cd ~/ remember.

I'm using your home directory, because this is where you have
permission to change permissions (if that makes sense??).
If we were elsewhere in the OS then we would need to
be the user root (all powerful OZ!!) to change permissions.

As you can see from the image, I've done a ls -l command

http://home.comcast.net/~debtboy/chmod1.jpg

See the change in permissions for Documents.

Now I've done a chmod 777 Documents command
Keep in mind the following are the same commands:
chmod 777 /home/debtboy/Documents
chmod 777 ~/Documents


Here is the result:
http://home.comcast.net/~debtboy/chmod2.jpg

See the change in permissions for Documents.

Now I've done a chmod 724 Documents command
so the defined permissions were assigned to the directory.
Did I say directory... yes I did!!
I like to think of it as Linux treats everything as a file,
the hard drive, cdrom drive, directories, etc.. (everything as a file)

Here is the image:
http://home.comcast.net/~debtboy/chmod3.jpg

See the change in permissions for Documents.
Practice will make these commands almost second nature.

Now just for fun, make sure your in you home directory pwd
if not get there you know how...
now type in firefox and hit enter, I believe this will work on most current distros depending on configuration.
Did the browser pop up??? (from the command line, Ha! Ha! Ha!)

http://home.comcast.net/~debtboy/chmod4.jpg

bk_7312 25. May 2009 10:29 AM

I was understanding them well enough to navigate using Konsole using cd, ls - l etc but you kinda lost me on the second part, the part with drwxr stuff. Still, I was able to launch Firefox from Konsole, I got this great feeling when Firefox popped out.

I found that you can type in ls -1 to list directories a bit similar to ls -l but less informative by accident:D.

Thanks for sharing this cool piece of info!

debtboy 25. May 2009 03:55 PM

Viewing a file's contents
 
Hi everyone,

Now that your starting to move around the filesystem with cd,
list files with ls, you may want to view the contents of some of these files.
I generally use more but there are some who prefer less,
either way you can view the contents of selected files which is very handy!!

The key word here is view, so you can NOT change the contents of
any of these files as you could with an editor like vi or emacs.

First lets create a text file named my_text.txt
The .txt extension is meaningless in Linux, but I'm sure
long time Windows user's feel better with it there.

Try this command:
echo "this is a test file" > my_text.txt

http://home.comcast.net/~linuxboy/more1.jpg


Now lets do a listing ls -l and see the file we just created.

http://home.comcast.net/~linuxboy/more2.jpg

Notice that default permissions were issued to the file automatically.

Now lets look inside the file we created.
more my_text.txt

http://home.comcast.net/~linuxboy/more3.jpg

The contents of the file are dumped right into the console.
If you looking at a file that's larger than the console window
you can navigate the same as you would with man pages (Linux manual)
enter key to scroll through line by line
spacebar to scroll page by page
q to quit out of viewing

Now you can change directories cd, list files ls
and view the contents of those files with more.
Start looking around the filesystem and start view the contents of files.
NOTE:
Don't try to view binary files which are normally located in /bin or /sbin directories.

more is especially useful for viewing network config files located in /etc
Try this to view the contents of your resolv.conf file which should call out
the DNS nameservers. (ref: other post about openDNS)
more /etc/resolv.conf

more is used to view files, to make changes you will need permissions and an editor.

debtboy 25. May 2009 05:47 PM

root su sudo
 
Hi everyone,

As you move around the filesystem and view the contents of files
with more, you will eventually run into permission issues.
For example there is a file /etc/sudoers that can only be viewed by root (all powerful OZ!!!).

First we'll cd /etc then ls -l sud* notice the (*) it's a wildcard.
In english I'm asking to change the directory to /etc and then
I want a listing of everything starting with sud

http://home.comcast.net/~linuxboy/su1.jpg

Now I try to view this file with more and WTH!!!
Notice the permissions, according to those permissions,
Only root or anyone in the root group can read this file.
If this doesn't make sense read Permissions a few posts back on this thread.

http://home.comcast.net/~linuxboy/su2.jpg

So I need root permissions to view this file...
There are many ways to handle this, but I'm going to demonstrate 2.
sudo this is a good way to substitute the user on a command by command basis
The user is based on the sudoers file (the very one we're trying to open),
but root is the default.

sudo more sudoers
It will ask for the root password, once supplied it will execute as root and you will remain as your normal user.

http://home.comcast.net/~linuxboy/su3.jpg

Now I'm kind of old school, and I have always used the su command.
The su command allows you to become any (substitute user) and remain that
user until your finished.
For example:
su root allows you to become root after the root password is supplied.
You are now root and can do anything within the system, BE CAREFUL!!!

http://home.comcast.net/~linuxboy/su4.jpg

NOTE:
There is no protection from root messing the system up
so proceed with caution as the root user.

Let's say you finished being root and want to return as your normal user...
just type exit in the console and you will exit root and be the normal user again.
If you get confused as to who you are because of su,
just type whoami and it tells you who you are logged in as.
For networking purposes, who shows you everyone who is logged into the system.

We need to start combining commands soon, and using pipes.
Once you get used to piping, you'll understand how linux can
be much easier to use than Windows (Ha! Ha! Ha!).

Any question so far??
I'm not very good at explaining things.

wdhpr 27. May 2009 02:09 AM

Thanks debtboy
I think you have done a great job on this thread. I have read it carefully and Learned some really cool and powerful stuff. As you also caution, one MUST be careful using it

again thanks

Wdhpr

debtboy 28. May 2009 01:14 AM

wget grep pipe
 
OK Everyone,

Before we get into the pipe...
Here are a few more standard commands you might already be using:
cp (copy)
rm (remove)
mkdir (make directory)
rmdir (remove directory)
mv (move)
find (find pattern)
sort (sort)
Look them up in the man pages.

Now lets look at wget (web get)
wget allows you to extract files/webpages from the internet,
and save them locally. It will recursively extract the hierachy of
files and directories in a domain name and also follow links.
It does respect the robots.txt file.

Now I know what your thinking and yes you could put this command into a
subnet loop and your 3/4's of the way through creating a robot or spider,
but that is not what it was designed for so it's not very fast.

I'm going to extract the default page of http://techsupportalert.com
from my Linux command line and for purposes of demonstration, I'm going
to force the output into this terminal instead of saving the file.
Now here is the command:
wget -qO - "http://techsupportalert.com"
The options I'm using are quite mode and file output to - the terminal
This is the very same command with the options/switches separated:
wget -q -O - "http://techsupportalert.com"

http://home.comcast.net/~linuxboy/wget1.jpg

Now if you've been following this thread, yes I changed my background.
It's one of those free ones from Digital Blasphemy.

Now here is some of the output from that command, it's really about 13 pages long.
http://home.comcast.net/~linuxboy/wget2.jpg

On to grep, I use this command quite often to extract matching patterns within files and I also use it to count occurrences within the input.

Here I'm going to pipe the output of wget directly into the input of
grep using the pipe character (it looks like 2 hyphens turned vertical on the keyboard).
grep will extract all lines with the pattern <title>

wget -qO - "http://techsupportalert.com" | grep -e "<title>"

http://home.comcast.net/~linuxboy/wget3.jpg

Here is the result of that piped command:

http://home.comcast.net/~linuxboy/wget4.jpg

This was only a single pipe, but you can have multiple pipes connecting
many outputs to inputs which is called what else... a pipeline.

I know what your thinking, you could create a bot looping through subnets
extracting anything with an "@" for email addresses. Yes it's possible,
but we've decided to use what we've learned for good and not evil!!

Hope this post wasn't too confusing...
Are you begining to see the power of the command line yet??
We haven't even created a script or even mentioned PERL.


All times are GMT +1. The time now is 08:38 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.