chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial Getting familiar with the linux command line [Part 2: command line basics] filter_list
Author
Message
Getting familiar with the linux command line [Part 2: command line basics] #1
Okay, so everybody (who's opinion matters) seems to want to see more of these. It would be nice if we could get a subforum on here just for teaching members about the Linux kernel and its accompanying software distributions, there really is a lot here. Maybe @Oni can consider that in his next strain of additions.



So, part 2: command line basics. I will keep this one brief as well, I don't want to flood you guys with information that you wont remember. Each tutorial will include a set of commands (and information) that you will be able to use independently to play around your linux system.

Going back to part 1, these are the ones you should already know and have played around with:
  • mount
  • umount
  • cd
  • ls
  • lsblk
  • rm
  • mkdir

So, you should know how to view file names and trees, navigate the filesystem, remove files, and create directories.

What if I want to see what is in a file?
This is one I accidentally left out of part 1, but it will fit just fine in part 2. There are multiple ways to view a file, depending on what type of file it is. This little part will contain three commands.
Well, firstly, we need to know what type of file we are dealing with. Have you ever opened an MP3 in notepad? It doesn't look very nice, but a text file looks fine in notepad. That is the difference between binary files and text files. Obviously both types of files can have their own types, not all binary files work the same way, that all has to do with the way they were saved on disk. We won't get into that one.
To see the type of a file, we use the file command (pretty simple).
Code:
file - view information about the file
Usage:
file <path to file>
Here are a couple different outputs, you will notice a pattern here.
binary file (executable):
Code:
(precise)phyrrus9@localhost:~/dns$ file db
db: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x44561c24b050d4c41c52e453b47017d5415686fd, not stripped
Now, notice there is a LOT of information here, if anybody wants to really break this down *cough*@roger_smith* that would make a great tutorial in the future, but all that is important is the "ELF" there. ELF is the linux executable type (Windows uses PE).
Text file:
Code:
(precise)phyrrus9@localhost:~/dns$ file dns.c
dns.c: ASCII C program text
(precise)phyrrus9@localhost:~/dns$ file A.txt
A.txt: data
I included a couple of them so that you can see what different types of information it can give you. After a while you will learn what types of files are binary and which are text without even using file.

So, we want to view these files. For binary data we will want to use a command called hexdump, and for the text files we use a program called cat
Code:
hexdump - display file bytes as hex numerals
Usage:
hexdump <path to file>
OR (what I prefer)
hexdump -C <path to file>
The -C option ("tac C") tells hexdump to also display ASCII data on the right side. I will hexdump A.txt for you (since it is a small file, and not actually a text file) using both methods:
Code:
(precise)phyrrus9@localhost:~/dns$ hexdump A.txt
0000000 c008 02a8 7405 7365 2e74 6577 0062    
000000d
(precise)phyrrus9@localhost:~/dns$ hexdump -C A.txt
00000000  08 c0 a8 02 05 74 65 73  74 2e 77 65 62           |.....test.web|
0000000d
If you are not familiar with ASCII coding, you should go take a look at the table:
Spoiler:
[Image: asciifull.gif]
Next, we have cat. I will cat the dns.c file for you (screenshot)
Code:
cat - View file contents
Usage:
cat <file path>
Code:
(precise)phyrrus9@localhost:~/dns$ cat dns.c
Spoiler:
[Image: fvIyw9R.png]



Now, the problem we saw with the cat was that we didn't get to see the full file. Just the end of it. What if we didn't want it to just scroll past us? This is where the less command comes into play.
Code:
less - view one screen's worth of output at a time
Usage:
<command> | less (most common)
OR
less <file>
Now it is time to teach you a new little thing. When we run a command there are at least four files that get opened.
1. The program itself
2. The output for the program (stdout)
3. The input for the program (stdin)
4. The error output (stderr)

All of the commands I have introduced to you so far are not actually writing the output to the console, but they are writing to stdout. stdout just simply happens to be the console, but it doesn't have to be. Programs like less can take their data on stdin (their input, usually the console). So, if we wanted to be able to scroll the output of one program, we attach the output of that program to the input of less. We do this with a pipe ( | ). the syntax looks like this
Code:
<output of this program> | <is input of this one>
So, try this:
Code:
precise)phyrrus9@localhost:~/dns$ cat dns.c | less
Spoiler:
[Image: oUs4M9A.png]
Now we can use the arrow keys to look around the file, and the q key to go back to our terminal.

This works on ALL commands, but is only useful if they read from stdin.



Now, redirection. This is the same thing as the pipe, but for files instead of programs. There are two types of redirection: truncation and append. They use the greater than symbol ( > ) in either one (truncate) or two (append).
It works by taking the stdout of a program and saving it to a file on the disk. Examples:

Code:
cat dns.c > dns.c.copy
that will copy a text file (same as cp, but don't hold that for truth on all files)

or:
Code:
<myprogram> >> log.txt
will add whatever myprogram outputs to log.txt

This is best used with the echo command:
Code:
echo - echo (really, you should know what an echo is)
Usage:
echo <stuff to echo (in quotes)>

example: create a file named hello with the contents "hello world"
Code:
echo "hello world" > hello
Pretty neat huh?



Recap: here is what you should have learned from this tutorial:
  • file
  • hexdump
  • cat
  • less
  • echo
  • pipe ( | )
  • truncate redirect ( > )
  • append redirect ( >> )

and what you should know at this point:
  • mount
  • umount
  • cd
  • ls
  • lsblk
  • rm
  • mkdir
  • file
  • hexdump
  • cat
  • less
  • echo
  • pipe ( | )
  • truncate redirect ( > )
  • append redirect ( >> )



Go ahead and play around before the next tutorial!

Reply

RE: Getting familiar with the linux command line [Part 2: command line basics] #2
I'm surprised after x amount of views, there's no replies.

Evidently, a lot of thought and preparation has been generated to formulate this thread. Given I'm predominantly on the Windows platform, both this and the first part of your tutorial, has refreshed my memory with Linux.

Well done.
[Image: AD83g1A.png]

Reply

RE: Getting familiar with the linux command line [Part 2: command line basics] #3
(06-21-2015, 03:21 AM)mothered Wrote: I'm surprised after x amount of views, there's no replies.

Evidently, a lot of thought and preparation has been generated to formulate this thread. Given I'm predominantly on the Windows platform, both this and the first part of your tutorial, has refreshed my memory with Linux.

Well done.

It seems all of the hardcore tutorial threads on this forum go unheard. The ones that are 5 minute quickies and dont explain much are the ones getting all the attention. I still post mine, eventually the members here will want to read them.

[+] 1 user Likes phyrrus9's post
Reply






Users browsing this thread: 1 Guest(s)