Difference between revisions of "Advanced Command Line"
(36 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Delete}} | |
− | + | ||
− | + | This class is not currently offered. | |
− | + | == Introduction and Prerequsites == | |
− | * | + | |
− | + | This class is intended to be an extension of the [[Basic Linux Command Line for Builders]] class. It will review how to read a command and then explain the various option types. Up to this point we haven't done much. We've learned about ls and looked around, but we need to learn some commands that help with file and directory operations. We'll then learn about BASH and some of it's many features. Lastly if there is time we'll learn about I/O redirection and process management. | |
− | ** | + | |
− | + | The instructor will expect that the students understand basic command line syntax as well as the advanced techniques of tab-completion and history usage (up arrow), both of which are taught in the basic command line class. It's OK if you don't remember everything, but the more you can remember the more new stuff we'll be able to discuss. | |
− | + | ||
− | + | == Deconstructing a Command == | |
− | + | ||
− | + | Let's break down a command (a fairly complicated one) and see what it's doing and how to read it. The command we'll deconstruct is the following, | |
− | + | ||
− | + | diskless243@gimpy:~$ ls -lQ --all --sort=size -r /usr/share/doc/bash/READ* | |
− | + | ||
− | + | First let's examine what each element in this command is. | |
− | + | ||
− | + | ; ls : This is the command you're going to run, it is an executable program which exists on your system (i.e. you can find the ls command, try 'which ls'. | |
− | ** '>' | + | ; -lQ : These are two short-hand options; the -l and the -Q options. These options list using the long format and put quotation marks around the file names. |
− | + | ; --all : This is a long-hand option that lists all of the contents of a directory, including hidden files (files that begin with a period. | |
− | ** | + | ; --sort=size : This is a long-hand option that takes an argument. In other words, we're telling the ls command to sort the output and specifically we're telling it to sort according to the file's sizes. |
− | * | + | ; -r : This is another short-hand option that could have been combined with the other short-hand options, but it wasn't. It tells the ls command to list in reverse. |
− | ** grave accent | + | ; /usr/share/doc/bash/READ* : This is the path which we wish to list. Note the '*' at the end; it's the wildcard character. It instructs ls to list only files within the /usr/share/doc/bash/ directory that begin with "READ" and have zero or more characters following it. This means that these are all valid matches for the wildcard; "READ", "READd", "READ182", "READABC123" and "READihsfkahsldkjhfhdsflkahsdlkhfalhdsflkajhslk3246367". |
+ | |||
+ | So, now that we know what each of the elements are how would we read this command? We'll you'd read it in a very similar way to the way that you'd read a sentence in the English language. The subject of our sentence is the computer, because it's always the thing which is taking the action. The verb in the sentence is the command. We're asking the computer to do something and the command is what we're asking it to do. The options are the adverbs, they modify the verb, similarly to if I said, "I slowly walked to the store", "slowly" is the adverb which modifies the verb. I didn't say that I walked to the store, I said that I slowly walked to the store. Lastly, the object to which we're acting upon is the argument or the target of the command. | ||
+ | |||
+ | == Examining Files == | ||
+ | In Linux there is no enforcement of file type extensions.In other words, if a file is named foo.txt there is no guarantee that the file is a text file as the extension .txt would imply. Generally it is a good idea to give files appropriate names and sometimes that means using .xxx at the end of the file name, but it's not required. This means that if you aren't familiar with a file (i.e. you didn't create it or haven't used it before) you need a way to figure out what it is. | ||
+ | |||
+ | ; file : This command is used by passing the command file a file which you're curious about. For example, "file foo.txt" would tell you what foo.txt is. | ||
+ | |||
+ | If you've determined that you have a plain text file and you'd like to see it's contents you can cat it. | ||
+ | |||
+ | ; cat : This command is used to examine the contents of one or more files. For example, "cat file1 file2 file3". This will just dump the contents of file1, file2 and file3 to the screen, in that order. | ||
+ | |||
+ | == Copying, Creating, Moving and Removing == | ||
+ | Now that you know how to identify files let's look at how to manipulate these files within the file system. WE'll also look at how to manipulate directories (a.k.a folders). | ||
+ | |||
+ | === Files === | ||
+ | ; touch : This command will update the time stamp on a file. If the file name specified doesn't exist it will create a new file. | ||
+ | ; rm : This command deletes one or more file(s) specified. | ||
+ | ; cp : This command copies files from one location in the file system to another. Since this is a copy the original file still exists in its original location after the operation. | ||
+ | ; mv : This command moves files from one location in the file system to another. Since this is a move the original file will not exist in its original location after the operation. | ||
+ | |||
+ | === Directories === | ||
+ | Folder manipulation can be a bit confusing. The main confusion usually occurs when performing an action on a non-empty directory. When there are contents in a directory an option-less cp or rm will fail. To successfully delete or copy a folder with contents you need the '-r' option. The -r option tells the commands to act [http://en.wikipedia.org/wiki/Recursion recursively], in other words to act on the folder and all of its contents. | ||
+ | |||
+ | ; mkdir : This command create a directory. | ||
+ | ; rmdir : This command deletes any empty directory. To delete a directory with contents run "rm -r <foldername>" | ||
+ | ; cp -r : This command copies a directory and its contents from one location to another. | ||
+ | ; mv : This command moves a file or a directory to a new location. | ||
+ | |||
+ | |||
+ | {| border = "1" | ||
+ | |- | ||
+ | ! Operation | ||
+ | ! Files | ||
+ | ! Directories | ||
+ | |- | ||
+ | | Create | ||
+ | | touch | ||
+ | | mkdir | ||
+ | |- | ||
+ | | Remove | ||
+ | | rm | ||
+ | | rmdir or rm -r | ||
+ | |- | ||
+ | | Copy | ||
+ | | cp | ||
+ | | cp -r | ||
+ | |- | ||
+ | | Move | ||
+ | | mv | ||
+ | | mv | ||
+ | |} | ||
+ | |||
+ | == BASH == | ||
+ | === What is BASH? === | ||
+ | The environment that you've been in throughout the basic class and now the advanced class is called BASH. From http://www.gnu.org/software/bash/, "Bash is the shell, or command language interpreter..." This just means that BASH is what understands the commands that you can use and executes them for you when you hit the "Enter" key. BASH is much more than just that though. In fact it's a programming language, so whether you've known it or not you've been doing some programming already. | ||
+ | |||
+ | === Environment Variables === | ||
+ | A common tool in programming languages is the use of variables- using system memory to store some information. Some environment variables are defined by the system and used to configure certain aspects of your command line experience. For example, PS1 defines what the command prompt looks like. | ||
+ | |||
+ | Substitute <Your Name> with your name. | ||
+ | |||
+ | diskless243@gimpy:~$ FOO=<Your Name> | ||
+ | diskless243@gimpy:~$ echo FOO | ||
+ | |||
+ | You can see that the echo command just repeats whatever you put after it. Not very interesting, huh? Now try this, | ||
+ | |||
+ | diskless243@gimpy:~$ echo $FOO | ||
+ | |||
+ | Now you should see your name printed to the screen. You've now accessed the contents of the variable and not the variable name itself. | ||
+ | |||
+ | You can define environment variables with any name that you'd like. But I'd recommend trying to echo the command before redefining it. For example, if you wanted to use the variable name USER you should try and echo it first. | ||
+ | |||
+ | diskless243@gimpy:~$ echo $USER | ||
+ | |||
+ | doing so produces some output. You can see that variable is already in use. Don't use it! How about BAR? | ||
+ | |||
+ | diskless243@gimpy:~$ echo $BAR | ||
+ | |||
+ | No output? Good, you can use BAR safely. | ||
+ | |||
+ | == PATH == | ||
+ | |||
+ | A path in Linux is a textual representation of a location on your file system. It tells you how to get to a folder. Here are some examples of paths. | ||
+ | |||
+ | / | ||
+ | /usr/local/bin/ | ||
+ | /etc/init.d/ | ||
+ | /lib/modules/2.6.18-5-686/kernel/drivers/scsi/ | ||
+ | ../ | ||
+ | ../../home/ | ||
+ | |||
+ | Your system uses an environment variable called PATH as a global search path for executable programs. Type the following, | ||
+ | |||
+ | diskless243@gimpy:~$ export | grep PATH | ||
+ | |||
+ | This shows you the definition of the PATH environment variable- it's a colon separated list of paths. | ||
+ | |||
+ | When you run the 'ls' command how does BASH know where that program is? It isn't in the directory you're in right now, so how does it know? Well, it uses the PATH. It looks for an executable file named 'ls' in the first path in the PATH. If it finds 'ls', it runs that program. If it doesn't find 'ls' in the first path, it moves on to the second path in the PATH. It will continue to do so until it finds 'ls' or not. If it finds 'ls' it executes it, if it doesn't find 'ls' it will print an error on the command line, that looks like this. | ||
+ | |||
+ | diskless241@gimpy:~$ ls-l | ||
+ | bash: ls-l: command not found | ||
+ | |||
+ | When you do not type anything on the command line and then hit <TAB> twice the system will list ~1802 commands. This list contains all of the possible commands that the user can run. To be precise, the list contains all of the executable files found in the PATH. | ||
+ | |||
+ | Also note that if there happen to be two executable files with the same name in the PATH, BASH will execute the first one that it finds. | ||
+ | |||
+ | == I/O Redirection == | ||
+ | * ls /media /foo 1> out.txt 2> err.txt | ||
+ | * '>' | ||
+ | * '>>' | ||
+ | * tee | ||
+ | |||
+ | == Process Management == | ||
+ | * ps (only user's processes) | ||
+ | * ps -axf (all processes) | ||
+ | * xeyes | ||
+ | * suspend | ||
+ | * bg and fg | ||
+ | * kill | ||
+ | * jobs | ||
+ | |||
+ | == Extras == | ||
+ | * grave accent | ||
+ | |||
+ | |||
+ | [[category:Classes]] | ||
+ | [[category:command_line]] |
Latest revision as of 14:15, 14 May 2013
This page has been requested to be deleted. |
This class is not currently offered.
Introduction and Prerequsites
This class is intended to be an extension of the Basic Linux Command Line for Builders class. It will review how to read a command and then explain the various option types. Up to this point we haven't done much. We've learned about ls and looked around, but we need to learn some commands that help with file and directory operations. We'll then learn about BASH and some of it's many features. Lastly if there is time we'll learn about I/O redirection and process management.
The instructor will expect that the students understand basic command line syntax as well as the advanced techniques of tab-completion and history usage (up arrow), both of which are taught in the basic command line class. It's OK if you don't remember everything, but the more you can remember the more new stuff we'll be able to discuss.
Deconstructing a Command
Let's break down a command (a fairly complicated one) and see what it's doing and how to read it. The command we'll deconstruct is the following,
diskless243@gimpy:~$ ls -lQ --all --sort=size -r /usr/share/doc/bash/READ*
First let's examine what each element in this command is.
- ls
- This is the command you're going to run, it is an executable program which exists on your system (i.e. you can find the ls command, try 'which ls'.
- -lQ
- These are two short-hand options; the -l and the -Q options. These options list using the long format and put quotation marks around the file names.
- --all
- This is a long-hand option that lists all of the contents of a directory, including hidden files (files that begin with a period.
- --sort=size
- This is a long-hand option that takes an argument. In other words, we're telling the ls command to sort the output and specifically we're telling it to sort according to the file's sizes.
- -r
- This is another short-hand option that could have been combined with the other short-hand options, but it wasn't. It tells the ls command to list in reverse.
- /usr/share/doc/bash/READ*
- This is the path which we wish to list. Note the '*' at the end; it's the wildcard character. It instructs ls to list only files within the /usr/share/doc/bash/ directory that begin with "READ" and have zero or more characters following it. This means that these are all valid matches for the wildcard; "READ", "READd", "READ182", "READABC123" and "READihsfkahsldkjhfhdsflkahsdlkhfalhdsflkajhslk3246367".
So, now that we know what each of the elements are how would we read this command? We'll you'd read it in a very similar way to the way that you'd read a sentence in the English language. The subject of our sentence is the computer, because it's always the thing which is taking the action. The verb in the sentence is the command. We're asking the computer to do something and the command is what we're asking it to do. The options are the adverbs, they modify the verb, similarly to if I said, "I slowly walked to the store", "slowly" is the adverb which modifies the verb. I didn't say that I walked to the store, I said that I slowly walked to the store. Lastly, the object to which we're acting upon is the argument or the target of the command.
Examining Files
In Linux there is no enforcement of file type extensions.In other words, if a file is named foo.txt there is no guarantee that the file is a text file as the extension .txt would imply. Generally it is a good idea to give files appropriate names and sometimes that means using .xxx at the end of the file name, but it's not required. This means that if you aren't familiar with a file (i.e. you didn't create it or haven't used it before) you need a way to figure out what it is.
- file
- This command is used by passing the command file a file which you're curious about. For example, "file foo.txt" would tell you what foo.txt is.
If you've determined that you have a plain text file and you'd like to see it's contents you can cat it.
- cat
- This command is used to examine the contents of one or more files. For example, "cat file1 file2 file3". This will just dump the contents of file1, file2 and file3 to the screen, in that order.
Copying, Creating, Moving and Removing
Now that you know how to identify files let's look at how to manipulate these files within the file system. WE'll also look at how to manipulate directories (a.k.a folders).
Files
- touch
- This command will update the time stamp on a file. If the file name specified doesn't exist it will create a new file.
- rm
- This command deletes one or more file(s) specified.
- cp
- This command copies files from one location in the file system to another. Since this is a copy the original file still exists in its original location after the operation.
- mv
- This command moves files from one location in the file system to another. Since this is a move the original file will not exist in its original location after the operation.
Directories
Folder manipulation can be a bit confusing. The main confusion usually occurs when performing an action on a non-empty directory. When there are contents in a directory an option-less cp or rm will fail. To successfully delete or copy a folder with contents you need the '-r' option. The -r option tells the commands to act recursively, in other words to act on the folder and all of its contents.
- mkdir
- This command create a directory.
- rmdir
- This command deletes any empty directory. To delete a directory with contents run "rm -r <foldername>"
- cp -r
- This command copies a directory and its contents from one location to another.
- mv
- This command moves a file or a directory to a new location.
Operation | Files | Directories |
---|---|---|
Create | touch | mkdir |
Remove | rm | rmdir or rm -r |
Copy | cp | cp -r |
Move | mv | mv |
BASH
What is BASH?
The environment that you've been in throughout the basic class and now the advanced class is called BASH. From http://www.gnu.org/software/bash/, "Bash is the shell, or command language interpreter..." This just means that BASH is what understands the commands that you can use and executes them for you when you hit the "Enter" key. BASH is much more than just that though. In fact it's a programming language, so whether you've known it or not you've been doing some programming already.
Environment Variables
A common tool in programming languages is the use of variables- using system memory to store some information. Some environment variables are defined by the system and used to configure certain aspects of your command line experience. For example, PS1 defines what the command prompt looks like.
Substitute <Your Name> with your name.
diskless243@gimpy:~$ FOO=<Your Name> diskless243@gimpy:~$ echo FOO
You can see that the echo command just repeats whatever you put after it. Not very interesting, huh? Now try this,
diskless243@gimpy:~$ echo $FOO
Now you should see your name printed to the screen. You've now accessed the contents of the variable and not the variable name itself.
You can define environment variables with any name that you'd like. But I'd recommend trying to echo the command before redefining it. For example, if you wanted to use the variable name USER you should try and echo it first.
diskless243@gimpy:~$ echo $USER
doing so produces some output. You can see that variable is already in use. Don't use it! How about BAR?
diskless243@gimpy:~$ echo $BAR
No output? Good, you can use BAR safely.
PATH
A path in Linux is a textual representation of a location on your file system. It tells you how to get to a folder. Here are some examples of paths.
/ /usr/local/bin/ /etc/init.d/ /lib/modules/2.6.18-5-686/kernel/drivers/scsi/ ../ ../../home/
Your system uses an environment variable called PATH as a global search path for executable programs. Type the following,
diskless243@gimpy:~$ export | grep PATH
This shows you the definition of the PATH environment variable- it's a colon separated list of paths.
When you run the 'ls' command how does BASH know where that program is? It isn't in the directory you're in right now, so how does it know? Well, it uses the PATH. It looks for an executable file named 'ls' in the first path in the PATH. If it finds 'ls', it runs that program. If it doesn't find 'ls' in the first path, it moves on to the second path in the PATH. It will continue to do so until it finds 'ls' or not. If it finds 'ls' it executes it, if it doesn't find 'ls' it will print an error on the command line, that looks like this.
diskless241@gimpy:~$ ls-l bash: ls-l: command not found
When you do not type anything on the command line and then hit <TAB> twice the system will list ~1802 commands. This list contains all of the possible commands that the user can run. To be precise, the list contains all of the executable files found in the PATH.
Also note that if there happen to be two executable files with the same name in the PATH, BASH will execute the first one that it finds.
I/O Redirection
- ls /media /foo 1> out.txt 2> err.txt
- '>'
- '>>'
- tee
Process Management
- ps (only user's processes)
- ps -axf (all processes)
- xeyes
- suspend
- bg and fg
- kill
- jobs
Extras
- grave accent