- MCP server with stdio transport for local use - Search episodes, transcripts, hosts, and series - 4,511 episodes with metadata and transcripts - Data loader with in-memory JSON storage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
314 lines
28 KiB
Plaintext
314 lines
28 KiB
Plaintext
Episode: 598
|
|
Title: HPR0598: Bash Scripting: Episode 2 Command Line Basics
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr0598/hpr0598.mp3
|
|
Transcribed: 2025-10-07 23:42:45
|
|
|
|
---
|
|
|
|
Hello ladies and gentlemen my name is Ken Fallon and this is the second and the
|
|
episode on Bash scripting. Today's episode is entitled Common Bash Commands.
|
|
Hi everybody. In the last episode we covered a basic introduction to the Bash shell and I
|
|
showed you how to open the terminal. We even went so far as to write in a Hello World
|
|
script which I hope you all have done and so today we're going to move on. We are going
|
|
to be doing one script but we're going to be going through some of the most common commands
|
|
that we're going to be using later on in some of our scripts and even if you're not interested
|
|
in Bash scripting today's episode we'll give you a basic introduction to the Linux command
|
|
line. One of the most daunting things I've ever done in computing was logging into my
|
|
brothers. I think it was a 8 to 8 6 PC, one of the original PCs and I got an about a board
|
|
to try ignore her for hours and actually it was the tour of having thought that I had
|
|
sent off nuclear missiles somewhere in the world. The word abort I always heard in movies.
|
|
It was actually that that made me want to change from doing mechanical engineering which I was
|
|
studying at the time to get interested in the computing field so there you go if it wasn't for
|
|
a tour of the command line you wouldn't actually be listening to this right now.
|
|
I'll never back to today's lessons. One of the tips that you can do when you do arrive at a
|
|
any system that is prompting you with a black and white sort of terminal. It can be a bit daunting
|
|
so but you have to realize that the programmers of this system have left you some sort of
|
|
bread come trail in order to find your way around. The most useful thing that you can probably do
|
|
and probably the safest thing to do is either type question mark which in the case of Bash won't
|
|
help you very much or you can type HLP the help command. In most systems proprietary or
|
|
open source or whatever if you type help you will get some sort of help indication
|
|
and in Bash when you type help you'll get a list of commands that are available in Bash
|
|
and typing help then the name of any of the commands listed will give you more information about
|
|
that command so typing help space help for instance gives you detailed information about
|
|
the help command which is probably not very useful to you at this stage. You can also get more
|
|
detailed information about a command by using the man command. Now man stands for manual as in
|
|
it's a reference manual. It's an electric format, electronic format of the book that should
|
|
be shipped with the program and just in case you forget the fifth line of the output from the help
|
|
command which we typed earlier makes reference to the man command and the info command. Now we're
|
|
not going to be dealing with the info command a lot because it's although it's arguably a better
|
|
format than man it's not as widely used on other systems, non-genew systems.
|
|
You can also use the command apropole which is actually a shortcut for man space dash k which is
|
|
listed on the fifth line of the help command which we've just typed earlier on and if you type
|
|
apropole followed by a word that you're interested in you're going to get a list of all the manual
|
|
pages that have that word listed in the short snobsis. So if you type man space or sorry apropole
|
|
space help or indeed man space dash k space help you're going to get a list of not very useful
|
|
commands actually to be honest with you that are that have the word help in their title.
|
|
Now the reason these man pages can be a bit daunting if this is the first time that you've
|
|
ever come across them and they're not actually intended for new users of bash for instance or new
|
|
unique Linux users. They're intended for experienced admins who've either forgotten something or
|
|
experienced admins who've come in from another system or want to use the term admin I mean users.
|
|
That's not to say that there's not useful information here for the novice so for example if you
|
|
type man space intro it's going to give you a basic introduction to man and it's or to the
|
|
bash system itself and some various commands. So it's actually a good a good topic for homework
|
|
today is to go and run the man space info command and have a little read of that. Don't be too
|
|
worried if you don't get it all don't be too worried if there's bits that you have to skip over.
|
|
There's also documentation shipped with your distribution and it's usually stored under slash
|
|
user slash share slash docs and in our case we're interested in slash bash but these are compressed
|
|
to save space it's probably not a problem if you browse to them on in a gooey in a gooey or file
|
|
manager or whatever but on the command line you need to go and to use command z more to be able
|
|
to read these files. But let's not just one last thing about the help and that is that you can
|
|
usually also type the command name so if you find out a command for instance on a bulletin
|
|
border somewhere around a wiki or something you can type the command followed by space dash
|
|
dash help or a space dash h for short. Usually what I do is if I'm looking up something and I don't
|
|
know if I have a problem and I see a list of commands given on a wiki page or on a forum or
|
|
something what I'll do is I'll type man space and the command name to see exactly what it is
|
|
that the user is trying to do and if I'm familiar with that command I know more or less the intricacies
|
|
of it I might just type man space dash dash help. Okay now while the man intro is a good start to
|
|
learning bash the learning curve can be quite steep. Now my personal opinion is that there's
|
|
a need to have some better command line guides give button to distros which are Linux distributions.
|
|
But given the focus on the GUI and making that easy to use I very much doubt if that's ever
|
|
going to be addressed. In order to keep the distribution short they're even compressing the manual
|
|
pages which I've shown here before so I very much doubt they're going to put any additional
|
|
files on to mainstream Linux distributions. By the way I'll have a link in the show notes
|
|
to what a Linux distribution is if you think of it just as a supplier of a Linux system in the
|
|
same way Ford supplies a car and VW supplies a car or an automobile. The Linux distribution like
|
|
Ubuntu or Fedora are just assemblies of the same thing the components may be the same but
|
|
under the hood it's Linux with a lot of GNU tools hence the word GNU Linux. Learning curves
|
|
there is a learning curve before you can start getting comfortable with command line
|
|
but it's journey only need to make ones. Now while the syntax may change from time to time
|
|
and from system to system the core knowledge that you're going to pick up will remain valid
|
|
for use to come and this explains why the man pages are deliberately kept sub-turs. They're meant
|
|
as a reminder for someone already familiar with the command rather than as a first introduction.
|
|
Now as we promised earlier this is going to be an introduction to some basic Unix commands
|
|
and as you'll all know at the end by the end of this series I plan to do no actual work
|
|
so I've asked and received permission from Chess Griffin to use extracts from his podcast series
|
|
Linux reality. A Linux reality was a podcast series that ran for 100 episodes from February 2006
|
|
to March 2008 and the snippet we're about to hear was from episode 14 entitled command line
|
|
basics and it was originally aired on the 17th of May 2006 and it was as valid now as it was then.
|
|
This is Linux reality episode 14 command line basics.
|
|
So let me talk about a few basic commands to help you start getting your way around the terminal.
|
|
Open up the terminal in whatever desktop environment you want to use it doesn't you know whatever
|
|
you're using KDE or gnome or what have you just open up a terminal and you will see a prompt.
|
|
Now the prompt contains a lot of information and it usually will have I mean sometimes it will
|
|
have the name of your directory if you see a little squiggly line a tilde it's called a tilde it's
|
|
the character that's in the upper left-hand corner of your keyboard next to the number one
|
|
that's called a tilde that the tilde character stands for your home directory so
|
|
sometimes the prompts and different distributions are a little bit different but it generally
|
|
will contain where you are what directory you happen to be in and it will have a little prompt
|
|
sign a little dollar sign or something like that and and and you know depending on whether or not
|
|
you're logged in as your normal user or as as root so let's just start with some basic commands
|
|
right from where you are don't do anything else and just type the following ls that stands for
|
|
list and that is a way to list the contents the files and folders or files and directories
|
|
of the directory you're in so presumably you're in your home directory and if you were to type
|
|
ls you would see a listing of all the files and directories contained within your home
|
|
so if you were to then open up conqueror or nautilus or another file manager to your home directory
|
|
you should see the same this is the same information the same files and the same directories should
|
|
be listed now in Linux there is there are also hidden directories and hidden files and those
|
|
begin with a period and you don't normally see them when you just do a regular ls or when you
|
|
just open up conqueror and just list the contents of your home directory you often have to toggle
|
|
the view mode if you will so you can see the hidden files and hidden files often have configuration
|
|
settings and other things like that that that are accessed by programs but that you don't normally
|
|
need to access which is why they're hidden in the first place but in the terminal if you were to do
|
|
ls dash a the dash a stands for all now you would see a list of all the files in your home directory
|
|
hidden and non hidden files and directories now another way you can list files is using the dash
|
|
l option so if you were to do ls dash l you would see a listing of all your non hidden files and
|
|
directories in a sort of a long format you know with additional information this year permissions
|
|
and who the owner and group is and the date and all of that that's the dash l option
|
|
and let me stop right there let me explain something I probably should have said right off the
|
|
bat the syntax of entering commands generally speaking commands are entered in the following
|
|
syntax in the terminal you'll have the command first then you'll have a dash with any options
|
|
that are available and then you'll have what are called arguments or what or what it's affecting
|
|
or what you're doing so let me give you an example we just did the ls command using two different
|
|
arguments we did dash l and we did dash a now you can combine you can combine the options
|
|
that I say arguments and options we did the ls command with two options the dash l and the dash
|
|
a you can combine them so you can have ls dash l a that's two options and then the argument in this
|
|
case is going to be at a you know which directory you're executing that command on if you leave it
|
|
blank it will be the current directory but you can put in a different directory as an argument
|
|
so type in the following ls space dash l a space forward slash usr forward slash bin
|
|
and then enter so that is now doing a long listing of all the files in the usr bin directory
|
|
so those are the three components of entering the command in at that at the command prompt
|
|
not every command has options not every command requires arguments you can often find information
|
|
about commands by typing in man ma and then the command name so you can type in man space ls
|
|
the man stands for manual or manual page Linux contains documentation about all the commands
|
|
they're called the man pages and I think there's a man page for almost everything and so you could
|
|
just type in man and then space and the name of the command and you'll see the syntax and some
|
|
examples and some information some and some help on how to use a particular command okay so that's
|
|
the ls the list command the next command I'll mention is cd and that stands for change directory
|
|
it's a way to jump around to different directories so you're in your home directory and if you were
|
|
to type cd space slash usr slash bin you would jump directly to the usr bin directory
|
|
so so cd you can change directories if you're in a directory if you're in your home directory and
|
|
let's show you have it you have a sub directory called music and you want to go into that folder
|
|
and and if you're in your home directory you can just do cd music and it will take you into that
|
|
sub directory now if you're in your music sub directory and you want to go back up a level
|
|
to your home directory you can do cd space and then two periods or cd space tilde takes you
|
|
directly to your home directory all right the next command I'm going to mention is the
|
|
sort of two different commands that go together one is cp for copy and one is mv for move
|
|
copy cp lets you copy files from you know from one directory or location to another directory
|
|
and mv allows you to move the file so it actually it's like a you know it just moves it so you don't
|
|
have two copies you just have one copy that's been moved from one location to another location
|
|
the move command mv is also used to rename files so if you're in your home directory and you have
|
|
a file called test and you want to rename it to test backup you would just type mv test
|
|
space test backup and it would just move move it from from test to test backup
|
|
there's the command rm for remove it's almost like a delete but now you got to be careful
|
|
anything that's that's removed with the remove command cannot be undone then if another command
|
|
sort of a basic little command here is mkdir or make directory this is how you can create
|
|
directories if you're in your home folder and you want to create a directory called music just type
|
|
mkdir space music and that will create a directory okay a couple more commands here and
|
|
less the command less less lets you print out a file a say a text file to the screen you can
|
|
just basically you know print the commands or or excuse me print the contents of the file onto
|
|
the screen so if you have a text file called file.txt and you just want to look at it just type
|
|
less space file.txt and it will just it will show you the contents you can just page up or down to
|
|
scroll and then queue for to quit back out. A more well you can you can combine commands using
|
|
what's called the pipe character the pipe character is above your enter key on your keyboard
|
|
I think it's usually a shifted backslash key but it's a straight line you know a straight up
|
|
and down line and and this takes two come it basically takes two commands and pipes them
|
|
together and connect them together if you will so here's an example let's say you were doing
|
|
ls-la okay that's that command again to list everything you know in your home directory
|
|
space or so ls space dash l a space pipe space less what that will do is it will print
|
|
the contents of your home directory it will pipe it through into the less command which as you
|
|
remember what that does is it takes whatever the contents are and prints it on your screen
|
|
and allows you to do page up and page down normally if you just do ls-la and you have a let's
|
|
say you have a thousand files well it will just scroll past really fast and you may not be able to
|
|
see it all even if you have you know you're you're even if your terminal has the scroll bar
|
|
well if you enter in this command it will let you it'll basically pause you know let's you look
|
|
at one page at a time and you can just do page up per page down to go back and forth and then quit
|
|
to go out but that pipe character character combined the two commands they combined the ls-la
|
|
with less and it put them together into one output you can also redirect contents of a command
|
|
to something else using the the greater than character which is a shifted period that's the
|
|
little triangle that's that's pointing to the right so for example if you did ls-la
|
|
space greater than space list files dot txt what that does is that that that takes the output
|
|
of ls-la and instead of printing it on the screen it directs it it redirects that to a file it
|
|
creates a file called list files dot txt a text file with the contents of your home directory in
|
|
it then you can just use less and look at that text file or use some other you know text editor
|
|
to look at that text file every time you did that command the text file would be overwritten
|
|
if you wanted to append onto the end of the file so the text file just continues to grow with more
|
|
data in there you would use two greater than characters together so it would be ls-la
|
|
space greater than greater than space list files dot txt and then it would just keep upending
|
|
the data onto the end of the file it wouldn't be overwritten every single time
|
|
i do this i have a a pod catcher client that um that does it that operates as a script in the
|
|
command line and i have it when it's done it prints the output of what it did to a text file so i
|
|
can see what it did every day and it depends it to the end so if i miss a few days i can go back
|
|
and look at that text file it doesn't get overwritten so that's an example of using of using that
|
|
that type of uh character combination okay so a couple more quick commands here the first one
|
|
is su stands for switch user allows you to switch from your regular user to the root user while
|
|
you're in the terminal you just type in su and it will ask you for the root password and the
|
|
and the prompt will also change after you after you complete that process to let you know you're now
|
|
acting as root and another little two finer final points um the terminal has what's called tab
|
|
completion so as you're as you're typing something out let's say for example you have a a long
|
|
file in your home directory called you know um this is a list of all my files dot txt and you
|
|
view the contents of that using the less command we talked about so you would type in less space
|
|
and you could start typing this is a list of all my files dot txt but that's really long it's a lot
|
|
of typing so if you were to just start typing it like this is a and then hit tab assuming there's
|
|
no other file that that has similar name it will just complete the rest of the file name it'll
|
|
just complete the rest of the file name for you it'll fill in the blanks if you have two files
|
|
that are similar like let's say you have file one dot txt and file two dot txt and you type in
|
|
f i and then you hit tab it will and you do you hit tab twice it will list both files for you
|
|
and it will fill in as much as it can and it will stop where there's a discrepancy
|
|
at the one or the two for example and let's you fill in the rest and then of course you can
|
|
keep hitting tab after you type in the one or two so tab completion of the command line is
|
|
awesome and then the last thing i'm going to mention is how to exit out you know you could type
|
|
exit exit exit exit to you know to get out of the terminal or you could close the window
|
|
but me again being a keyboard shortcut guy i just like to hit control d and that will exit out
|
|
of the command line exit out of the terminal that is so hopefully this has been helpful i know
|
|
it's not so but that is all that i've got for this week so let me close up the shop
|
|
thank you very much chess first of all thank you very much for recording that
|
|
episode but also chess releases work under creative commons attribution on commercial
|
|
no derivative's us license but he's given us permission to take snippets of his show to re-release
|
|
here on this networking episode i'm very very thankful for all the work he did and is a scholar
|
|
and a gentleman and listening to that episode just made me remember how much i really look forward
|
|
to his shows every week and naturally a link will be in the show notes now i actually got some
|
|
feedback from Jim Kerman from Virginia USA and he had amongst many other commons he had an
|
|
excellent suggestion to start each tab each week a script that we would build on
|
|
where we could use commands that we've learned so far and script we're going to use is we're
|
|
going to back up our home directory to a usb stick and as the weeks go on we're going to expand
|
|
the script we're going to build in more intelligence and we're going to deal with a lot of errors
|
|
and we're going to check for this that and the next thing and we're going to
|
|
deal with different situations that occur and this actually is how scripts tend to be built
|
|
a two-liner is put into a script and then over time it's modified and changed as things arrived
|
|
so now you all know how to open a terminal and we're going to create a directory called bin
|
|
in your home directory and the reason we're going to do this is because this is the traditional
|
|
location for user scripts and many distributions Linux distributions which we've talked about before
|
|
which are like Ubuntu or Fedora or Susa or whatever are set up to allow and tapion of course
|
|
and allow scripts in your home directory which is squiggly forward slash bin or tilde forward slash
|
|
bin the number to the left the shift left of one on a us keyboard so if you look for the one key
|
|
it's on the left hand side if you hold down the shift key yes that was a lot more complicated
|
|
than that to be anyway if you have tilde slash bin or your home directory any commands that you put
|
|
in there that are marked as executable will be included in the path so that it's one of the places
|
|
the system will look for commands okay so you're on the command line and I want you to type in some
|
|
of the following commands after listening to Chess's episode you're going to know what CD does
|
|
typing CD itself will change the current directory so if you do that you will be in the
|
|
directory slash home or you see a tilde forward slash or slash home whatever and if you do a LS
|
|
and you're going to have a look for the word BIN if you don't see it then you can type
|
|
MKDIR space BIN and what that command just going to do is make a directory called bin
|
|
now in your text editor in your GUI or using whatever text editor you've decided to use
|
|
following on from the first episode you can now create a file called episode 2.bash
|
|
you can actually call it whatever you want some people that I was asked actually what they
|
|
convention was for naming files I tend to call them .bash simply because I wanted to distinguish
|
|
them as being bash scripts as opposed to shell scripts shell scripts tended to have an extension
|
|
.sh but I've seen I've seen far I've seen scripts either not have an extension or whatever
|
|
whether it is an extension or not really just a matter as far as it goes but seeing those
|
|
we're dealing with we're talking about bash scripting why not name them .bash
|
|
okay now for the next step you're going to need a usb stick and the usb stick is going to be big
|
|
enough to be able to take in all the command all the files in your home directory so if you put
|
|
that into your into your pc and browse that you use in the GUI and your file manager you can make
|
|
a note of the path to your disk now when I do that I get a path coming up in my doll from web
|
|
browser of slash media slash disk okay now this is actually one of the things we're going to be
|
|
improving on in the later series but for right now we're going to make a very dom script that's going
|
|
to assume that that is always the the back application okay in your you're editing the file in your
|
|
home directory in the in the directory bin so your home directory till the forward slash bin
|
|
forward slash episode two dot bash and we're going to have the tic-tac toll sign exclamation mark
|
|
forward slash bin forward slash bash and we're going to type echo space double quotes this is the
|
|
backup script from episode two double quotes so the next line of our script is going to be where we
|
|
make a backup directory on the usb disk and the command we're going to use is make your space
|
|
forward slash media forward slash disk forward slash backup and the next thing we're going to do is
|
|
copy over the files from your home drive onto the usb disk but we haven't covered here how we're
|
|
going to copy multiple files and sub directories so we're going to need to look up that we did
|
|
cover in Chess's episode about the copy command but now we're going to need to type in the console man
|
|
space cp and we're looking for help on cloud to copy some directories so we need to look for references
|
|
to the word directories now quick way to search man pages is by pressing the forward slash followed
|
|
by the search term so in our case we're looking for the word directory
|
|
so if you do type forward slash d i r a c two or y in press center you're going to see the word
|
|
directory highlighted in the name section where it says copy files and directories so that looks
|
|
good we know we can copy directories at least we look we see something in the synopsis section
|
|
which it's always very confusing so it has a format which you'll get familiar with this time
|
|
goes on but for now you can probably quickly jump over the synopsis section and then we'll see
|
|
in the description section which has copy source to destination or multiple destination sources
|
|
to a directory which is exactly what we want and if you scroll down even further the next one is
|
|
um dash r r sorry dash r comma space minus r dash r lowercase space dash dash recursive so
|
|
the options here are uppercase r lowercase r or dash dash recursive all of those three will do
|
|
the same thing which is copy directories recursively and what recursively means means copy the
|
|
directory and all other files and sub directories under that directory which is exactly what we want to
|
|
do so well done you've just read a man page and you are now well on your way to understanding what's
|
|
going on so the command we're going to use is copy um actually before that normally um copy doesn't
|
|
give you any feedback so as well as reading man in for your homework for this week is to find
|
|
the option in the man page for copy that explain what has been done and that's it so you're
|
|
looking for the option to explain what is being done okay I don't think you can make that
|
|
then clear uh now back to the script under make your line in your script you can type cp
|
|
space minus r and then the other option then space uh till the backslash which is your home directory
|
|
space forward slash media for search backup uh forward slash media forward slash disk forward
|
|
slash backup and then as the last thing we're going to do is ls space a l space forward slash media
|
|
forward slash disk forward slash backup then you can exit and save the file and then what we're
|
|
going to need to do is change mod space plus x space till the forward slash bin forward slash episode
|
|
to that bash and change mod stands for change the mode of the file and that will create a very very
|
|
very rudimentary um option to backup your um home drive now board warning before you run that file
|
|
you are well advised to make sure that you have an empty usb um a disk attached
|
|
you're also advised that the disk shouldn't have a subjectry called backup in it and you also
|
|
want to make sure that it's got more disk space on it than the amount of space that you're going
|
|
to be using on your home drive now if you're relatively new to Linux you're more than likely
|
|
not going to have that much stuff on it so you can go ahead and run that and so long as the usb
|
|
disk is empty when it gets full it'll just simply stop okay however the author access to
|
|
a responsibility for this program okay um also don't worry if you can get everything
|
|
all the commands that were in the show they're all going to be included in the show notes including
|
|
the commands from chess's segment and i've also included a link to a very very excellent one page
|
|
cheat sheet which is a how to uh you know command you unix and Linux command line reference
|
|
and it's created by flosswire.com and it's released under creative commons share like
|
|
um attribution license which means i can include it in uh reference to it in the show notes and
|
|
i can even distribute it myself so what i would suggest you do is print that out
|
|
don't be too worried if you don't see um if you don't know what everything in that does but we're
|
|
going to be covering quite a lot of the sections uh quite a lot of the commands in that some of what
|
|
you'll already be familiar with you're well on your way to send my friends and um i like to thank
|
|
um everybody for all the feedback and no doubt there are going to be corrections um so if you
|
|
have corrections or commons or requests please send it to feedback at canfallon.com and tune in
|
|
for the next episode which will be on basic programming concepts. Thank you for listening to
|
|
half the public radio. HPR is sponsored by caro.net so head on over to c-a-r-o dot n-e-t for all of us
|