MrSigma

Untitled

Jul 27th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.62 KB | None | 0 0
  1. [b][u][size=medium][color=#ff3333]Contents:[/color][/size][/u][/b]
  2. [b][list=1]
  3. [*]What Is the Command Line?
  4. [*]List Files
  5. [*]Change Directories
  6. [*]Creating or Removing Folders
  7. [*]Creating and Removing Files
  8. [*]Edit Plain Text Files
  9. [*]Displaying Files
  10. [*]Command redirection
  11. [*]Running a Script in the Current Folder
  12. [*]Using History
  13. [*]Looping Over a Set of Files
  14. [*]Find Files
  15. [*]Find a Text String in Files
  16. [*]Batch Rename Files
  17. [*]Using Bash Shortcut Keys
  18. [*]Customizing Your Command Shell
  19. [*]Using Aliases
  20. [*]Control Your System from the Shell
  21. [/list][/b]
  22.  
  23. [b][u][size=medium][color=#ff3333]What Is the Command Line?[/color][/size][/u][/b]
  24. The command-line interface sometimes referred to as the CLI, is a tool into which you can type text commands to perform specific tasks—in contrast to the mouse's pointing and clicking on menus and buttons. Since you can directly control the computer by typing, many tasks can be performed more quickly, and some tasks can be automated with special commands that loop through and perform the same action on many files—saving you, potentially, loads of time in the process.
  25.  
  26. The application or user interface that accepts your typed responses and displays the data on the screen is called a shell, and there are many different varieties that you can choose from, but the most common these days is the Bash shell, which is the default on Linux and Mac systems in the Terminal application.
  27.  
  28. [b][u][size=medium][color=#ff3333]List Files:[/color][/size][/u][/b]
  29. First, let's display a list of files inside the active folder. For this task, you'll need to use the ls command. You can pass a number of parameters to the command to display extra details or change the sorting. For instance, if I add [color=#3399ff][b]-l[/color][/b] to the end of my ls command, I'll see a detailed listing; [color=#3399ff][b]-t will sort the results by file time; [color=#3399ff][b]-S[/color][/b] will sort by file size; and [color=#3399ff][b]-r[/color][/b] will reverse the sorting. You could use a combination of these together, like this command, which will show all files sorted by file size with the largest files at the bottom:
  30.  
  31. [code]ls -lSr[/code]
  32.  
  33. If you use the [color=#3399ff][b]–a option, you can see hidden files, and you'll also notice something else in the listing: there are two entries for "." and ".." at the beginning of the list. These represent the current folder—the "." folder—and the parent folder—the ".." folder.
  34.  
  35. [img]https://commonwealth.service-now.com/cmdcode_1.jpgx[/img]
  36.  
  37. [b][u][size=medium][color=#ff3333]Change Directories:[/color][/size][/u][/b]
  38. You can change between directories using the cd command, and using what we just learned about the ".." folder, you can use the following command to switch to the directory directly above the current one:
  39.  
  40. [code]cd ..[/code]
  41.  
  42. You can navigate to either full or relative paths. For example, the command above navigates to a relative path—one above the current folder. If you're in /path/to/ and you want to navigate to the folder stuff inside that folder, you can simply type:
  43.  
  44. [code]cd stuff[/code]
  45.  
  46. You can also navigate to absolute paths. Again, if I were in /path/to/ and I want to navigate to /another/path/, I would simply type:
  47.  
  48. [code]cd /another/path[/code]
  49.  
  50. To swap directories to the previous working directory, the '-' (hyphen) shortcut is handy to have on hand. For example, if you were in the /first/folder/path/ directory and then switched over to /etc/ to make a configuration change, you might not want to type in the full path to switch back. You can quickly switch back to the previous working directory with this command:
  51.  
  52. [code]cd -[/code]
  53.  
  54. [b][u][size=medium][color=#ff3333]Creating or Removing Folders:[/color][/size][/u][/b]
  55. To create a new folder, you can simply use the command:
  56. [code]mkdir <foldername>[/code]
  57.  
  58. If there are files in the folder, you'll have to delete those files before you can remove the folder. You can then remove any folder with this command as long as the folder is empty:
  59. [code]rmdir <foldername>[/code]
  60.  
  61. [b][u][size=medium][color=#ff3333]Creating and Removing Files:[/color][/size][/u][/b]
  62. You can use this command to create a new, blank file:
  63. [code]touch <filename>[/code]
  64.  
  65. And then use this command to delete files:
  66. [code]rm <filename>[/code]
  67.  
  68. You can quickly remove all files in a directory by using the '*' (asterisk) wildcard—another simple tool that will come in very handy during your time in the command line. For example, if you're in a folder and want to delete every file inside that folder, just type:
  69.  
  70. [code]rm *[/code]
  71.  
  72. If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the
  73. [color=#3399ff][b]-r[/color][/b] option for recursive, and the [color=#3399ff][b]-f[/color][/b] option for force. This command will wipe out every instance of a matching filename pattern (note the slightly different use of the wildcard) from the current directory and below:
  74.  
  75. [code]rm –rf filename.*[/code]
  76.  
  77. [b][u][size=medium][color=#ff3333]Edit Plain Text Files:[/color][/size][/u][/b]
  78. The command that you use to edit text files will be different based on the platform you're using and the application you prefer to use. If you're using Ubuntu Linux, you can use the nano editor to quickly edit files, which might be more suitable for beginners.
  79.  
  80. [code]nano /path/to/file[/code]
  81.  
  82. Otherwise, the vim editor is available on just about any system and can be invoked with the syntax:
  83. [code]vi <filename>[/code]
  84.  
  85. [b][u][size=medium][color=#ff3333]Displaying Files:[/color][/size][/u][/b]
  86. You can display the file contents directly on the screen with the cat command, but the results will probably go flying past you on most large files, so it's usually better to use the more or less commands. For instance:
  87.  
  88. [code]more <filename>[/code]
  89.  
  90. This will display the contents of a file on the screen, and prompt you to scroll through the file a screen at a time.
  91.  
  92. [b][u][size=medium][color=#ff3333]Command redirection:[/color][/size][/u][/b]
  93. Each command line application can accept standard input and writes to standard output, and you can use the > or | operators to redirect output from one command into another, which lets you chain commands together into much more powerful commands. For instance, if you want to use ls [color=#3399ff][b]–l to display a list of files but it keeps scrolling off the screen, you can pipe the output from the [color=#3399ff][b]ls –l[/color][/b] command into the input of the more command by using the | character:
  94.  
  95. [code]ls –l | more[/code]
  96.  
  97. If you wanted to save the output of that list directly into a file instead of displaying on the console, you could use the > operator to redirect the output into a file instead:
  98.  
  99. [code]ls -l > filename.list[/code]
  100.  
  101. You could then use the cat command to display the contents of that file, pipe that into the grep command (detailed further below), and then redirect that output into a separate file:
  102.  
  103. [code]cat filename.list | grep keyword > filefound.list[/code]
  104.  
  105. [b][u][size=medium][color=#ff3333]Running a Script in the Current Folder:[/color][/size][/u][/b]
  106. If you have an application or shell script in the current folder, you can't simply type the name of the command and expect it to start. You'll need to add a ./ to the beginning of the command in order to start it. Why? Because in the Bash shell, the current directory, or "." folder, is not included in the system path. So to launch scriptname.sh in the current folder, you'll need to use:
  107.  
  108. [code]./scriptname.sh[/code]
  109.  
  110. [b][u][size=medium][color=#ff3333]Using History:[/color][/size][/u][/b]
  111. You can use the history command to show a list of all the recently used commands, or the up/down arrows to loop through them. The Ctrl+R shortcut key will start a search mode where you can type the first few characters of a command to search through your recent history.
  112.  
  113. [b][u][size=medium][color=#ff3333]Looping Over a Set of Files:[/color][/size][/u][/b]
  114. If you want to loop through a set of filenames and perform an action on each one, you can use the for command to loop through a set of files. For instance, to loop through all the .txt files in the current directory and display them on the console, you could use:
  115.  
  116. [code]for f in *.txt;do echo $f;done[/code]
  117.  
  118. [b][u][size=medium][color=#ff3333]Find Files:[/color][/size][/u][/b]
  119. If you wanted to find all files with .txt in the name that were modified in the last 5 days, you would use this command:
  120.  
  121. [code]find . –name "*.txt" –mtime 5[/code]
  122.  
  123. [b][u][size=medium][color=#ff3333]Find a Text String in Files:[/color][/size][/u][/b]
  124. The grep command can be used to quickly find text within files, even searching through subdirectories. For instance, if you wanted to search through all files in the current directory and below it for "text string", you could use this command:
  125.  
  126. [code]grep –ir "text string" *[/code]
  127.  
  128. [b][u][size=medium][color=#ff3333]Batch Rename Files:[/color][/size][/u][/b]
  129. You can use the rename command to quickly rename files using a regular expression pattern. For instance, if you wanted to rename all files containing foo to contain bar instead, you could use a command like this one:
  130.  
  131. [code]rename –v 's/foo/bar/g' *[/code]
  132.  
  133. [b][u][size=medium][color=#ff3333]sing Bash Shortcut Keys:[/color][/size][/u][/b]
  134. There are a number of very useful shortcut keys you can use the bash shell, and it pays to master them all. Here's a couple to get you started:
  135.  
  136. [color=#3399ff][b]Ctrl + U: Clears the line from the cursor point back to the beginning.
  137. Ctrl + A: Moves the cursor to the beginning of the line.
  138. Ctrl + E: Moves the cursor to the end of the line.
  139. Ctrl + R: Allows you to search through the previous commands.[/b][/color]
  140.  
  141. [b][u][size=medium][color=#ff3333]Customizing Your Command Shell:[/color][/size][/u][/b]
  142. There's no need to do your work in a boring terminal when you can do all sorts of tricks to customize it, like changing the colors, fonts, and adding aliases to complicated commands to save yourself time.
  143.  
  144. [img]https://commonwealth.service-now.com/cmdprmpt2.jpgx[/img]
  145.  
  146. [b][u][size=medium][color=#ff3333]Using Aliases:[/color][/size][/u][/b]
  147. Aliases save you loads of time by shortening long, complicated commands down into really simple ones, or by setting the default parameters to a command so you don't have to type them every time. For instance, if you wanted to set up an alias for installing packages on your Ubuntu setup that's quicker and simpler than [color=#3399ff][b]sudo apt-get install packagename[/color][/b], you could use something like this:
  148.  
  149. [code]alias agi='sudo apt-get install'[/code]
  150.  
  151. This alias would make it so you could simply type [color=#3399ff][b]agi packagename[/color][/b] at the shell to install any package in fewer keystrokes. You can also use aliases to set the default arguments for a command, so if you always wanted ls to actually do [color=#3399ff][b]ls –l[/color][/b], you could use this alias:
  152.  
  153. [code]alias ls='ls –l'[/code]
  154.  
  155. There are any number of useful aliases that you can use aliases to personalize your setup, but if you're having trouble coming up with good ideas, check out the list of the ten most useful aliases.
  156.  
  157. [b][u][size=medium][color=#ff3333]Control Your System from the Shell:[/color][/size][/u][/b]
  158.  
  159. [img]https://commonwealth.service-now.com/cmdprmpt3.jpgx[/img]
  160.  
  161. The terminal has a rich set of tools for manipulating processes and checking on system stats. You can use the ps command to see a list of system processes like this:
  162.  
  163. [code]ps aux[/code]
  164.  
  165. You can then use this command to get rid of any processes that you want to kill:
  166. [code]kill <pid>[/code]
Add Comment
Please, Sign In to add comment