Linux/Mac Terminal Tutorial

This tutorial is mainly based on YouTube - Linux/Mac Terminal Tutorial by CoreyMS. You can also refer to GitHub - Terminal Cheat sheet for Mac (Basics).

Show the help manual for the command

No matter what kind of command you meet, you can use the command as follow to show the help manual:

man <command>

For example, if you would like to know more about the command curl:

➜  ~ man curl

You will get:

curl(1)                           Curl Manual                          curl(1)



NAME
       curl - transfer a URL

SYNOPSIS
       curl [options] [URL...]

DESCRIPTION
       curl  is  a tool to transfer data from or to a server, using one of the
       supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS,  IMAP,
       IMAPS,  LDAP,  LDAPS,  POP3,  POP3S,  RTMP, RTSP, SCP, SFTP, SMB, SMBS,
       SMTP, SMTPS, TELNET and TFTP). The command is designed to work  without
       user interaction.

       curl offers a busload of useful tricks like proxy support, user authen-
       tication, FTP upload, HTTP post, SSL connections, cookies, file  trans-
       fer  resume,  Metalink,  and more. As you will see below, the number of
       features will make your head spin!

       curl is powered by  libcurl  for  all  transfer-related  features.  See
       libcurl(3) for details.
:

Press q to quit.

Navigating your file system

If you would like to get the full path to working directory:

➜  ~ pwd

Notice:

  • . represent the current folder
  • .. represent the parent/enclosing directory.

If you would like to go to specific folder:

cd <Directory Path>

For example, if you would like to move 2 level up:

➜  ~ cd ..

If you would like to list files in the working directory:

➜  ~ ls

-a

-a means including hidden files. For example, if you would like to show all the files including those hidden files:

➜  ~ ls -a

-l

-l means list long/more information.For example, if you would like to long list files including hidden files with human readable file sizes, you can use -lah:

ls -lah <Dir>

You will get the information in the form as follow:

<permission> <user> <group owner of files/folders> <size> <date info>

Manipulating your files and directories

Make new

If you would like to make a new directory:

mkdir <dir>

If you would like to create a new file:

touch <>file

Open

If you would like to open a file:

open <file>

Copy

If you would like to copy a file to file:

cp <file> <new file>

If you would like to copy a file to new folder:

cp <file> <newdir>

If you would like to copy directory

cp <dir> <newdir>

For example:

➜  ~ cp -R TestDir/ CopyDir/

Move

If you would like to move a file to a new directory:

mv <file> <newdir>

Rename

Or you would like to rename a file:

mv <file> <newfile>

For example, if you want to rename directory without any options:

➜  ~ rm TestDir OrgDir

Remove

Remove file

If you would like to remove a file:

rm <file> # Be careful!!!

There are several suffixes:

Remove with confirmation:

rm -i <file>

Force removal without confirmation:

rm -f <file>

Remove directory

If you would like to remove a directory and contents:

rm -r <dir>

For example:

➜  ~ rm -R TestDir

If you want to force remove directory:

➜  ~ rm -rf TestDir #  Be careful!!!

How to use the find command

If you want to search for files:

find <dir> -name <search_pattern>

For example:

➜  ~ find . -name "*.txt"

-type d

Find all directories in the <dir>:

find <dir> -type d

-type f

Find all the files in the <dir>:

find <dir> -type f

-name

Find all the file with the name of "<file_name>" (-name is case sensitive) in the <dir>:

find <dir> type f -name "<file_name>"

For example:

➜  ~ find . type f -name "test_1.txt"
➜  ~ find . type f -name "te*.txt"

-iname

Find all the file with the name of "<file_name>" (-iname is case insensitive) in the <dir>:

find . type f -iname "te*.txt"

-mmin or -mtime

Find files modified in less than 10 min ago:

➜  ~ find . type f -iname "te*.txt" -mmin -10

Find files modified in more than 10 min ago:

➜  ~ find . type f -iname "te*.txt" -mmin +10

Find files modified in more than 1 min but less than 10 min:

➜  ~ find . type f -iname "te*.txt" -mmin -10 -mmin +1

Find files modified in less than 10 days ago:

➜  ~ find . type f -iname "te*.txt" -mtime -10

What’s more:

-amin -atime # access min and access days
-cmin -ctime # change min and change days

Find all the files with the size larger than 5 megabytes:

➜  ~ find . size +5M

-perm

What is 777 permission? See Understanding File Permissions: What Does “Chmod 777” Mean?.

  7       7    7
 user   group  world
 r+w+x  r+x    r+x
 4+2+1  4+0+1  4+0+1 = 755

Mode Str Perms Explanation
0477 -r—rwxrwx owner has read only (4), other and group has rwx (7)
0677 -rw-rwxrwx owner has rw only(6), other and group has rwx (7)
0444 -r—r—r— all have read only (4)
0666 -rw-rw-rw- all have rw only (6)
0400 -r———— owner has read only(4), group and others have no permission(0)
0600 -rw———- owner has rw only, group and others have no permission
0470 -r—rwx—- owner has read only, group has rwx, others have no permission
0407 -r——-rwx owner has read only, other has rwx, group has no permission
0670 -rw-rwx—- owner has rw only, group has rwx, others have no permission
0607 -rw——rwx owner has rw only, group has no permission and others have rwx

See full list 0000 to 0777.

Find files with 777 permission, which can be read, wrote and excuted:

➜  ~ find . -perm 777

-chown

Change owner. User is <user>, and group is <group>. {} is placefolder. + or \ is the sign end of command:

find <Dir> -exec chown <user>:<group> {} +
ls -la <Dir>

Change permission of directories to 775

find <Dir> -type d -exec chmod 775 {} +
find <Dir> -perm 775

Change permission of files to 664

find <Dir> -type f -exec chmod 664 {} +
find <Dir> -perm 664

-maxdepth

Before the batch operation of files, excuting find command to check the file list that sutisfies the requirements is a good habbit.

Search down only one direcory which is the current working directory:

➜  ~ find -type -name "*.jpg" -maxdepth 1

Delete all the .jpg files in the current directory:

➜  ~ find -type -name "*.jpg" -maxdepth 1 -exec rm {} +

How to use the curl command

curl command allow us to query url from command line:

➜  ~ curl http://blog.baoduge.com

First of all, I should running a local host using hexo:

➜  ~ hexo s
INFO  Start processing
INFO  Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.

-i

-i, —include
Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more…

curl -i <url>

For example, I try to query jason response and see response header:

➜  ~ curl -i http://localhost:4000/json-test

-X, —request
(HTTP) Specifies a custom request method to use when communicat-
ing with the HTTP server. The specified request method will be
used instead of the method otherwise used (which defaults to
GET). Read the HTTP 1.1 specification for details and explana-
tions. Common additional HTTP requests include PUT and DELETE,
but related technologies like WebDAV offers PROPFIND, COPY, MOVE
and more.

For example, check post request (You might be interested in REST—REpresentational State Transfer1):

➜  ~ curl -d <data> http://localhost:4000/methods
➜  ~ curl -X PUT -d <data> http://localhost:4000/methods
➜  ~ curl -X DELETE -d <data> http://localhost:4000/methods

Provide username and password

-u

-u, —user
Specify the user name and password to use for server authentica-
tion. Overrides -n, —netrc and —netrc-optional.

➜  ~ curl -u <user_name>:<password> http://localhost:4000/secret

-o

-o, —output
Write output to instead of stdout. If you are using {} or
[] to fetch multiple documents, you can use ‘#’ followed by a
number in the specifier. That variable will be replaced
with the current string for the URL being fetched.

For example:

➜  ~ curl -o <file name> http://localhost:4000/download

curl -o commits.json https://api.github.com/repos/CoreyMSchafer/Demo-Repo/commits

How to use the rsync command

Synchronize the files in the folder, not including the subfolders and the content inside.

For example, Folder Backup is synthesized with all files in Folder Original:

➜  ~ rsync Original/* Backup/

-r

-r, —recursive recurse into directories

If you want to synchronize all the files in the subfolder:

➜  ~ rsync -r Original/ Backup/

-a

-a,
—archive archive mode; same as -rlptgoD (no -H) —no-OPTION turn off an implied OPTION (e.g. —no-D)

-a is not only recursive, but can also copy symlinks preserves the permission, modification times, groups, owners and so on.

-a is used most of the time to *copy everything.

For example, I want to copy everything in folder Original into the folder Backup :

➜  ~ rsync -a Original/ Backup/

--dry-run or -in

-v, —verbose increase verbosity

-n, —dry-run show what would have been transferred

At first, run the command as follow to check what would have been transferred. Make sure you won’t make a mistake (GOOD HABBIT):

➜  ~ rsync -av --dry-run Original/ Backup/

Then run the command:

➜  ~ rsync -av Original/ Backup/

--delete

When Backup folder contains some files that Original folder doesn’t have.

Run the command at first (GOOD HABBIT):

➜  ~ rsync -av --dry-run --delete Original/ Backup/

Then run the command:

➜  ~ rsync -av --delete Original/ Backup/

For remote machine

You need the SSH access and IP address.

-z zip and compress

-P show the progress

➜  ~ rsync -zaP ~/Projects/my_site coreyms@192.168.56.100:~/public/
  • `<username>@``
  • ~/Projects/my_site: my source;
  • no slash means that I’d like to syn the entire directory
  • colon to specify the location in the remote machine

SSH into the remote machine

➜  ~ ssh coreyms@192.168.56.100

Local machine synthesized with the remote machine:

➜  ~ rsync -zaP coreyms@192.168.56.100:~/public/my_site/backups ~/Projects/my_site/

How to schedule tasks with crontab

Check more about cron table on Linux crontab command.

The crontab (short for “cron table”) is a list of commands that are scheduled to run at regular time intervals on your computer system. The crontab command opens the crontab for editing, and lets you add, remove, or modify scheduled tasks.

-l

Display the current cron table:

➜  ~ crontab -l

-u

-u
Specify the name of the user whose crontab is to be tweaked. If
this option is not given, crontab examines ``your’’ crontab,
i.e., the crontab of the person executing the command. Note that
su(1) can confuse crontab and that if you are running inside of
su(1) you should always use the -u option for safety’s sake.

Edit the cron table of user 2

➜  ~ crontab -u user2 -e

-r

-r Remove the current crontab.

Remove crontab:

➜  ~ crontab -r

crontab.guru can help you to edit your cron table.

Customizing your terminal: .bash_profile and .bashrc files

  • .bash_profile file for login shell
  • .bashrc file for non-login shell

Creating alias for commands

change .bash_profile

➜  ~ sudo nano .bash_profile

In the nano editor

➜  ~ alias dt='cd ~/Desktop/' # Go the the desktop

Example:

# Show all hidden files
defaults write com.apple.finder AppleShowAllFiles YES
killall Finder
1. 知乎|怎样用通俗的语言解释REST,以及RESTful?
-------------End of postThanks for your time-------------
BaoDuGe_飽蠹閣 wechat
Enjoy it? Subscribe to my blog by scanning my public wechat account