Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 25, 2022 03:14 am

Command Line Basics and Useful Tricks With the Terminal


 In this post, we'll go over what the command line is, as well as some commands to get started using it.


About Terminal


What Is Terminal?


Terminal is an interface for you to execute text-based commands, and it gives you direct access to the underlying operating system, via a shell, on your Mac. A long time ago, before computers had graphical user interfaces (GUIs), the only way to operate a computer was through the command line. Needing to run the computer through the command line helped users better understand what was going on inside their computer.


Ever since GUIs became more commonplace, we have almost forgotten about the command line, and the typical Mac user probably doesn't even know that it exists. This may be okay for the layperson, but as software developers, it's still crucial that we know how to use the terminal.


Why Use the Terminal?


Wasn't the whole point of graphical user interfaces to eliminate the need for a command line? Well, not really. There are still several reasons that you should learn how to use the Terminal if you are a developer, or even a user who cares about performance:



  • Certain things simply cannot be done without the command line (especially as a developer). These include installing CocoaPods, using a git repository, and more.

  • You can easily make changes to your computer's settings which would be tedious or even impossible to do using the user interface only.

  • The terminal is very flexible, and once you learn to use it, it is quite simple and straightforward.

  • It is a very direct way of telling your computer what to do, whereas a GUI is akin to controlling the computer from a distance.


Those are just a few of the reasons that the Terminal is still a useful tool today. It's a practical and functional tool to improve and enhance your daily interactions with your computer.


Getting Started


Open Terminal


Terminal is located in your Applications folder within a subfolder called Utilities. If you cannot find it, activate Spotlight search by pressing Command-Spacebar and search for Terminal. Once you have it open, you should see something which looks like this:


Figure 1 New Terminal Window

This is called the "command prompt"—you'll see the current directory, then your username followed by a $.


Executing Commands


To run a command, simply type it in at the command prompt and press Enter. Try it now with the command ls—this will list all the files in the current directory. Often, online instructions will write commands to be entered starting with a $. You don't need to type this out. For example: 






For the command above, you would just type cd ~/Desktop, omitting the $.


Basic Commands Cheat Sheet


Here is a list of some of the basic commands that you'll need to know in order to use the terminal effectively. This is a handy cheat sheet for the most important and most commonly used commands.


Help




  • help—as the name suggests, you can type this command into the Terminal to get a list of different commands.


  • man <command name>—similar to the previous command, this command tells you exactly what a command does (and gives you full documentation) without you having to search Google for it.


File Management




  • ls—lists all of the contents in the current directory. This command comes in handy if you don't want to use the Finder to browse files—you can simply list them using this command in your Terminal.

  • la -a—list hidden files and directories


  • cd <directory name>—this command is used to change directories. If you write cd alone, you will move out of the current directory. After using ls (to see the directories), you can write the name of the directory you want to enter.


  • code .—open any source code folder via terminal.


  • touchcreate a new empty file, or update an existing file to today's date


  • clear—wipe the terminal display


  • mv—move files and directories from one location to another. You can also use it to rename files and directories. 


  • cp—copy files from one folder to another.

  • rm —remove a file. Be careful with this command, the file will not go to the recycle bin!

  • rmdir—remove an empty directory.

  • open—open a file with the appropriate system viewer.


Changing Preferences




  • defaults <setting to change>—this command is used to modify default system settings, some of which cannot be changed without using the terminal.


  • caffeinate—as the name suggests, this command is used to prevent your Mac from dimming, turning off, or sleeping. To end this, all you need to do is press Control-C.


Text Editing




  • vim <file name>—this is one of my favorites. You can edit text files using the default TextEdit (or any GUI based editor), but vim is basically a command-line text editor—that is, it works entirely within the terminal.


Networking




  • ping <URL or IP Address>—this command allows you to check the server response time of a specified URL or IP Address. This may not be useful for every developer, but it is nice to know.


Admin




  • sudo <action to perform>—a way to override your user's privileges and become a superuser of the computer. You will be prompted for an admin password when you use it.


In Depth


Now that you know a few of the basic commands, let's go a little bit more in depth to take a closer look at five of the things you can do with the Terminal. 


1. Change the Default Format for Screenshots


We all know about using Command-Option-4 to take screenshots on our Macs. And although it's great that these screenshots are taken in high-quality PNG files, we don't necessarily want 15 MB to 20 MB of our precious storage taken up for every single screenshot. Luckily, you can change this default with the Terminal.


After your prompt (remember, it's that $ sign), type in the following command:






Figure 3 Stress Test

After you press Enter, you will notice that the file type of your screenshots has changed to what you just set it to. You can use any format such as PDF, JPG, and more, but for this example I have used JPG. 


2. Stress Test Your Mac


There is a simple command which many Apple technicians use when your computer is acting up. This command stresses your CPU by simply printing the letter "y" over and over. If you open your Activity Monitor, you will soon see that this process takes up most of your CPU!


In order to do this, after the prompt, all you need to do is type the following command:



This seemingly innocent command will stress your Mac's CPU to the max, and will easily help you check if your computer is crashing under pressure.


3. Stop Your Mac From Sleeping


Isn't it annoying when you are installing or uploading something on your Mac, and your Mac keeps going to sleep? This amusingly named command will make sure that doesn't happen. 


All you need to do is type the following command after the prompt:



Figure 4 Caffeinate


Press Enter, and your Mac won't sleep, dim, or turn off. Once you're ready to disable this, all you need to do is press Control- from the terminal.


4. Show Hidden Files


By default, your computer hides certain important files for its user, but as developers, sometimes we need access to these files. This prevents amateur users from messing with the computer's internal settings, but as developers, we understand how to handle these files.


To make all hidden files visible, enter the following command after the prompt:



And then, type this command to reset Finder after the prompt:



Figure 5 Show Hidden Files

If you want to hide them again, enter the following command after the prompt:



And then reset Finder again:



Figure 6 Hide Hidden Files

5. Downloading Files


We've all gone through the process of downloading files, unzipping them, and using them. But that can be a hassle, especially when you need to do this multiple times. Using the terminal, you can do this with the URL of your download file.


To do this, use the curl command as follows:




6. How to Open a Website on Your Terminal


Open your terminal write the following start www. website address after writing the website address click on your enter button, you will be redirected to the browser and you see the website. Below is an example of a website I opened. 




7. How to Shut Down or Restart Your Computer


Another simple way of shutting down your laptop is through the terminal below is the command for shutting down and restarting your computer 


Shutdown Computer



Restart Computer



8. How to Check the IP Address of a Website 


With terminal, you can easily check the IP address of any website with the following command:




9. How to Hide a Folder with Terminal


Some folders are not supposed to be accessible to everyone with your laptop. Hiding a folder can be hard, but it's simple with a terminal with the following command




after successfully hiding this folder, how do you recover it? well it is very simple, you can unhide your folder with the following command:




Conclusion


Although it may seem daunting at first, the command line is an extremely powerful tool that, in some cases, is easier to use than the GUI. I hope you enjoyed this tutorial and found it helpful.


For more about coding for Mac and iOS, check out some of our other tutorials here on Envato Tuts+.


This post has been updated with contributions from Ezekiel Lawson. Ezekiel is a front-end developer who focuses on writing clean, maintainable code with web technologies like JavaScript, Vue.js, HTML, and CSS.



Original Link: https://code.tutsplus.com/tutorials/command-line-basics-and-useful-tricks-with-the-terminal--cms-29356

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code