|
|
@@ -20,3 +20,159 @@ Bash is a command processor that typically runs in a text window, where the user
|
|
20
|
20
|
|
|
21
|
21
|
The shell's name is an acronym for Bourne-again shell, a pun on the name of the Bourne shell that it replaces and on the common term "born again".
|
|
22
|
22
|
|
|
|
23
|
+## Top Ten (plus a few) shell commands
|
|
|
24
|
+
|
|
|
25
|
+### pwd
|
|
|
26
|
+`pwd` is Print Working Directory. What else can be said about the good old pwd who has been printing the current directory name for ages. Prints out where in the filesystem you are currently.
|
|
|
27
|
+
|
|
|
28
|
+### ls
|
|
|
29
|
+Lists the folder and file names in the current working directory. You can narrow the list down to files matching a specific name by passing the name as a parameter. This comes with a variety of flags; a few key ones are `-l` to list the long format that displays the file permissions, and `-a` to list all files including hidden files.
|
|
|
30
|
+
|
|
|
31
|
+So now you can see your files and folders, to navigate to a different directory you use…
|
|
|
32
|
+
|
|
|
33
|
+### cd
|
|
|
34
|
+Change Directory to the path specified, for example cd projects. There are a few really helpful arguments to aid this:
|
|
|
35
|
+
|
|
|
36
|
+. refers to the current directory ./projects
|
|
|
37
|
+.. can be used to move up one folder use cd .., and can be combined to move up multiple levels ../../my_folder
|
|
|
38
|
+/is the root of your system to reach core folders, such as system, users, etc
|
|
|
39
|
+~ is the home directory, usually the path /users/username. Move back to folders referenced relative to this path by including it at the start of your path, for example ~/projects . If you need to access your shell configuration, you can easily find this with ~/.bashrc or ~/.zshrc — then add all sorts of beneficial aliases, configurations, commands and paths.
|
|
|
40
|
+For more on the mac directory structure
|
|
|
41
|
+- OSXDaily: Mac OS X Directory Structure Explained
|
|
|
42
|
+Now you can navigate directories, its time to create some of your own using…
|
|
|
43
|
+
|
|
|
44
|
+### mkdir
|
|
|
45
|
+Make directories with this command mkdir my_folder . Not only can it make the folder specified, but also its parents if they do not exist already using the -p option. The command mkdir -p first_folder/next_folder/my_folder would also create the first and next folder. There is an option to set the mode, or permissions, via the -m flag, and these can be changed later with the chmod command (see below for more on mode and permissions).
|
|
|
46
|
+
|
|
|
47
|
+There are various ways to then create files using the command line, one of the most common is…
|
|
|
48
|
+
|
|
|
49
|
+### touch
|
|
|
50
|
+Touches the file to update the access and or modification date of a file or directory without opening, saving or closing the file. But one of the most common uses is to create an empty file touch my_file.
|
|
|
51
|
+
|
|
|
52
|
+Before you ask why I suggested touch to create files, read this page, which explores ways to create files using shell commands
|
|
|
53
|
+- StackExchange Unix & Linux: Why isn’t there any shell command to create files?
|
|
|
54
|
+To then use the file, open my_file will launch the application set for this file type. But you might want to see what’s in your files within the scope of the terminal. To do this you can use…
|
|
|
55
|
+
|
|
|
56
|
+### cat
|
|
|
57
|
+Concatenate and print files to stdout cat my_file. You can pass one or more file names to this command, and even number the lines using the -n to number the lines.
|
|
|
58
|
+
|
|
|
59
|
+For more on ways to use the cat command
|
|
|
60
|
+- 13 Basic Cat Command Examples in Linux
|
|
|
61
|
+The files may not be in the place you want them to be, rather than drag and drop using the mouse and windows, you command your terminal to…
|
|
|
62
|
+
|
|
|
63
|
+### mv
|
|
|
64
|
+Moves files and folders. The first argument is the file you want to move, and the second is the location to move it to. Use the flags -f to force move them and -i to prompt confirmation before overwriting files.
|
|
|
65
|
+
|
|
|
66
|
+Alternatively, you may not want to remove the original, for example when making backups, in this case you would use…
|
|
|
67
|
+
|
|
|
68
|
+### cp
|
|
|
69
|
+Copies files and folders cp my_file ./projects . The flag -r recursively copies subfolders and files.
|
|
|
70
|
+
|
|
|
71
|
+Now you have too many files, or some of them are no longer needed. The next command is a danger zone, because it is so good at its job…
|
|
|
72
|
+
|
|
|
73
|
+### rm
|
|
|
74
|
+Removes files and folders rm my_folder . Using -r will again recursively delete subfolders, -f force deletes, and -rf for a recursive force delete. If you want to remove all folders and files in the current directory the command is rm -rf ./* , if you leave out the dot then it would reference the root directory!
|
|
|
75
|
+
|
|
|
76
|
+Want to see what happens if you force a computer to run rm -rf /? Tip: don’t try this on yours, instead watch this video:
|
|
|
77
|
+- What happens when you sudo rm -rf / your machine? — YouTube
|
|
|
78
|
+There are ways to stop accidental deletes. If you use the -i flag (for interactive) you can specify the computer to prompt confirmation before delete.
|
|
|
79
|
+
|
|
|
80
|
+Another way is with permissions, which can be modified…
|
|
|
81
|
+
|
|
|
82
|
+### chmod
|
|
|
83
|
+Change mode so you can set permissions for read, write and execute for the user, members of your group and others. This uses binary values as an argument to set these. There are many common chmod permissions, a few key ones are:
|
|
|
84
|
+
|
|
|
85
|
+777 — anyone can read, write and execute chmod 777 my_file
|
|
|
86
|
+755 — for files that should be readable and executable by others, but only changeable by the issuing user
|
|
|
87
|
+700 — only the user/owner can do anything to the file
|
|
|
88
|
+For a more lengthy and detailed explanations of chmod see `man chmod`
|
|
|
89
|
+
|
|
|
90
|
+### chown
|
|
|
91
|
+Change owner is used to set which user owns a file or directory. Often you need to run `sudo` to make `chown` work. `chown` is a command to change the ownership of a file/folder or even multiple files/folders for a specified user/group.
|
|
|
92
|
+
|
|
|
93
|
+### man
|
|
|
94
|
+Manuals for a command can be shown with this instruction. Below is some of the output from running `man ls`, it also displays all the options available for running the command. This can be very important when you need to learn about the weird flags you might not know anything about. Try `man man`?
|
|
|
95
|
+
|
|
|
96
|
+```
|
|
|
97
|
+LS(1) BSD General Commands Manual LS(1)
|
|
|
98
|
+NAME
|
|
|
99
|
+ ls -- list directory contents
|
|
|
100
|
+SYNOPSIS
|
|
|
101
|
+ ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]
|
|
|
102
|
+DESCRIPTION
|
|
|
103
|
+ For each operand that names a file of a type other than directory, ls displays its name as well as
|
|
|
104
|
+ any requested, associated information. For each operand that names a file of type directory, ls
|
|
|
105
|
+ displays the names of files contained within that directory, as well as any requested, associated
|
|
|
106
|
+ information.
|
|
|
107
|
+
|
|
|
108
|
+ ...trimmed for example...
|
|
|
109
|
+```
|
|
|
110
|
+
|
|
|
111
|
+### env
|
|
|
112
|
+When you type `env`, you'll get a listing of all the shell's current environment variables.
|
|
|
113
|
+
|
|
|
114
|
+```
|
|
|
115
|
+$ env
|
|
|
116
|
+TERM_PROGRAM=Apple_Terminal
|
|
|
117
|
+SHELL=/bin/bash
|
|
|
118
|
+TERM=xterm-256color
|
|
|
119
|
+TMPDIR=/var/folders/2r/h0wd6frs6mjg7svdz6xkxchc0000gn/T/
|
|
|
120
|
+Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.SnQXyNrJvs/Render
|
|
|
121
|
+PLAN9=/Users/jj/GitHub/plan9port
|
|
|
122
|
+TERM_PROGRAM_VERSION=400
|
|
|
123
|
+TERM_SESSION_ID=73B19B26-443A-4DA6-AB30-C2AF953168E4
|
|
|
124
|
+USER=jj
|
|
|
125
|
+SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.WrZJikIm6Z/Listeners
|
|
|
126
|
+PATH=/Users/jj/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
|
|
|
127
|
+PWD=/Users/kristofer
|
|
|
128
|
+LANG=en_US.UTF-8
|
|
|
129
|
+XPC_FLAGS=0x0
|
|
|
130
|
+XPC_SERVICE_NAME=0
|
|
|
131
|
+SHLVL=1
|
|
|
132
|
+HOME=/Users/jj
|
|
|
133
|
+GOPROJ=/Users/jj/GitHub/go/src/github.com/ggiti/
|
|
|
134
|
+LOGNAME=jj
|
|
|
135
|
+GOPATH=/Users/jj/GitHub/go/
|
|
|
136
|
+SECURITYSESSIONID=186a7
|
|
|
137
|
+_=/usr/bin/env
|
|
|
138
|
+```
|
|
|
139
|
+
|
|
|
140
|
+you can use `echo` to see a particular setting in the shell, `echo $TERM` would print something like `xterm-256color`.
|
|
|
141
|
+
|
|
|
142
|
+### df & du (disk commands)
|
|
|
143
|
+Running out of disk space is a gradual process that might take years, but can still catch you off guard. There are two commands you can use to check your free space and determine which files are hogging your disk: du (disk usage) and df (disk free). They both take a -h option (human readable). To get a sense of how full your disk is, use the df command:
|
|
|
144
|
+
|
|
|
145
|
+```
|
|
|
146
|
+$ df -h
|
|
|
147
|
+Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
|
|
|
148
|
+/dev/disk1s1 234Gi 61Gi 167Gi 27% 1514122 9223372036853261685 0% /
|
|
|
149
|
+devfs 198Ki 198Ki 0Bi 100% 689 0 100% /dev
|
|
|
150
|
+/dev/disk1s4 234Gi 5.0Gi 167Gi 3% 5 9223372036854775802 0% /private/var/vm
|
|
|
151
|
+map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
|
|
|
152
|
+map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
|
|
|
153
|
+```
|
|
|
154
|
+
|
|
|
155
|
+and du command:
|
|
|
156
|
+
|
|
|
157
|
+```
|
|
|
158
|
+$ du -h -s *
|
|
|
159
|
+ 0B #.bashrc#
|
|
|
160
|
+1.6M Answers
|
|
|
161
|
+ 0B Applications
|
|
|
162
|
+106M Desktop
|
|
|
163
|
+311M Documents
|
|
|
164
|
+1.5G Downloads
|
|
|
165
|
+2.2G GitHub
|
|
|
166
|
+ 72K IdeaProjects
|
|
|
167
|
+ 15G Library
|
|
|
168
|
+ 0B Movies
|
|
|
169
|
+ 0B Music
|
|
|
170
|
+ 14M PageRank-Page-Brin.pdf
|
|
|
171
|
+ 32M Pictures
|
|
|
172
|
+ 0B Public
|
|
|
173
|
+220K UNIX-Ritchie-Thompson.pdf
|
|
|
174
|
+112K UnixAsApp-Mach.pdf
|
|
|
175
|
+1.1M vonnuemann-edvac.pdf
|
|
|
176
|
+ 0B zipcity.php?inputstring=19707
|
|
|
177
|
+```
|
|
|
178
|
+which shows how much space each item in a home directory is taking on the disk.
|