Editing
Linux Basics Tutorial
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Using Bash == From this point on, we will start to get into more complex Linux use. The tools listed here are powerful enough to greatly enhance your experience using Linux, or if you're not careful, you can accidentally harm your computer, perhaps even permanently! If you have any doubt as to whether a command will have any unintended consequences for your system, feel free to ask any member of RHLUG for help or advice. Bash, or the '''B'''ourne '''A'''gain '''Sh'''ell, is the standard tool for interacting with your computer through a text console. Because it uses text commands for everything, it can easily be used to specify the exact behavior you want, or even automate commands! In this tutorial, we will give console commands in the following format: <code>$ command</code> The "$" sign here indicates that this is a console command, and will likely appear as a console prompt before you type in the real command. So, if you're copying the command into your own console, you should omit the "$" and the space that comes after it. Also, we will be using the words "Bash," "console," "terminal," and "shell" interchangably. There are technical differences between these terms, but for the purposes of this tutorial, the differences aren't very major. To open a console window in Ubuntu, click the "Console" button on the sidebar, or press Ctrl+Alt+T. === The Working Directory === Navigation in Bash (and any type of command-line interface, really) is based on the concept of a "working directory," the folder you are thought to be "within" at the moment. Your working directory is often shown in the terminal prompt, before the "$" symbol. When you first start up a terminal in Ubuntu, the default for the starting working directory is "~", which is a symbol that evaluates to your "home" directory, which can be thought of as your personal user folder (more on that later). When you execute commands that take a filepath (such as <code>rm</code>, which deletes a file), those filepaths are relative to your working directory. So, if you want to delete "~/filetodelete.txt", and your working directory is "~", you can simply type: <code>$ rm filetodelete.txt</code> and Bash will know you mean the file in the ~ directory. === Navigation === The following commands and symbols are used to navigate directories: * '''pwd''' (or '''p'''rint '''w'''orking '''d'''irectory) - Prints the working directory to the console. * '''cd''' (or '''c'''hange '''d'''irectory) - Changes the working directory to the one given as an argument. If your working directory is "~", and you execute <code>$ cd myfolder</code>, your working directory will now be "~/myfolder/" (assuming that folder exists). * '''.''' - A symbol that evaluates to the working directory. You can use this to, for example, run an executable file in the current directory: <code>$ ./myprogram</code> * '''..''' - A symbol that evaluates to the parent of the working directory. If your working directory is "~/myfolder/", and you execute <code>$ cd ..</code>, your working directory will now be "~". === Other Useful Bash Commands === The following commands are used for simple operations from the command line: * '''ls''' (or '''l'''i'''s'''t): Lists (non-hidden) files and folders in the working directory, or in a directory given as an argument. It is common to use this with the arguments "l", "a", and "h" to show hidden files, show more details on the files, and print the results in a nicer format: <code>$ ls -lah</code> * '''mkdir''' (or '''m'''a'''k'''e '''dir'''ectory): Creates a new directory in the working directory, with the name given in an argument * '''rm''' (or '''r'''e'''m'''ove): Deletes a file or directory in the working directory. If you are deleting a non-empty directory, you will need to use <code>rm -rf</code> to tell it to delete the entire tree recursively, and force it to delete, even though there are files there. * '''mv''' (or '''m'''o'''v'''e): Moves a file or directory given in the first argument, to the directory given in the second argument. * '''cp''' (or '''c'''o'''p'''y): Copies a file or directory given in the first argument, to the directory given in the second argument. * '''cat''' (or con'''cat'''enate): Prints the contents of a file given in an argument, to the console. * '''echo''': Prints the fully-evaluated form of the argument given. For example, <code>$ echo ~</code> prints the fully-evaulated form of your home directory, which is likely "/home/[your username]" === Special Bash Tools === The following symbols can help make working in Bash easier: * '''!!''': Evaluates to the previous command you entered. You can also add on other text. So, for example, <code>$sudo !!</code> puts <code>sudo</code> before your previous command, and runs it. The use of <code>sudo</code> will be discussed later in this tutorial. * '''&''': Putting this at the end of a command will run that command in the background, meaning you will be able to keep using the same shell to do other commands, while the current command is still running. This is useful if you want to launch an application from the command line, but don't want to be locked out of Bash while it's running. * '''&&''': This lets you run two commands in one. For example, <code>$ echo firstcommand && echo secondcommand</code> will output the two arguments, each on its own line. * '''>''': This will take the output of a command on its left, and put it into a file specified on its right. For example, <code>$ echo hi > tmp.txt</code> will put the word "hi" into the file tmp.txt, creating it if it doesn't exist. * '''|''': This will take the output of a command on its left, run the command on the right, and feed that output into the right command's input. This is a more complicated one, but it's particularly useful for commands that look for "matches" of a pattern in some block of text, like <code>grep</code>. * '''*''': This is the "wildcard" character, acting like a keyword that matches anything. For example, <code>$ rm *</code> will remove all files from your working directory. <code>$ rm *.txt</code> will only remove files ending in ".txt".
Summary:
Please note that all contributions to RHLUG Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
RHLUG Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information