How to use your macOS terminal smarter
If you have ever had a Linux you might already use the terminal to navigate through your computer, but often, mac owners might ignore or not know the existence of this powerful tool, it might look scary for someone who hasn’t used it before, so here are some tips and tricks to start right away.
Note: You can think about Directory as a “folder” it can have files or other directories inside, while also belonging to other directory.
What is the terminal?
The terminal is the Mac command-line interface, it offers text-based access to the operating system, instead of relying on the graphical interface of macOS, everything you do on the terminal will affect your computer, so be careful as if you accidentally delete something you shouldn’t it will be gone forever.
So let’s get started
While on the terminal you will be able to see where you are by looking what says before the User, if you see a ~ it means you are in the home directory if it has anything else written on it, that’s the folder it is inside.
To go to Home Directory type:
cd
or cd ~
You can also use cd
+ the directory name to access a folder:
Not uncommonly while coding you might not be able to access your assets simply because you are in the wrong directory, to see the files inside you can type ls
.
You can also type pwd
to see the full path to the working directory.
And once you found your file, you can open it with:
Open index.html
(if it’s an HTML file, it will open it in the browser, if you want to open the code editor, check to see how you can do it with the code editor of your choice).
Or if you wish to open the folder, just type:
open .
To create a new file use:
touch fileName.txt
And if you wish you can edit the file with Vim or Nano typing:
nano fileName.txt
or vim fileName.txt
(.txt if it’s a text file)
But if you are a beginner you might want to avoid it, as it can be a little confusing to someone just starting to use the command line.
To remove a file from the directory use:
rm -i fileName
(typing -i, will ask you to confirm if you wish to delete that file, you can also type rm filename and delete it directly).
You can also create a new directory, in the one you are currently:
mkdir directoryName
To remove a directory with its content it’s:
rm -r directoryName
Others commands include:
history 3
, to see a list with all the commands typed, with a limit of a certain amount of items, in this case, 3.!!
, to Execute the last command typed.!!:p
, shows in the console what was the last command executed.cp [file][newfile]
,cp [file][newFileName]
, orcp [file][directory]
, to copy file to an existing file or new file or directory.find [dir] -name [search_pattern]
, to search for files, e.g. find /Development -name “fineName.txt”.
Add Terminal Aliases
Something else you can do to make your experience with the terminal even better it’s to make it your own, you can create your own command line commands on your macOS terminal with .bash_profile.
For example, if you wish, you can type c + enter
instead of clear
on your terminal by adding the alias c=“clear”, as I’m constantly going to the same folder, I often had to type cd Development/code
to access it, but now I only need to type school
.
If you want to try, follow these instructions:
- Make sure you are on your home directory by typing
cd
- Type
nano .bash_profile
to open it with the nano editor, you can also usevi .bash_profile
- Use the down key to go to the end of the terminal to type the new command, to write your new command type
alias [newCommand]= “[old command]”
- You will then press ctrl + X to exit, it will then ask if you wish to save the changes by pressing Y or N.
- Restart your terminal
If you wish to add more alias, just write the new after the one you just created, or if you wish to remove or to edit the alias you created, you can open .bash_profile
and edit the file.