-
Notifications
You must be signed in to change notification settings - Fork 342
/
basics.sh
317 lines (215 loc) · 7.85 KB
/
basics.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# some basic unix commands
# you can execute this file by typing 'sh basics.sh'
# not that it does anything useful though
# run the following command to install 'emojify'
# sudo sh -c "curl https://raw.githubusercontent.com/mrowa44/emojify/master/emojify -o /usr/local/bin/emojify && chmod +x /usr/local/bin/emojify"
# https://github.com/mrowa44/emojify
# ssh into your machine
ssh root@your-ip-address
# transfer file with sftp
sftp root@your-ip-address
# lists files
ls
# lists files hidden file and directories
ls -a
# lists files in long format
ls -l
# list the present working directory
pwd
# the . indicates the current directory
# the .. indicate the parent directory
# cd .
# go to the home directory
cd ~ # cd works as well
# go to previous directory
# cd -
# create an empty file
touch original.txt
# concatenate and print file
cat original.txt
# copy file
cp original.txt copy.txt
# rename file
mv copy.txt clone.txt
# delete file
rm original.txt clone.txt
# create a directory
mkdir folder
# remove a directory
rmdir folder
# recursively and forcefully remove directories and their contents
rm -rf folder
# bang (!) repeats the last command starting with the first letter after it
!t # executes 'touch original.txt'
# write a line of text to standard output
# the file descriptor for standard output (stdout) is 1
echo "Hello World"
# print without a new line
echo -n "Hello World"
# redirect stdout to a file
echo "Hello World" > original.txt
# you can append to a file as well
echo "Hello Again World" >> original.txt
# you can have more than one command in one line by using the semi-colon
echo "excuse me?" ; echo "come again?"
# simple arithmetic with echo
echo $((1+1))
# generate strings from A to Z
echo {A..Z}
# print out the first ten lines of a file
# you can also use the -n option to specify how many lines you want
head original.txt
# you can print out the last few lines of a file using tail
# you can monitor a file as it gets updated with the -f option, for example follow the last 10 lines with -10f
tail original.txt
tail -10f original.txt
# show the environment variables
env
# show the current PATH
echo $PATH
# generate a random number
echo $RANDOM
# create your own environment variable
MESSAGE="UNIX is fun!"
# cut out the third field of a string delimited by a space
echo $MESSAGE | cut -f3 -d" "
# cut out the first four characters
echo $MESSAGE | cut -c1-4
# count how characters there are in a string (-w for words)
echo $MESSAGE | wc -c
#As an alternative to echo, can use printf
printf $MESSAGE
greeting="HELLO"
# translate all uppercase characters to lowercase
echo $greeting | tr [:upper:] [:lower:]
# uppercase to lowercase using character ranges instead
echo $greeting | tr [A-Z] [a-z]
# show the type of the a file
file original.txt
# download a file from the web
curl -o readme.txt https://raw.github.com/tacksoo/bash_basics/master/README.md
# download file with its original filename
curl -O https://bootstrap.pypa.io/get-pip.py
# redo previous command
!!
# find about the location of a command
which bash
# archive the contents of a folder
tar cvf archive.tar somedir
# archieve the contents of a folder and compress it
tar zcvf archive.tar.gz somedir
# unarchive the contents of an archive
tar xvf archive.tar.gz
# list contents of an archive
tar tf archive.tar.gz
# compress file into zip file that can be decompressed in mac and windows
zip somefile.zip somefile
# compress folder into zip file
zip -r somefile.zip somedir
# decompress zip file
unzip somefile.zip
# compress a file and see the original go away
gzip somefile
# decompress a file and see the original go away
gzip -d somefile.gz
# compress a file and keep original
gzip -c somefile > somefile.gz
# decompress a file and keep original
gzip -cd somefile.gz > somefile
# bzip2 can offer better compression and can be used just like gzip
# https://gist.githubusercontent.com/tacksoo/dbb221a6ea2c605cf59e/raw/d05e22635ce4d1a67c52c0180fbf5a34d25460bc/words.txt
# globally search a regular expression and print - grep
# print out lines that contain word 'the' in readme.txt
grep 'the' readme.txt
# print out lines that contain the whole the word 'the' in readme.txt
grep -w 'the' readme.txt
# ignore case
grep -i 'the' readme.txt
# count how many times the word 'the' matched
grep -c 'the' readme.txt
# show the line number of where the match occurs
grep -n 'the' readme.txt
# match 'the' starting with lowercase or uppercase t
grep '[Tt]he' readme.txt
# match any three numbers in a row
grep '[0-9][0-9][0-9]' readme.txt
# match zipcode
grep -w '[0-9]\{5\}' readme.txt
# match the word 'the' that start from the beginning of a line
grep '^the' readme.txt
# match the word 'the' at the end of a line
grep 'the$' readme.txt
# check every file in the current directory for the word 'the' and print out the filename
grep -l 'the' *
# search for file in the current directory
ls | grep file
# Create a file called file.sh and change the permissions of a file
touch file.sh
chmod u+x file.sh
# sorts a text file line by line lexicographically
sort readme.txt
# sort and print only unique lines
sort -u readme.txt
# reverse sort
sort -r readme.txt
# do a numeric sort i.e. 10 appears after 9 not 1
sort -n readme.txt
# sort from a particular field, in this case start sorting from field 2 and end at field 2
sort -k2,2 readme.txt
# the stream editor command is useful for adding/deleting/changing words in a text file
# the following command appends the word 'Start: ' at the beginning of every line
sed 's/^/Start: /' readme.txt
# this would only apply to the first five lines
sed '1,5s/^/Start: /' readme.txt
# you can also delete words using sed, the following will delete all the words 'the'
sed 's/the//' readme.txt
# of course you can also replace words
sed 's/teh/the/' readme.txt
# comment out any statement that starts with Log
sed 's/Log/\/\/&/' readme.txt
# delete all leading spaces of a string
sed 's/^[ \t]*//' readme.txt
# find out the access, modify, and change time for a file
stat readme.txt
# find all the files ending with .txt from the current directory
find . -name '*.txt' -print
# find all files that were modified later than readme.txt
find . -newer readme.txt -print
# find all files created in the last 3 days (ctime, mtime, atime)
find . -ctime -3 -print
# look for the word 'the' in the files modified in the last 3 days
find . -mtime -3 -print | xargs grep 'the'
# look for files 'newer' than readme.txt
find . -newer readme.txt -print
# look for the word 'the' in every "file"
find . -type f | xargs grep 'the'
# look for the word 'the' in every "file" including files that contain a space
# http://serverfault.com/questions/268368/how-can-i-handle-spaces-in-file-names-when-using-xargs-on-find-results
find . -type f -print0 | xargs -0 grep 'the'
# look for a files in a directory starting with extenson .txt and delete files which are n number of days old
# Note : In place of InputPath and InputNumberofdaysoldfile pass your path and number of days old to delete files from that path
InputPath='/folder/demo/'
InputNumberofdaysoldfile=30
find $InputPath -name "*.txt" -type f -mtime +${InputNumberofdaysoldfile} -exec rm -f {} \;
# heredoc http://en.wikipedia.org/wiki/Here_document
cat << EOF
Write a wall of text without echo commands
EOF
# show today's date and time
date
# show today's date with different time zone:
# change the TZ variable to TZ="America/New_York", TZ="Asia/Seoul", TZ="Europe/Berlin", etc
TZ="America/Los_Angeles" date
# install some programs in ubuntu
sudo apt-get install imagemagick
sudo apt-get install fortune
sudo apt-get install dict
sudo apt-get install weather-util
# resize an image using imagemagick
convert cute.gif -resize 200% cute_big.gif
# look up a word using dict
dict love
# print a quote using fortune
fortune # note: you need to add /usr/games to your path
# create a one megabyte file filled with zeros
dd if=/dev/zero of=foobar count=1 bs=1M