As you've noticed, the vertical line | is very important. That's what connects all the little tools together. It acts to pass the result of the first command to the second program and that's all we need to know.
wc
find some random file. run:
cat file | wc
The wc shows you how many lines, words and bytes are there in a file. Pretty simple huh. But careful, from now on, you should be able to differ | from > or <. > creates a file and put the content from the left into that file. And | actually passes the content of a file to another program.
Try cat file > wc. You just made another copy of file. cat wc, you'll see.
ls
some people go wild with ls. I'm not really a big fan. But this is a very useful command. (Of course, at least I guess you want to know what's in your computer.)
ls shows the files / directories at a certain directory. And cd something lets you open that folder if you didn't already know. cd .. let's you go up a level.
You can vastly start using tab now, it auto-completes for you.
ls -a shows normal files and the hidden files.
ls -l detailed information.
and ls supports some wildcards.
Now, run our favorite command, seq 1 100 | xargs -i touch {}.txt
Now: ls *.txt | xargs -i cp {} {}.bak .
You've just made backups of each of your files.
But the file extension is kind of long and annoying, so instead of that, run
ls *.txt | tr -d '.txt' | xargs -i cp {}.txt {}.bak .
That backs up x.txt to x.bak
tr means translate. But here -d deletes what's given. I don't know that's in a parameter form because the command getting rid of some thing would be useful per se.
tr is simple and useful, but we'll save it for a later time.
To view a file, you actually have more control with less and more.
Run seq 1 100 > 1.txt
then cat 1.txt.
You only see the last few lines because cat is not interactive.
But if you use less 1.txt or more 1.txt, then you can choose where to go.
Use d and b to scroll down and back with both less and more. Use q to exit.
That's as much I use less or more for, and less doesn't read the whole file at startup so it is designed to start up much faster compared to vi. vi is cultural in Linux but unfortunately I was educated to use Emacs which is much more wrist twisting. So I'll try to invite my co-author, who was also my TA for my CS course to introduce you his favorite text editor, vi. And I'll fight back with Emacs.
So, sidetrack:
Introduction to THE text editors in Linux, vi , Emacs.
Here are some other short commands that will be very handy but doesn't require much of an explanation:
sort : sort
uniq : filter out duplicates
top : system process monitor
bc : powerful calculator
pwd : current directory
cal: a little calendar
Another tool that is very useful is man
Just try these yourselves and if there's confusion, I'll be adding stuff here. There's another little useful tool wget we'll be using. But you'll see it in tutorial 3 anyway.
It's been fun. Continue the BASH adventure. 3. Finding and twisting text massively, tr, grep, sed.
PS: The majority of the tutorial requires some participation too. Please come up with tasks or improvements to the commands.
For example, my back up command here:
ls *.txt | tr -d '.txt' | xargs -i cp {}.txt {}.bak . could be "improved" if we wanted the date to be included in the back up file's name too. The command won't be that simple and it's harder to do if we wanted a one liner (one command basically) and tell me if you have smarter ones. And this one, it seems I cannot do with a one liner... I have to first save date to a variable, then just change .bak to .bak.date...
So you can comment some questions like that too and we'll really have some "tasks" to do to actually expand the material.
Here are some commands I haven't been able to figure out.
No comments:
Post a Comment