02Creating, Opening, and Editing Files
2.1. Create a New File: touch
To create a new file, use the touch command followed by the name of the file you want to create, for example, touch example.txt.
2.2. Open a File: open
To open a file with its default application, use the open command followed by the filename, like open example.txt.
2.3. Edit a File: nano, vim, emacs
There are several text editors you can use in the terminal:
- Nano: Nano is a simple and beginner-friendly editor. To open a file with Nano, type
nano example.txt.
- Vim: Vim is more powerful and
has a steeper learning curve. Open a file with Vim using vim example.txt.
- Emacs: Emacs is another powerful editor, which some users prefer over Vim. Open a file with Emacs using
emacs example.txt.
In each of these editors, make your changes then save and exit.
- In Nano, press
Ctrl + Oto save, thenCtrl + Xto exit. - In Vim, press
Esc, type:wq, then pressEnter. - In Emacs, press
Ctrl + X, thenCtrl + Sto save, andCtrl + X, thenCtrl + Cto exit.
04Copying, Moving and Deleting Files
4.1. Copy a File: cp
To copy a file, use the cp command. The syntax is cp source destination. For example, to copy file1.txt to the same directory with the name file2.txt, you would use cp file1.txt file2.txt.
4.2. Move or Rename a File: mv
To move a file (or to rename a file), use the mv command. The syntax is similar to cp: mv source destination. For example, to rename file1.txt to file3.txt, you would use mv file1.txt file3.txt.
4.3. Delete a File: rm
To delete a file, use the rm command, followed by the name of the file. For example, to delete file2.txt, you would use rm file2.txt.
Warning: Be careful with the
rmcommand. Once you delete a file, it's gone.
05Redirection and Piping
5.1. Redirection: >, >>
In Unix-like systems, you can redirect the output of a command to a file. For example, if you want to save the output of ls -l to a file, you would use ls -l > output.txt. This will overwrite output.txt. If you want to append to the file without deleting the current content, use >> instead, like ls -l >> output.txt.
5.2. Piping: |
Piping allows you to use the output of one command as the input to another command. This is particularly useful for chaining commands together. For example, you can pipe the output of ls -l to grep txt to find all txt files, like ls -l | grep txt.
06File Permissions and Ownership: chmod, chown
6.1. Change File Permissions: chmod
File permissions determine who can read, write, or execute a file. You can change file permissions with
the chmod command. For example, to give the user read, write, and execute permissions on file1.txt, you would use chmod u+rwx file1.txt.
6.2. Change File Ownership: chown
The chown command is used to change the owner of a file. For example, to change the owner of file1.txt to the user "john", you would use sudo chown john file1.txt.
Note: The
sudocommand is used to run commands with superuser privileges ("root" privileges). Be very careful when usingsudo, as it allows you to make critical changes to your system.
07Viewing File Content: cat, less, head, tail
To view the content of files, you can use commands like cat, less, head, and tail.
catdisplays the entire content of the file.lessallows you to scroll through the content of large files.headshows the first few lines of a file (10 lines by default).tailshows the last few lines of a file (also 10 by default).
These commands and many more allow you to interact with your macOS file system directly from the terminal, providing a powerful and flexible way to manage files and directories.
By now, you've mastered the essentials of navigating the file system from your terminal. But, as with any language, fluency comes with practice. Spend some time getting comfortable with these commands, and you'll soon be navigating your file system like a pro.