![]() |
|
Tutorial Getting familiar with the linux command line [Part 2: command line basics] - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Computers (https://sinister.ly/Forum-Computers) +--- Forum: Operating Systems (https://sinister.ly/Forum-Operating-Systems) +--- Thread: Tutorial Getting familiar with the linux command line [Part 2: command line basics] (/Thread-Tutorial-Getting-familiar-with-the-linux-command-line-Part-2-command-line-basics) |
Getting familiar with the linux command line [Part 2: command line basics] - phyrrus9 - 06-20-2015 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:
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>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 strippedText file: Code: (precise)phyrrus9@localhost:~/dns$ file dns.c
dns.c: ASCII C program text
(precise)phyrrus9@localhost:~/dns$ file A.txt
A.txt: dataSo, 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>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|
0000000dSpoiler:![]() 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.cSpoiler:![]() 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>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>Code: precise)phyrrus9@localhost:~/dns$ cat dns.c | lessSpoiler:![]() 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.copyor: Code: <myprogram> >> log.txtThis 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" > helloRecap: here is what you should have learned from this tutorial:
and what you should know at this point:
Go ahead and play around before the next tutorial! RE: Getting familiar with the linux command line [Part 2: command line basics] - mothered - 06-21-2015 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. RE: Getting familiar with the linux command line [Part 2: command line basics] - phyrrus9 - 06-21-2015 (06-21-2015, 03:21 AM)mothered Wrote: I'm surprised after x amount of views, there's no replies. 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. |