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 Timebzip2
gzip
- the GNU Compression Utilitytar
- Packaging Files for Transfer or Storagewhich
- 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 UsersThe reading assignment for this week is chapter 3 of Sobell, The Utilities.
You will find a list of all reading assignments here.
There is a link to this page on the class web page.
I have posted Homework 3 here.
As usual, it will be due next Sunday at 11:59 PM.
The is the first homework assignment that will require you to write a script.
Your script must meet certain requirements that I mentioned in the last class.
Let's review the requirements for homework scripts.
... cd /home/tsoro/course_files/it244_files ... grep -r foo . ...
cd
commandcd
command given above ...cd / home/tsoro/course_files/it244_files
cd
command changed the directory to / ...cd takes only one argument
-r
option to grep
causes it to search files ...$ 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/tsoro: No such file or directory
/home/tsoro/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 itserver6
-i
option ...hostname
will print the IP address of the machine
hostname -i 192.168.107.136
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 ...grep
takes two arguments
grep
has the following format
grep STRING FILE [FILE ...]
grep
on the files in a directory ...grep -r
will search through all files in a directory ...grep
, like Unix, is case sensitivegrep
ignore case ...grep
find all lines that do not match a string ...head
- 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 ...sort
does not change the filesort
, by default, sorts in alphabetical ordersort -n
sort -nr
uniq
- Eliminate Duplicate Linesuniq
prints the lines in a file$ 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 worksort
along with uniq
uniq -i
ignores case when looking for duplicate linesdiff
- Differences between Filesdiff
compares two files ...diff
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
utility ...diff
$ 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.tsoro@vm74:~/it244/work$
/home/tsoro/it244/work
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 f11_it244_class_web: directory hw_it244: directory it244: symbolic link to `/courses/it244/s12/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
$ 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
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]
tofrodos
unix2dos
tofrodos
package provides todos
...fromdos
to go the other wayunix2dos
package uses unix2dos
...dos2unix
to go the other waybzip2
bzip2
is one such utilitybzip2
like this
bzip2 FILENAME
bzip2
compresses the file ...bzip2
with the -k
(for keep) optionbzip2
...bunzip2
like this
bunzip2 FILENAME.bz2
bunzip2
decompresses the .bz2 file ...bunzip2
has compressed is unreadablebzcat
bzcat
, will print the uncompressed contents of a file to the screenbzcat
does not alter the original, compressed filegzip
- the GNU Compression Utilitygzip
to compress filesbzip2
bzip2
gzip
creates have a .gz extensionzcat
gunzip
tar
- Packaging Files for Transfer or Storagetar
(tape archive) is the Unix utility used for this purposetar
does not compress filestar
is usually used along with a compression programtar
to create a single file ...tar
to unpack the filestar
with different options ...
tar cvf ARCHIVE_NAME.tar DIRECTORY_NAME
tar -tvf ARCHIVE_FILE
tar -xvf ARCHIVE_FILE
-x
option stands for extractbzip2
is used for compression ...gzip
is used ...which
- Finding a Program Filewhich
...tar
program file we would run
$ which tar /bin/tar
which
shows that the executable file for tar
...which
uses the PATH system variable ...whereis
- 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 locationswhereis
on tar
which
returned
$ whereis tar tar: /bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz
man
uses ...tar
which
and whereis
which
or whereis
on these programs ...$ 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/tsoro -name red_sox.txt /home/tsoro/course_files/it244_files/red_sox.txt /home/tsoro/course_files/it441_files/red_sox.txt
locate
, find
will not accept a partial file name
$ find /home/tsoro -name red $
$ find /home/tsoro -name memo.\*
/home/tsoro/memo.bak
/home/tsoro/memo.txt
/home/tsoro/tmp/memo.bak
/home/tsoro/tmp/memo.txt
/home/tsoro/tmp/memo.backup
who
- See Users Logged Onwho
prints a list of all users currently logged on to the machine
$ who tsoro 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 tsoro pts/0 2012-08-12 13:41 (dsl092-066-161.bos1.dsl.speakeasy.net)
$ whoami tsoro
finger
- Get information on Usersfinger
provides information about Unix accounts
$ finger tsoro Login: ghoffman Name: Glenn Hoffman Directory: /home/tsoro Shell: /bin/bash On since Thu Sep 24 11:43 (EDT) on pts/5 from 158.121.249.7 5 seconds idle Mail forwarded to tsoros@gmail.com New mail received Fri Sep 18 14:00 2015 (EDT) Unread since Fri Sep 18 07:43 2015 (EDT) Plan: Office: Science 3-92A (Pterosaur poster on door) Office Hours: Tuesday & Thursday, 12:30 - 1:30 PM and 5:30 - 6:30 PM On Campus: Wednesday 1 - 6PM Appointment needed on Wednesday Classes: IT 244-01 Introduction to Linux/Unix TuTh 2:00 - 3:15 PM Science S3-143 (IT Lab) IT 244-02 Introduction to Linux/Unix TuTh 4:00 - 5:15 PM Science S3-028 (Web Lab) IT 341_02 Introduction to System Administration TuTh 7:00 - 8:15 PM Science S3-143 (IT Lab) Home Address: 40 Central Street 617-821-3936 (home; 9am - 9pm only) Somerville, MA 02143
finger
, like mv
, has two functionsfinger
shows every user currently logged in
$ finger Login Name Tty Idle Login Time Office Office Phone eb Ethan Bolker pts/4 3:35 Sep 22 19:19 (pool-108-26-203-122.bstnma.fios.verizon.net) ghoffman Glenn Hoffman pts/5 Sep 24 11:43 (158.121.249.7) root root *tty1 52d Mar 11 2015
finger
$ finger hoffman Login: ghoffman Name: Glenn Hoffman Directory: /home/ghoffman Shell: /bin/bash On since Thu Sep 24 11:43 (EDT) on pts/5 from 158.121.249.7 2 seconds idle Mail forwarded to glennhoffman@mac.com New mail received Fri Sep 18 14:00 2015 (EDT) Unread since Fri Sep 18 07:43 2015 (EDT) Plan: Office: Science 3-92A (Pterosaur poster on door) Office Hours: Tuesday & Thursday, 12:30 - 1:30 PM and 5:30 - 6:30 PM On Campus: Wednesday 1 - 6PM Appointment needed on Wednesday Classes: IT 244-01 Introduction to Linux/Unix TuTh 2:00 - 3:15 PM Science S3-143 (IT Lab) IT 244-02 Introduction to Linux/Unix TuTh 4:00 - 5:15 PM Science S3-028 (Web Lab) IT 341_02 Introduction to System Administration TuTh 7:00 - 8:15 PM Science S3-143 (IT Lab) Home Address: 40 Central Street 617-821-3936 (home; 9am - 9pm only) Somerville, MA 02143 Login: ghoffmn Name: Glenn Legacy Hoffman Directory: /home/ghoffmn Shell: /bin/bash Last login Mon Jul 13 13:14 (EDT) on pts/5 from dsl092-066-161.bos1.dsl.speakeasy.net Mail forwarded to glennhoffman@mac.com Mail last read Thu May 28 18:30 2015 (EDT) Plan: Office: McCormack M-3-607 Fall 2014 Office Hours: Tuesday & Thursday, 10:00 - 12:00 PM and by appointment Classes: IT 341-2 Introduction to System Administration TuTh 12:30-1:45 S3-148 (IT Lab) IT 244-1 Introduction to Linux/Unix TuTh 2:00-3:15 S3-028 (Web Lab) IT 244-3 Introduction to Linux/Unix TuTh 4:00-5:15 S3-148 (IT Lab) IT 244-2 Introduction to Linux/Unix TuTh 5:30-6:45 S3-028 (Web Lab) Home Address: 40 Central Street 617-821-3936 (home; 9am - 9pm only) Somerville, MA 02143 Login: it244gh Name: Glenn Hoffman Dummy Directory: /home/it244gh Shell: /users/nologin Never logged in. Mail forwarded to glennhoffman@mac.com No mail. Plan: This account is a test account for Glenn Hoffman teaching it244