echo
- Print Text to the Terminalhostname
- Print the Name of Your Host Machinegrep
- Finding Strings inside Fileshead
- View the Top of a Filetail
- View the Bottom of a Filesort
- Print a File in Sorted Orderuniq
- Eliminate Duplicate Linesdiff
- Differences between Filesfile
- Show the File Typedate
- Get the Date and Timewc
- Counting characterswhich
- Finding a Program Filewhereis
- Finding Files Used by a Programlocate
- Search for Any Filefind
- Search for Files Using Different Criteriawho
- See Users Logged Onfinger
- Get information on UsersI have posted the answers to Quiz 1 here.
I have posted homework 3.
As usual, it will be due this coming Sunday at 11:59 PM.
Are there any questions before I begin?
man
or info
more
or less
nano
, vi
, vim
, emacs
or any text editors... cd /home/ghoffman/course_files/it244_files ... grep -r foo . ...
cd
commandcd
command given above, the student typed
cd / home/ghoffman/course_files/it244_files
cd
caused the student to go to
the directory /
cd
takes only one argument-r
option to grep
causes it to search files in all subdirectories$ cat go_home_bad.sh # this script contains an error cd /home/ghofman pwd
$ bash go_home_bad.sh
go_home_bad.sh: line 2: cd: /home/ghofman: No such file or directory
/home/ghoffman/course_files/it244_file
$ cat go_home_bad_2.sh # this script contains an error which is # hard to see in the output cd /home/ghofman ls -l
$ bash go_home_bad_2.sh
go_home_bad_2.sh: line 3: cd: /home/ghofman: No such file or directory
-rw-rw-r-- 1 ghoffman faculty 51 Jun 8 11:54 numbers1.txt
-rw-rw-r-- 1 ghoffman faculty 51 Jun 8 10:55 numbers2.txt
-rw-rw-r-- 1 ghoffman faculty 50 Jun 8 10:55 numbers.txt
-rwxrwxr-x 1 ghoffman faculty 94 Jun 8 10:55 print_arg_numbers.sh
-rwxrwxr-x 1 ghoffman faculty 156 Jun 8 10:55 print_foo_bar.sh
-rwxrwxr-x 1 ghoffman faculty 71 Jun 8 10:55 print_foo.sh
-rwxrwxr-x 1 ghoffman faculty 132 Jun 8 10:55 print_positionals.sh
-rwxrwxr-x 1 ghoffman faculty 122 Jun 8 10:55 read_1.sh
-rwxrwxr-x 1 ghoffman faculty 141 Jun 8 10:55 read_2.sh
-rw-rw-r-- 1 ghoffman faculty 1214 Jun 9 10:35 red_sox.txt
-rwxrwxr-x 1 ghoffman faculty 260 Jun 8 10:55 repeat.sh
....
$ bash go_home_bad_2.sh > /dev/null
go_home_bad_2.sh: line 3: cd: /home/ghofman: No such file or directory
> /dev/null
echo
- Print Text to the Terminalecho
prints text to the screen
$ echo Hello Hello
echo
to print the value of a system variable ...$ echo $SHELL /bin/bash
hostname
- Print the Name of Your Host Machinehostname
prints the network name of the machine that you have logged on to
$ hostname pe15
-i
option hostname
will print the IP address of the machine
hostname -i 158.121.104.215
more
and less
less
, just to be confusing, has more features than more
grep
- Finding Strings inside Filesgrep
is used to find all lines in a file that contain a
string
grep
takes two arguments
grep
has the following format
grep STRING FILE [FILE ...]
grep -r
will search recursively through a directorygrep
, like Unix, is case sensitivegrep
ignore case, run it with the -i optiongrep -v
returns all lines that do not match the search stringhead
- View the Top of a Filehead
prints the first 10 lines of any filehead
with a dash, -, and a number
head -20 foo.txt
tail
- View the Bottom of a Filetail
prints the last 10 lines of any filetail
with a dash, -, and a number
tail -5 foo.txt
sort
- Print a File in Sorted Ordersort
prints all the lines in a file in sorted ordersort
does not change the filesort
, by default, sorts in alphabetical ordersort -n
sort -nr
ghoffman@pe15:~/it244/work$
/home/ghoffman/it244/work
$ cat red_sox.txt
2011-07-02 Red Sox @ Astros Win 7-5
2011-07-03 Red Sox @ Astros Win 2-1
2011-07-04 Red Sox vs Blue Jays Loss 7-9
2011-07-05 Red Sox vs Blue Jays Win 3-2
2011-07-06 Red Sox vs Blue Jays Win 6-4
2011-07-07 Red Sox vs Orioles Win 10-4
2011-07-08 Red Sox vs Orioles Win 10-3
2011-07-09 Red Sox vs Orioles Win 4-0
2011-07-10 Red Sox vs Orioles Win 8-6
2011-07-15 Red Sox @ Rays Loss 6-9
2011-07-16 Red Sox @ Rays Win 9-5
2011-07-17 Red Sox @ Rays Win 1-0
2011-07-18 Red Sox @ Orioles Win 15-10
2011-07-19 Red Sox @ Orioles Loss 2-6
2011-07-20 Red Sox @ Orioles Win 4-0
2011-07-22 Red Sox vs Mariners Win 7-4
2011-07-23 Red Sox vs Mariners Win 3-1
2011-07-24 Red Sox vs Mariners Win 12-8
2011-07-25 Red Sox vs Royals Loss 1-3
2011-07-26 Red Sox vs Royals Win 13-9
2011-07-27 Red Sox vs Royals Win 12-5
2011-07-28 Red Sox vs Royals Loss 3-4
2011-07-29 Red Sox @ White Sox Loss 1-3
2011-07-30 Red Sox @ White Sox Win 10-2
2011-07-31 Red Sox @ White Sox Win 5-3
$ grep Rays red_sox.txt
2011-07-15 Red Sox @ Rays Loss 6-9
2011-07-16 Red Sox @ Rays Win 9-5
2011-07-17 Red Sox @ Rays Win 1-0
$ grep Rays red_sox.txt | grep Win
2011-07-16 Red Sox @ Rays Win 9-5
2011-07-17 Red Sox @ Rays Win 1-0
sort
to get the results in the order we want
$ grep Rays red_sox.txt | grep Win | sort -r
2011-07-17 Red Sox @ Rays Win 1-0
2011-07-16 Red Sox @ Rays Win 9-5
uniq
- Eliminate Duplicate Linesuniq
prints the lines in a file, removing adjacent identical lines
$ cat numbers2.txt
4
5
6
7
8
9
10
11
11
11
12
13
14
15
16
17
18
19
20
$ uniq numbers2.txt
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
uniq
to workuniq
will not work with this file
$ cat numbers3.txt 4 5 6 7 8 9 10 11 12 13 11 14 15 11 17 18 19 20 $ uniq numbers3.txt 4 5 6 7 8 9 10 11 12 13 11 14 15 11 17 18 19 20
sort
into uniq
$ sort -n numbers3.txt | uniq 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 20
uniq -i
ignores case when looking for duplicate linesdiff
- Differences between Filesdiff
compares two files and displays the lines that are differentdiff
is confusing
$ cat numbers1.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ cat numbers2.txt 4 5 6 7 8 9 10 11 11 11 12 13 14 15 16 17 18 19 20 $ diff numbers1.txt numbers2.txt 1,3d0 < 1 < 2 < 3 10a8,9 > 11 > 11
diff
was created for use with the Unix patch
utilitypatch
creates a new version of a file from the changes given by diff
-y
option
$ diff -y numbers1.txt numbers2.txt 1 < 2 < 3 < 4 4 5 5 6 6 7 7 8 8 9 9 10 10 > 11 > 11 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20
diff -i
ignores case when looking for differences.file
- Show the File Typefile
utility takes an argument of one or more files ...$ file * bin: directory cars.txt: ASCII text cmds: directory dead.letter: ASCII news text downloads: directory exercises_it244: directory homework_it244: directory it244: symbolic link to `/courses/it244/s19/ghoffman/' java: directory ...
file
on individual files
$ file break.sh break.sh: Bourne-Again shell script, ASCII text executable $ file make_upper.py make_upper.py: Python script, ASCII text executable $ file index.html index.html: HTML document, ASCII text
date
- Get the Date and Timedate
displays the time and date
$ date Tue Aug 7 20:02:48 EDT 2012
date
with a +
and a format string
$ date +"%Y-%m-%d %r"
2012-08-07 08:19:44 PM
info date
* Date conversion specifiers:: %[aAbBcCdDeFgGhjmuUVwWxyY]
date
is run with this option
$ date +%s 1525711270
wc
- Counting characterswc
counts the lines, words and characters in a file
$ cat lines.txt Line 1 Line 2 Line 3 Line 4 Line 5 $ wc lines.txt 5 10 35 lines.txt
wc
what to count
$ wc -c lines.txt 35 lines.txt $ wc -w lines.txt 10 lines.txt $ wc -l lines.txt 5 lines.txt
which
- Finding a Program Filewhich
...tar
program file we would run
$ which tar /bin/tar
which
shows that the executable file for tar
is located in the /bin directory
which
uses the PATH system variable to find the location of the filewhereis
- Finding Files Used by a Programwhereis
is another program that can be used to locate program fileswhereis
takes an approach different from that of which
whereis
searches these locations which
only shows the location of a command
$ which tar /usr/bin/tar
whereis
on tar
we get more information than which
returned
$ whereis tar tar: /usr/bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz
man
uses
to provide information about tar
which
and whereis
which
or whereis
on these programs you will get nothing back
$ which cd $
locate
- Search for Any Filewhich
and whereis
only work on programslocate
can be used to find any filelocate
locate
will search on a partial file name
$ locate foot /etc/update-motd.d/99-footer /usr/share/doc/java-common/debian-java-faq/footnotes.html /usr/share/emacs/23.3/lisp/mail/footnote.elc /usr/share/emacs/23.3/lisp/org/org-footnote.elc /usr/share/libparse-debianchangelog-perl/footer.tmpl /usr/share/xml-core/catalog.footer /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/Kconfig /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/Makefile /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/Makefile.boot /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/debug-macro.S /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/entry-macro.S /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/hardware.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/io.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/irqs.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/isa-dma.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/memory.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/system.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/timex.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/uncompress.h /usr/src/linux-headers-3.0.0-12/arch/arm/mach-footbridge/include/mach/vmalloc.h
locate
does not actually search the file system itselfupdatedb
updatedb
is usually run automatically in the background to update the databasefind
- Search for Files Using Different Criteriafind
locate
find
can be used to search for a file based on many different things such as
find
is beyond the scope of this coursefind
is searching for files by name
find DIRECTORY -name FILENAME
$ find /home/ghoffman -name red_sox.txt /home/ghoffman/course_files/it244_files/red_sox.txt
locate
, find
will not accept a partial file name
$ find /home/ghoffman -name red $
$ find /home/ghoffman -name memo.\*
/home/ghoffman/memo.bak
/home/ghoffman/memo.txt
/home/ghoffman/tmp/memo.bak
/home/ghoffman/tmp/memo.txt
/home/ghoffman/tmp/memo.backup
who
- See Users Logged Onwho
prints a list of all users currently logged on to the machine
$ who ghoffman pts/0 2012-08-12 13:41 (dsl092-066-161.bos1.dsl.speakeasy.net) rouilj pts/1 2012-08-12 04:25 (pool-74-104-161-40.bstnma.fios.verizon.net) eb pts/2 2012-08-12 08:19 (pool-96-237-251-11.bstnma.fios.verizon.net)
who
also provides information about each user's login sessionwho am i
will show the user who is logged into a specific terminal
$ who am i ghoffman pts/0 2012-08-12 13:41 (dsl092-066-161.bos1.dsl.speakeasy.net)
$ whoami ghoffman
finger
- Get information on Usersfinger
, like mv
, has two functionsfinger
shows every user currently logged in
$ finger Login Name Tty Idle Login Time Office Office Phone admerrib Adil Merribi pts/23 35 Sep 8 12:19 (75.69.126.23) apera Antonio Pera *pts/2 23:47 Sep 7 04:43 (73.16.254.131) bnsevak Birva Sevak pts/12 14 Sep 8 11:45 (65.96.118.54) gabrgabr Gabriel Park pts/13 45d Jul 17 09:46 (tmux(29686).%0) gabrgabr Gabriel Park pts/14 52d Jul 17 16:09 (tmux(29686).%5) gabrgabr Gabriel Park pts/17 52d Jul 17 16:23 (tmux(29686).%6) gabrgabr Gabriel Park pts/20 45d Jul 18 19:39 (tmux(29686).%8) gghinita Gabriel Ghinita pts/9 5d Sep 2 16:00 (192.168.105.101) ghoffman Glenn Hoffman pts/21 Sep 8 12:51 (209.6.202.123) grullnor Raffi T Sahagian pts/11 20:42 Sep 5 15:27 (192.168.107.156) grullnor Raffi T Sahagian pts/26 29 Sep 8 12:28 (73.126.84.97) ldavid Leonard David pts/1 3d Sep 3 11:34 (158.121.23.66) lilmnh Michael Nguyen pts/16 Sep 8 12:38 (71.174.229.240) mkhr Mikhail Khayretdinov pts/8 19 Sep 8 10:54 (76.28.116.136) offner Carl D. Offner pts/0 2d Jul 30 07:38 (50.200.93.68) offner Carl D. Offner pts/22 3d Sep 4 15:20 (192.168.107.170) rickm Rick Martin pts/5 20:43 Aug 17 09:42 (192.168.105.41) vyiep Vinson Yiep pts/10 5d Jul 24 18:30 (73.47.67.141:S.0) xiaohui Xiaohui Liang pts/7 2d Sep 6 10:55 (192.168.107.184)
finger
provides information about the account
$ finger ghoffman Login: ghoffman Name: Glenn Hoffman Directory: /home/ghoffman Shell: /bin/bash On since Sun Sep 8 12:51 (EDT) on pts/21 from 209.6.202.123 Mail forwarded to glennhoffman@mac.com Mail last read Fri Sep 6 12:04 2019 (EDT) Plan: Office: McCormack 3-0201-22 (Pterosaur poster on door) Office Hours: Tuesday & Thursday, 11:OO AM - 12:00 PM and 5:30 to 6:30 PM Classes: IT 116-1 Introduction to Scripting TuTh 12:30 - 1:45 PM Wheatley 1-004 IT 244-1 Introduction to Linux/Unix TuTh 2:00 - 3:15 PM McCormack M1-409 IT 117-1 Intermediate Scripting TuTh 4:00 - 5:15 PM University Hall 2-2310 Home Address: 40 Central Street Somerville, MA 02143 617-821-3936 (9am - 9pm only)