Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

Helpful Terminal Commands for Beginners!

$
0
0

This cheat sheet was originally posted on malikbrowne.com .

I love learning new commands and tools to optimize my workflow on my Mac. Since I have started working with more terminal-oriented applications, there are several commands that I use that I'd love to share with beginners and terminal lovers alike.

Note:This article assumes that you're using some type of UNIX shell , with a preference towards Mac. Some of these commands may not work on windows CMD/Powershell.

Basic Commands

Commands in a shell can be used in a ton of different ways - but I'd say that the important ones to learn fall under three main categories:

Navigating and Working with Files & Directories Manipulating Output for Input Finding Things Navigating and Working with Files & Directories

On a computer, files and directories (otherwise known as folders) are responsible for managing information. Here are some commands to make your life easier when working with files:

cd - navigate to different directories pwd - see the name of the current directory ls - lists all files in the current directory mkdir - make a new directory touch - make a new file cp - copy a file mv - move a file or directory rm - removes a file or directory zip - compresses files into a zip archive unzip extracts files from a zip archive chmod - Allows you to make a file executable and change permissions granted to it by your machine. In order to make a file executable, you can type chmod +x [name of file] tar - Allows you to work with tarballs in a linux command line. It has a long list of uses, including package management for systems. tar -cvf allows you to create a .tar archive tar -xvf allows you to "untar" a .tar archive tar -tvf list the contents of a .tar archive

Honorary Mention: sudo

sudo is a very widely used command in bash interfaces that allows you to run a command with administrative or root privileges.

For example, if you edit any files in the root directory (also known with the path / ) of your machine without sudo privileges, you will be denied permissions to edit the file.

For your safety, there are a couple of things your computer will not allow you to do as a sudo 'd command, i.e. running bash scripts.

Manipulating Output for Input

Often times when scripting the goal you are trying to achieve is to run filters through text, check output logs from a server, or run batch commands on multiple files.

Here are some commands that can be beneficial for manipulating file output:

cat - show a file's contents head - displays the first few lines of a file tail - displays the last few lines of a file Using the -f flag with tail will allow you to see updates to the file as they are coming in. This is extremely helpful for tracking log output from servers. | - The pipe character take a command's output and uses it for the input of another command * - matches zero or more characters in a filename There is actually a whole way of matching multiple files and names via globs! ? - matches any single character in a filename > - stores a command's output to a file, and creates a new file if one doesn't already exist WARNING: This will override and replace all contents that were in the original file. >> - concatenates a command's output to a file, creates a new file if none is found Finding things echo grep ack Other helpful commands I've found pbcopy - Copy selected text into your clipboard buffer pbpaste - Paste the selected text in the terminal from your clipboard buffer curl - Allows you to make HTTP calls RESTful endpoint lsof -i tcp:[port number here] - list all running processes on a specific port This one is particularly useful for when you get an error like: Error: listen EADDRINUSE :::3005 at Object.exports._errnoException (util.js:1023:11) at exports._exceptionWithHostPort (util.js:1046:20) at Server._listen2 (net.js:1261:14) at listen (net.js:1297:10) at Server.listen (net.js:1375:9) at Object.<anonymous> (/path/to/node/server/server.js:15:34) at Module._compile (module.js:571:32) at loader (/path/to/node/modules/node_modules/babel-register/lib/node.js:144:5) at Object.require.extensions.(anonymous function) [as .js] (/var/www/html/gcsbpo/rocc/node_modules/babel-register/lib/node.js:154:7) at Module.load (module.js:488:32)

It provides an easy way to find out the pid to kill the running process.

Shortcuts/Tips & Tricks !! - repeats the last command executed clear - clears all output from the terminal, old output - can still be accessed by scrolling up CMD + k - clears all output from the session, only previously called commands are retained CTRL + c - Stops any command in terminal safely CTRL + z - Force stops any command in terminal CTRL + a - Takes you to the beginning of the bash input CTRL + e - Takes you to the end of the bash input CTRL + u - Clears all input before the cursor CTRL + r - Opens a prompt that will search the previous commands from your session Conclusion

Learning how to navigate the terminal is definitely daunting at first, but after spending some time scripting you'll find that these commands can save you tons of time on a day to day tasks that you do.

Personally, I'm starting to reach the point where I navigate faster via Terminal than when I use Finder.

If you have any commands, shortcuts, or tips I should add to this post, please leave me a comment! Would love to add more.


Viewing all articles
Browse latest Browse all 11063

Trending Articles